Android实现截屏和截长图功能的各种方法 您所在的位置:网站首页 安卓系统截长屏 Android实现截屏和截长图功能的各种方法

Android实现截屏和截长图功能的各种方法

2024-07-12 19:47| 来源: 网络整理| 查看: 265

  微信好友或者朋友圈的分享,可以是普通的截图分享,也可以是截取长图的分享,甚至还会有需求让你拼上生成的二维码和logo图片,下面我们直接来看看这些方法的使用:

先说一下拼接三张不同的图片后有黑色背景的解决方案(在下面的6拼接合成图片的方法里加上就可以了):

//设置画布背景色为白色,即自定义控件显示的背景色为白色: canvas.drawRGB(255,255,255);

1.普通的截屏方法

/** * 截屏 * * @param activity * @return */ public static Bitmap activityShot(Activity activity) { /*获取windows中最顶层的view*/ View view = activity.getWindow().getDecorView(); //允许当前窗口保存缓存信息 view.setDrawingCacheEnabled(true); view.buildDrawingCache(); //获取状态栏高度 Rect rect = new Rect(); view.getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; WindowManager windowManager = activity.getWindowManager(); //获取屏幕宽和高 DisplayMetrics outMetrics = new DisplayMetrics(); windowManager.getDefaultDisplay().getMetrics(outMetrics); int width = outMetrics.widthPixels; int height = outMetrics.heightPixels; //去掉状态栏 Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight); //销毁缓存信息 view.destroyDrawingCache(); view.setDrawingCacheEnabled(false); return bitmap; }

2.截取scrollView的屏幕方法

/** * 截取scrollview的屏幕 * @param scrollView * @return */ public static Bitmap getBitmapByView(ScrollView scrollView) { int h = 0; Bitmap bitmap = null; // 获取listView实际高度 for (int i = 0; i < scrollView.getChildCount(); i++) { h += scrollView.getChildAt(i).getHeight(); scrollView.getChildAt(i).setBackgroundResource(R.drawable.white_drawable); } // 创建对应大小的bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); scrollView.draw(canvas); // 测试输出 FileOutputStream out = null; try { out = new FileOutputStream("/sdcard/screen_test.png"); } catch (FileNotFoundException e) { e.printStackTrace(); } try { if (null != out) { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); } } catch (IOException e) { // TODO: handle exception } return bitmap; }

3.截取ListvListView的屏幕方法

/** * 截图listview * **/ public static Bitmap getbBitmap(ListView listView) { int h = 0; Bitmap bitmap = null; // 获取listView实际高度 for (int i = 0; i < listView.getChildCount(); i++) { h += listView.getChildAt(i).getHeight(); } // 创建对应大小的bitmap bitmap = Bitmap.createBitmap(listView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); listView.draw(canvas); // 测试输出 FileOutputStream out = null; try { out = new FileOutputStream("/sdcard/screen_test.png"); } catch (FileNotFoundException e) { e.printStackTrace(); } try { if (null != out) { bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); } } catch (IOException e) { // TODO: handle exception } return bitmap; }

4.截取线性布局或相对布局屏幕的方法

/** * 截取RelativeLayout **/ public static Bitmap getRelativeLayoutBitmap(RelativeLayout relativeLayout) { int h = 0; Bitmap bitmap; for (int i = 0; i < relativeLayout.getChildCount(); i++) { h += relativeLayout.getChildAt(i).getHeight(); } // 创建对应大小的bitmap bitmap = Bitmap.createBitmap(relativeLayout.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); relativeLayout.draw(canvas); return bitmap; } /** * 截取LinearLayout **/ public static Bitmap getLinearLayoutBitmap(LinearLayout linearLayout) { int h = 0; Bitmap bitmap; for (int i = 0; i < linearLayout.getChildCount(); i++) { h += linearLayout.getChildAt(i).getHeight(); } // 创建对应大小的bitmap bitmap = Bitmap.createBitmap(linearLayout.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); linearLayout.draw(canvas); return bitmap; }

5.截取除了导航栏之外的整个屏幕

/** * 截取除了导航栏之外的整个屏幕 */ public static Bitmap screenShotWholeScreen(Activity activity) { View dView = activity.getWindow().getDecorView(); dView.setDrawingCacheEnabled(true); dView.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache()); return bitmap; }

6.拼接合成图片的方法

//合成三张图片 private static Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap, Bitmap threeBitmap) { Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight() + secondBitmap.getHeight() + threeBitmap.getHeight(), firstBitmap.getConfig()); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(firstBitmap, new Matrix(), null); canvas.drawBitmap(secondBitmap, 0, firstBitmap.getHeight(), null); canvas.drawBitmap(threeBitmap, secondBitmap.getWidth(), firstBitmap.getHeight(), null); return bitmap; } //合成两张图片 public static Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) { Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(),firstBitmap.getConfig()); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(firstBitmap, new Matrix(), null); canvas.drawBitmap(secondBitmap, 0, 0, null); return bitmap; }

7.具体的调用和拼接合成图片:

Bitmap bitmap = StringUtil.activityShot(JCZQTDetailActivity.this); Bitmap bitmap = StringUtil.getBitmapByView(scrollView_jc_type);//截取长图 // Bitmap bitmap = StringUtil.getRelativeLayoutBitmap(rl_jc_all); // Bitmap bitmap1 = StringUtil.screenShotWholeScreen(JCZQTDetailActivity.this); // Bitmap bitmap2 = ShareUtil.mergeBitmap(bitmap, bitmap1);ShareUtil.weiChatPic(Constants.wx_api, 7, MakeReceiptDetailsActivity.this, bitmap2);

8.微信分享图片方法和链接生成二维码图片方法:

/** * 分享 图片 */ public static void weiChatPic(IWXAPI api, int flag, Context context, Bitmap bitmap) { if (api.isWXAppInstalled()) {//判断微信是否安装 //Bitmap mWXShareBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo_icon);//将截屏得到的bitmap赋值 GlobalLog.e("sgf", "-------GlobalEntity.USER.getUrl()-------" + GlobalConfig.getURL(context)); if ("".equals(GlobalConfig.getURL(context)) || "null".equals(GlobalConfig.getURL(context))) { Intent intent = new Intent(context, LoginActivity.class); context.startActivity(intent); return; } loadRQInfo(GlobalConfig.getURL(context)); Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.sharewinlogo); GlobalLog.e("sgf", "-------bitmap-------" + bitmap); GlobalLog.e("sgf", "-------bitmaps-------" + bitmaps); GlobalLog.e("sgf", "-------b-------" + b); Bitmap bitmap1 = mergeBitmap(bitmap, bitmaps, b); WXImageObject imgObject = new WXImageObject(bitmap1); //imgObject.imagePath WXMediaMessage mediaMessage = new WXMediaMessage(); mediaMessage.mediaObject = imgObject; //设置缩略图 Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap1, bitmap1.getWidth() / 10, bitmap1.getHeight() / 10, true); mediaMessage.thumbData = bmpToByteArray(thumbBmp, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("img");//分享类型是图片 req.message = mediaMessage; //表示发送给朋友圈 WXSceneTimeline 表示发送给朋友 WXSceneSession req.scene = flag == 7 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline; api.sendReq(req); GlobalLog.e("sgf", "-------end-------"); } else { Toast.makeText(context, "您没有安装微信客户端", Toast.LENGTH_SHORT).show(); } /** * 生成万二维码 * * @param soldUrl */ private static void loadRQInfo(String soldUrl) { //回收bitmaps if (null != bitmaps && !bitmaps.isRecycled()) { bitmaps.recycle(); bitmaps = null; } try { bitmaps = StringUtil.makeQRImage(soldUrl, 480, 350); } catch (WriterException e) { e.printStackTrace(); } }

9.项目中的StringUtil类,在此分享出来:

package com.yasenagat.yy.rf.util; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Rect; import android.os.Environment; import android.text.Html; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.ScrollView; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.yasenagat.yy.rf.R; import com.yasenagat.yy.rf.common.GlobalException; import com.yasenagat.yy.rf.common.GlobalLog; import static com.yasenagat.yy.rf.R.id.iv_codes_qr; import static com.yasenagat.yy.rf.R.id.linearLayout; public class StringUtil { private static int counter = 0; private static final String TAG = "StringUtil"; public static boolean isEmpty(String str) { if (null == str || str.trim().equals("")) { return true; } return false; } public static boolean isEmptyMoney(String str) { try { if (null == str || str.equals("") || str.equals("0") || str.equals("0.00") || 0 == Double.parseDouble(str)) { return true; } if (Double.parseDouble(str) == 0) { return true; } } catch (NumberFormatException e) { e.printStackTrace(); return false; } return false; } // String five_before = hitlistsBean.getHit_five().substring(0, hitlistsBean.getHit_five().indexOf(":")); //String five_after = hitlistsBean.getHit_five().substring(hitlistsBean.getHit_five().indexOf(":") + 1, hitlistsBean.getHit_five().length()); /** * 截取字符串的前半截 * * @param str * @return */ public static String subStringBefore(String str) { String beforeStr = str.substring(0, str.indexOf(":")); return beforeStr; } /** * 截取字符串的后半截 * * @param str * @return */ public static String subStringAfter(String str) { String afterStr = str.substring(str.indexOf(":") + 1, str.length()); return afterStr; } /** * 截取":" 字符的字符串数组 * * @param str * @return 返回一个数组 */ public static String[] subStringArray_one(String str) { String StrArray[] = str.split(":"); return StrArray; } /** * 截取":" 字符的字符串数组 * * @param str * @return 返回一个数组 */ public static String[] subStringArray_two(String str) { String[] StrArray = str.split(","); return StrArray; } /** * 截取":" 字符的字符串数组 * * @param str * @return 返回一个数组 */ public static String[] subStringArray_three(String str) { String[] StrArray = subStringArray_two(str); //for(){} return StrArray; } /** * 判断str1中包含str2的个数 * * @param str1 * @param str2 * @return counter */ public static int countStr(char str1, String str2) { int countss = 0; for (int i = 0; i < str2.length(); i++) { if (str2.charAt(i) == str1) { countss++; } } return countss; } /** * 截取()中的字符串 * * @param str2 * @return counter */ public static String countStrParentheses(String str2) { // TODO SGF ADD String str1 = ""; Pattern pattern = Pattern.compile("(?


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有