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 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();
}
}
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 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();
Convert view into bitmap with modification
Issue 1: yes, you have to change visibility of those views you don't want them to be converted into image(the easiest way).
Issue 2: for adding a watermark, you can just draw it on canvas(you can use canvas.drawBitmap to do it) instead of putting it in the layout.
Convert View Rect coordinates to bitmap area
So the solution was to scale the bitmap to screens size.
Bitmap bitmap = BitmapUtil.scaleBitmap(bitmap, screenWidth, screenHeight);
Then crop by the Rect
cordinates
Bitmap cropBitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());
Related Topics
How to Detect When an Android Application Is Running in the Emulator
Error Type 3 Error: Activity Class {} Does Not Exist
Text Size and Different Android Screen Sizes
Android: How to Validate Edittext Input
How to Include *.So Library in Android Studio
Check Android Application Is in Foreground or Not
How to Handle Dynamic Json in Retrofit
Noclassdeffounderror For Code in an Java Library on Android
How to Detect Android App Uninstall
Programmatically Switching Off Android Phone
Lollipop:Draw Behind Statusbar with Its Color Set to Transparent
How to Find Out Which Keystore Was Used to Sign an App
How to Hide Soft Keyboard on Android After Clicking Outside Edittext
Android Room - Simple Select Query - Cannot Access Database on the Main Thread