1

I have two ImageView's in a RelativeLayout, the first one is as large as the RelativeLayout size (as background),and the second ImageView has a small image, now I want to combine these to ImageView's image into one by screenshot. not by combine two bitmap directly.

In the snippet below, I successfully take screenshot of the RelativeLayout, but I found the image quality is not as good as the first ImageView's bitmap, can anyone help me please?

view.setDrawingCacheEnabled(true);
view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap drawCache = view.getDrawingCache(true);
Bruno Bieri
  • 8,893
  • 10
  • 60
  • 83
randomcode
  • 103
  • 1
  • 8

2 Answers2

2

You can use this code

 public static Bitmap loadBitmapFromView(View v, int width, int height)
 {
   Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);                
   Canvas c = new Canvas(b);
   v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
   v.draw(c);
   return b;
 }

or use this one

Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
View v1 = getWindow().getDecorView().getRootView();//this line for get better result
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

this is what works for me.

Bruno Bieri
  • 8,893
  • 10
  • 60
  • 83
RamBabu Pudari
  • 932
  • 7
  • 18
  • thank you for reply,my problem is the screenshot image's quality is lower than the first ImageView's bitmap,how can I fix that ? – randomcode Nov 28 '14 at 08:44
  • check once http://stackoverflow.com/questions/16710277/screenshot-compression-loss-of-quality and http://stackoverflow.com/questions/7762643/android-take-screen-shot-programatically – RamBabu Pudari Dec 01 '14 at 10:52
0

Try to get bitmap using window DecorView :

View window = activity.getWindow().getDecorView()

Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(window.getWidth()*2, window.getHeight()*2, Bitmap.Config.ARGB_8888);

bitmapCanvas.setBitmap(bitmap);
bitmapCanvas.scale(2.0f, 2.0f);
window.draw(bitmapCanvas);

bitmap.compress(Bitmap.CompressFormat.PNG, 0, myOutputStream);

Ref : High resolution screen shot in Android

Community
  • 1
  • 1
Haresh Chhelana
  • 24,394
  • 5
  • 55
  • 67