android 手機截取長屏實例代碼
更新時間:2017年06月23日 17:16:27 作者:喜歡萌妹子的少年
本篇文章主要介紹了android 手機截取長屏實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
最近項目遇到一個需求:把當(dāng)前頁面保存到手機相冊。想了想 我還不會呢,就百度了下大神的足跡,踏著大神的足跡,一路向前。廢話不說,記錄下,后期學(xué)習(xí)。
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"));
}
// 創(chuàng)建對應(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實際高度
for (int i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
}
// 創(chuàng)建對應(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)建一個新文件
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)圖庫
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)));
好了,截取成功了!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)簡易計算器(可以實現(xiàn)連續(xù)計算)
這篇文章主要為大家詳細介紹了Android實現(xiàn)簡易計算器,可以實現(xiàn)連續(xù)計算,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-03-03
Android應(yīng)用開發(fā)的版本更新檢測升級功能實現(xiàn)示例
本文對Android版本更新的知識做全面的總結(jié),主要包括開發(fā)中版本的設(shè)置,如何檢測本程序的版本,版本的更新判斷和顯示,新版本程序的安裝2022-04-04
Android實現(xiàn)圖片自動輪播并且支持手勢左右無限滑動
這篇文章給大家介紹android實現(xiàn)圖片自動輪播并且支持手勢左右無限滑動,代碼簡單易懂,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-10-10
Android性能優(yōu)化死鎖監(jiān)控知識點詳解
這篇文章主要為大家介紹了Android性能優(yōu)化死鎖監(jiān)控知識點詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
關(guān)于Android中drawable必知的一些規(guī)則
drawable這個東西相信大家天天都在使用,每個Android開發(fā)者都再熟悉不過了,但可能還有一些你所不知道的規(guī)則,那今天我們就來一起探究一下這些規(guī)則。2016-08-08

