How to convert Views to bitmaps?
How to convert View into Bitmap
FrameLayout view = (FrameLayout)findViewById(R.id.framelayout);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
Converting a view to Bitmap without displaying it in Android?
there is a way to do this. you have to create a Bitmap and a Canvas and call view.draw(canvas);
here is the code:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
if the view wasn't displayed before the size of it will be zero. Its possible to measure it like this:
if (v.getMeasuredHeight() <= 0) {
v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return b;
}
EDIT: according to this post, Passing WRAP_CONTENT
as value to makeMeasureSpec()
doesn't to do any good (although for some view classes it does work), and the recommended method is:
// Either this
int specWidth = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.AT_MOST);
// Or this
int specWidth = MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED);
view.measure(specWidth, specWidth);
int questionWidth = view.getMeasuredWidth();
How to convert View to Bitmap in android?
Activity Class In this Class I Convert the Layout(View) into Image.
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ImageActivity extends Activity {
LinearLayout layout=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_convert);
layout=(LinearLayout)findViewById(R.id.layout);
((ImageView)findViewById(R.id.ImageView01)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bitmap map=ConvertToBitmap(layout);
Log.v("BitmapObject", map.toString());
}
});
}
protected Bitmap ConvertToBitmap(LinearLayout layout) {
Bitmap map;
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
return map=layout.getDrawingCache();
}
}
Android Convert view to BitMap
Hi you can use this method to convert View to Bitmap
private Bitmap createBitmapFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
How to merge multiple views into one bitmap
If you want to draw multiple views to a bitmap, call draw method of the parent viewGroup which holds all the views.
How to properly work with custom Views bitmap?
To solve your problem you should store bitmaps in memory only for those of your views which are visible at the moment to save memory.
Also you should store a scaled down version of bitmap to save memory. BitmapFactory
class has special mechanism for this. Use inJustDecodeBounds
property of BitmapFactory.Options
class to load width and height of the image without memory allocation for the image itself. Then calculate the value for inSampleSize
property and use this property to load scaled down bitmap. Read this article for deeper description.
These problems (and others concerning showing images) are solved in third-party libraries: Glide, Fresco and others. These libs are widely used, so you can use them too. Also for example Glide supports extensions to modify images.
If you want to have an image view with zoom functionality there is the great library for this. It includes pinch to zoom, panning, rotation and animation support, and allows easy extension. The view even optionally uses subsampling and tiles to support very large images.
Related Topics
Android M Permissions:Confused on the Usage of Shouldshowrequestpermissionrationale() Function
Android Adb Device Offline, Can't Issue Commands
What's the Difference Between Fill_Parent and Wrap_Content
Fool-Proof Way to Handle Fragment on Orientation Change
Add New Item Count to Icon on Button - Android
Blur or Dim Background When Android Popupwindow Active
Android; Check If File Exists Without Creating a New One
Making Data Persistent in Android
Fragment Onresume() & Onpause() Is Not Called on Backstack
Showing Empty View When Listview Is Empty
Android Studio How to Package Single Aar from Multiple Library Projects
Firebase (Fcm) How to Get Token
How to Get a Working Vertical Seekbar in Android
Measuring Text Height to Be Drawn on Canvas ( Android )
What Is the Real Android Studio Gradle Version