View截图
/**
* 该方式原理主要是:View组件显示的内容可以通过cache机制保存为bitmap
*/
public static Bitmap createBitmapFromView(View view) {
Bitmap bitmap = null;
//开启view缓存bitmap
view.setDrawingCacheEnabled(true);
//设置view缓存Bitmap质量
view.setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);
//获取缓存的bitmap
Bitmap cache = view.getDrawingCache();
if (cache != null && !cache.isRecycled()) {
bitmap = Bitmap.createBitmap(cache);
}
//销毁view缓存bitmap
view.destroyDrawingCache();
//关闭view缓存bitmap
view.setDrawingCacheEnabled(false);
return bitmap;
}
recycleview截图
/**
* recycleview截图
* @param view
* @return
*/
public static Bitmap shotRecyclerView(RecyclerView view) {
RecyclerView.Adapter adapter = view.getAdapter();
Bitmap bigBitmap = null;
if (adapter != null) {
int size = adapter.getItemCount();
int height = 0;
Paint paint = new Paint();
int iHeight = 0;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
int density = 0;
LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
for (int i = 0; i < size; i++) {
RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
holder.itemView.measure(
View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
holder.itemView.getMeasuredHeight()+ SizeUtils.dp2px(20));
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.buildDrawingCache();
Bitmap drawingCache = holder.itemView.getDrawingCache();
if (drawingCache != null) {
bitmaCache.put(String.valueOf(i), drawingCache);
density = drawingCache.getDensity();
}
height += holder.itemView.getMeasuredHeight();
}
bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
bigBitmap.setDensity(density);
Canvas bigCanvas = new Canvas(bigBitmap);
Drawable lBackground = view.getBackground();
if (lBackground instanceof ColorDrawable) {
ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
int lColor = lColorDrawable.getColor();
bigCanvas.drawColor(lColor);
}
for (int i = 0; i < size; i++) {
Bitmap bitmap = bitmaCache.get(String.valueOf(i));
bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
iHeight += bitmap.getHeight()+ SizeUtils.dp2px(20);
bitmap.recycle();
}
}
return bigBitmap;
}
Bitmap拼接
/**
* 将bitmap集合上下拼接,纵向(多个)
* @param bgColor
* @param bitmaps
* @return
*/
public static Bitmap drawMultiV(String bgColor, ArrayList<Bitmap> bitmaps) {
if(null==bitmaps){
return null;
}
int width = bitmaps.get(0).getWidth();
int height = bitmaps.get(0).getHeight();
int density = bitmaps.get(0).getDensity();
for (int i = 1;i<bitmaps.size();i++) {
if (width < bitmaps.get(i).getWidth()) {
width = bitmaps.get(i).getWidth();
}
height = height+bitmaps.get(i).getHeight();
}
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
result.setDensity(density);
Canvas canvas = new Canvas(result);
if (bgColor!=null && !bgColor.isEmpty())
canvas.drawColor(Color.parseColor(bgColor));
Paint paint = new Paint();
paint.setDither(true);
canvas.drawBitmap(bitmaps.get(0), 0, 0, paint);
int h = 0;
for (int j = 1;j<bitmaps.size();j++) {
h = bitmaps.get(j).getHeight()+h;
canvas.drawBitmap(bitmaps.get(j), 0,h, paint);
}
return result;
}
/**
* 将bitmap集合上下拼接,横向(多个)
* @param bgColor
* @param bitmaps
* @return
*/
public static Bitmap drawMultiH(String bgColor,ArrayList<Bitmap> bitmaps) {
if(null==bitmaps){
return null;
}
int width = bitmaps.get(0).getWidth();
int height = bitmaps.get(0).getHeight();
int density = bitmaps.get(0).getDensity();
for (int i = 1;i<bitmaps.size();i++) {
if (height < bitmaps.get(i).getHeight()) {
height = bitmaps.get(i).getHeight();
}
width = width+bitmaps.get(i).getWidth();
}
Bitmap result = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
result.setDensity(density);
Canvas canvas = new Canvas(result);
if (bgColor!=null && !bgColor.isEmpty())
canvas.drawColor(Color.parseColor(bgColor));
Paint paint = new Paint();
paint.setDither(true);
canvas.drawBitmap(bitmaps.get(0), 0, 0, paint);
int w = 0;
for (int j = 1;j<bitmaps.size();j++) {
w = bitmaps.get(j).getWidth()+w;
canvas.drawBitmap(bitmaps.get(j), w,0, paint);
}
return result;
}