国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android 手機截取長屏實例代碼

2019-12-12 02:36:44
字體:
來源:轉載
供稿:網友

最近項目遇到一個需求:把當前頁面保存到手機相冊。想了想 我還不會呢,就百度了下大神的足跡,踏著大神的足跡,一路向前。廢話不說,記錄下,后期學習。

 public class ScreenUtils {/** * 截取scrollview的屏幕 * @param scrollView * @return */public static Bitmap getBitmapByView(ScrollView scrollView) {  int h = 0;  Bitmap bitmap = null;  // 獲取scrollview實際高度  for (int i = 0; i < scrollView.getChildCount(); i++) {    h += scrollView.getChildAt(i).getHeight();    scrollView.getChildAt(i).setBackgroundColor(        Color.parseColor("#ffffff"));  }  // 創建對應大小的bitmap  bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,      Bitmap.Config.RGB_565);  final Canvas canvas = new Canvas(bitmap);  scrollView.draw(canvas);  return bitmap;}/** * 截圖listview * **/public static Bitmap getListViewBitmap(ListView listView,String picpath) {  int h = 0;  Bitmap bitmap;  // 獲取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);  return bitmap;}/** * 壓縮圖片 * @param image * @return */public static Bitmap compressImage(Bitmap image) {  ByteArrayOutputStream baos = new ByteArrayOutputStream();  // 質量壓縮方法,這里100表示不壓縮,把壓縮后的數據存放到baos中  image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  int options = 100;  // 循環判斷如果壓縮后圖片是否大于250K,大于繼續壓縮  while (baos.toByteArray().length / 1024 > 1024 && options >10) {    // 重置baos    baos.reset();    // 這里壓縮options%,把壓縮后的數據存放到baos中    image.compress(Bitmap.CompressFormat.JPEG, options, baos);    // 每次都減少10    options -= 10;  }  // 把壓縮后的數據baos存放到ByteArrayInputStream中  ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  // 把ByteArrayInputStream數據生成圖片  Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);  return bitmap;}/** * 保存到sdcard * @param b * @return */public static String savePic(Context context, Bitmap b) {  File outfile = new File("/sdcard/image");  // 如果文件不存在,則創建一個新文件  if (!outfile.isDirectory()) {    try {      outfile.mkdir();    } catch (Exception e) {      e.printStackTrace();    }  }  String fname = outfile + "/" + System.currentTimeMillis() + ".jpg";  FileOutputStream fos = null;  try {    fos = new FileOutputStream(fname);    if (null != fos) {      b.compress(Bitmap.CompressFormat.JPEG, 90, fos);      fos.flush();      fos.close();    }  } catch (FileNotFoundException e) {    e.printStackTrace();  } catch (IOException e) {    e.printStackTrace();  }  // 其次把文件插入到系統圖庫  try {    MediaStore.Images.Media.insertImage(context.getContentResolver(),        outfile.getAbsolutePath(), fname, null);  } catch (FileNotFoundException e) {    e.printStackTrace();  }  // 最后通知圖庫更新  context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fname)));  return fname;   }}

以上為百度的工具類。

使用方法:

 ScreenUtils   .savePic(XXXActivity.this,ScreenUtils.compressImage(ScreenUtils      .getBitmapByView(XXXScrollView)));

好了,截取成功了!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汾阳市| 柘荣县| 正镶白旗| 焉耆| 郧西县| 和龙市| 体育| 许昌市| 疏勒县| 沾化县| 淳化县| 华阴市| 揭东县| 嘉定区| 会昌县| 陵川县| 合作市| 宜宾县| 祁阳县| 佛学| 赤壁市| 临城县| 福安市| 长治市| 大悟县| 九寨沟县| 曲沃县| 南京市| 临泉县| 谷城县| 高要市| 黎川县| 绍兴市| 油尖旺区| 福海县| 阳新县| 星座| 顺昌县| 内黄县| 柞水县| 卓尼县|