赞
踩
一、分类
开发中,我们有时候需要根据View生成图片。
本文根据不同情况的View生成图片进行了一些示例,分类如下
第一种,普通View生成图片(view已经渲染加载到界面上)
第二种,无中生有,通过java代码创建的或者inflate创建
第三种,WebView 生成图片
第四种,ScrollView 生成图片
第五种,ListView 生成图片
第六种,RecyclerView 生成图片
还是图来的直接
shot.gif
核心代码
public class SimpleUtils {
/**
* 将 Bitmap 保存到SD卡
* @param context
* @param mybitmap
* @param name
* @return
*/
public static boolean saveBitmapToSdCard(Context context, Bitmap mybitmap, String name){
boolean result = false;
//创建位图保存目录
String path = Environment.getExternalStorageDirectory() + "/1000ttt/";
File sd = new File(path);
if (!sd.exists()){
sd.mkdir();
}
File file = new File(path+name+".jpg");
FileOutputStream fileOutputStream = null;
if (!file.exists()){
try {
// 判断SD卡是否存在,并且是否具有读写权限
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
fileOutputStream = new FileOutputStream(file);
mybitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
//update gallery
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);
Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
result = true;
}
else{
Toast.makeText(context, "不能读取到SD卡", Toast.LENGTH_SHORT).show();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 手动测量摆放View
* 对于手动 inflate 或者其他方式代码生成加载的View进行测量,避免该View无尺寸
* @param v
* @param width
* @param height
*/
public static void layoutView(View v, int width, int height) {
// validate view.width and view.height
v.layout(0, 0, width, height);
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
// validate view.measurewidth and view.measureheight
v.measure(measuredWidth, measuredHeight);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 获取一个 View 的缓存视图
* (前提是这个View已经渲染完成显示在页面上)
* @param view
* @return
*/
public static Bitmap getCacheBitmapFromView(View view) {
final boolean drawingCacheEnabled = true;
view.setDrawingCacheEnabled(drawingCacheEnabled);
view.buildDrawingCache(drawingCacheEnabled);
final Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap;
if (drawingCache != null) {
bitmap = Bitmap.createBitmap(drawingCache);
view.setDrawingCacheEnabled(false);
} else {
bitmap = null;
}
return bitmap;
}
/**
* 对ScrollView进行截图
* @param scrollView
* @return
*/
public static Bitmap shotScrollView(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
return bitmap;
}
/**
* 对ListVie
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。