欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android 手機(jī)截取長(zhǎng)屏實(shí)例代碼

 更新時(shí)間:2017年06月23日 17:16:27   作者:喜歡萌妹子的少年  
本篇文章主要介紹了android 手機(jī)截取長(zhǎng)屏實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

最近項(xiàng)目遇到一個(gè)需求:把當(dāng)前頁(yè)面保存到手機(jī)相冊(cè)。想了想 我還不會(huì)呢,就百度了下大神的足跡,踏著大神的足跡,一路向前。廢話不說,記錄下,后期學(xué)習(xí)。

 public class ScreenUtils {

/**
 * 截取scrollview的屏幕
 * @param scrollView
 * @return
 */
public static Bitmap getBitmapByView(ScrollView scrollView) {
  int h = 0;
  Bitmap bitmap = null;
  // 獲取scrollview實(shí)際高度
  for (int i = 0; i < scrollView.getChildCount(); i++) {
    h += scrollView.getChildAt(i).getHeight();
    scrollView.getChildAt(i).setBackgroundColor(
        Color.parseColor("#ffffff"));
  }
  // 創(chuàng)建對(duì)應(yīng)大小的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實(shí)際高度
  for (int i = 0; i < listView.getChildCount(); i++) {
    h += listView.getChildAt(i).getHeight();
  }
  // 創(chuàng)建對(duì)應(yīng)大小的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();
  // 質(zhì)量壓縮方法,這里100表示不壓縮,把壓縮后的數(shù)據(jù)存放到baos中
  image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
  int options = 100;
  // 循環(huán)判斷如果壓縮后圖片是否大于250K,大于繼續(xù)壓縮
  while (baos.toByteArray().length / 1024 > 1024 && options >10) {
    // 重置baos
    baos.reset();
    // 這里壓縮options%,把壓縮后的數(shù)據(jù)存放到baos中
    image.compress(Bitmap.CompressFormat.JPEG, options, baos);
    // 每次都減少10
    options -= 10;
  }
  // 把壓縮后的數(shù)據(jù)baos存放到ByteArrayInputStream中
  ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
  // 把ByteArrayInputStream數(shù)據(jù)生成圖片
  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");
  // 如果文件不存在,則創(chuàng)建一個(gè)新文件
  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();
  }

  // 其次把文件插入到系統(tǒng)圖庫(kù)
  try {
    MediaStore.Images.Media.insertImage(context.getContentResolver(),
        outfile.getAbsolutePath(), fname, null);
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
  // 最后通知圖庫(kù)更新
  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)));

好了,截取成功了!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論