This is my code. I am Getting null value. i was trying to find out the solution since morning, i have created layout by using xml file then i got the bitmap of view, but i want it dynamically and I have created layout programatically see bellow code.
My view is appearing fine but still getting getDrawingCache() null bitmap
LinearLayout mainlayout = new LinearLayout(getApplicationContext());
mainlayout.setOrientation(LinearLayout.VERTICAL);
mainlayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ImageView top= new ImageView(getApplicationContext());
ImageView bot= new ImageView(getApplicationContext());
Bitmap bottomImage = BitmapFactory.decodeResource(getResources(), R.drawable.collagebot);
Bitmap topImage =BitmapFactory.decodeResource(getResources(), R.drawable.collagetop);
top.setImageBitmap(topImage);
bot.setImageBitmap(bottomImage);
LinearLayout midLayout = new LinearLayout(getApplicationContext());
midLayout.setOrientation(LinearLayout.HORIZONTAL);
midLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Bitmap midlaybg =BitmapFactory.decodeResource(getResources(), R.drawable.bg);
Drawable middrow = new BitmapDrawable(getResources(),midlaybg);
midLayout.setBackground(middrow);
ImageView right= new ImageView(getApplicationContext());
ImageView left= new ImageView(getApplicationContext());
Bitmap rightImage = BitmapFactory.decodeResource(getResources(), R.drawable.fb);
Bitmap leftImage =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Drawable rightdrow = new BitmapDrawable(getResources(),rightImage);
Drawable leftdrow = new BitmapDrawable(getResources(),leftImage);
right.setBackground(rightdrow);
left.setBackground(leftdrow);
right.setLayoutParams(new LayoutParams(150,150));
left.setLayoutParams(new LayoutParams(150,150));
left.setLayoutParams(new LinearLayout.LayoutParams(150, 150, 10));
midLayout.setPadding(50, 0, 0, 0);
midLayout.addView(right);
midLayout.addView(left);
mainlayout.addView(top);
mainlayout.addView(midLayout);
mainlayout.addView(bot);
Bitmap mainlaybg =BitmapFactory.decodeResource(getResources(), R.drawable.bg);
Drawable maindrow = new BitmapDrawable(getResources(),mainlaybg);
mainlayout.setBackground(maindrow);
mainlayout.setDrawingCacheEnabled(true);
mainlayout.buildDrawingCache();
// setContentView(mainlayout); // i dont want to setContentView
Log.e("ceche",""+mainlayout.getDrawingCache());// here i am getting null bitmap of mainlayout view
Bitmap imgb=mainlayout.getDrawingCache();
mainlayout.measure(MeasureSpec.makeMeasureSpec(
mainlayout.getLayoutParams().width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(
mainlayout.getLayoutParams().height,
MeasureSpec.EXACTLY));
mainlayout.layout(0, 0, mainlayout.getMeasuredWidth(),
mainlayout.getMeasuredHeight());
mainlayout.setDrawingCacheEnabled(true);
final Bitmap bitmap_ = Bitmap
.createBitmap(mainlayout.getDrawingCache());
mainlayout.setDrawingCacheEnabled(true);
Related
This question already has answers here:
Get bitmap from layout
(3 answers)
Closed 7 years ago.
I have one problem in converting relativelayout to Bitmap in android.
In onCreate(), I have already converted the relativelayout to Bitmap.
(The relativelayout inclue the imageview and the other view)
And then I want to convert the relavielayout to Bitmap again after I change the image of the ImageView.
But the result is same to last converted image result.
The code :
v = (RelativeLayout) findViewById(R.id.relative_layout_id);
v.setDrawingCacheEnabled(true);
v.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, view.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, share_screen.getMeasuredWidth(), share_screen.getHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(share_screen.getDrawingCache());
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
image_view.setImageBitmap(b);
Use this code.
public Bitmap getBitmap(View view)
{
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
return bm;
}
you can create bitmap of a view by this way
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;
}
image is set in imageview. I draw text successfully on image with the help of below code.
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.awais, myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
// canvas.drawCircle(60, 50, 25, paint);
canvas.drawText(String.valueOf(f), h, w, paint);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
but I want draw Buttons on image with different height and width.
If you already define your imageView inside relative layout in xml than just instantiate it by findViewById
//RelativeLayout relativeLayout =(RelativeLayout)(findViewById(.....));
otherwise dynamically create relative layout like this
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.setLayoutParams(lp);
relativeLayout.addView(imageview);
//than create button dynmaically
Button b = new Button(this);
b.setText("Button");
RelativeLayout.LayoutParams btnrl = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
btnrl.addRule(RelativeLayout.ALIGN_BOTTOM);
b.setLayoutParams(btnrl);
relativeLayout.addView(b);
**Edit:**
In case you want arbitrary width and height than set button with and height like this
int width = 0; // assign your width
int height = 0; // assign your height
RelativeLayout.LayoutParams btnrl = new RelativeLayout.LayoutParams(
width,height);
btnrl.addRule(RelativeLayout.ALIGN_BOTTOM);
b.setLayoutParams(btnrl);
how can I display a programmatically created ImageView on top of a previously created View Canvas? I tried using bringToFront(), but without success.
Code:
RelativeLayout root = findViewById(R.id.layout);
ImageView img = new ImageView(GameGuessActivity.context);
img.setImageResource(R.drawable.image);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.leftMargin = x;
params.topMargin = y;
root.addView(img, params);
img.bringToFront();
The Image is being displayed, but underneath the canvas, which I don't want.
You have to redraw your imageview on canvas:
pcanvas = new Canvas();
pcanvas.setBitmap(bitmap); // drawXY will result on that Bitmap
pcanvas.drawBitmap(bitmap, 0, 0, null);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
image.draw(pcanvas);
img.bringToFront();
img.invalidate();
img.draw(pcanvas);
View vvv=mViewFlipper.getCurrentView();
RelativeLayout rrr=(RelativeLayout)vvv;
ImageView img=(ImageView) rrr.getChildAt(0);
img.setRotation(90);
img.setDrawingCacheEnabled(true);
img.buildDrawingCache();
Bitmap bitmap =img.getDrawingCache();
img.destroyDrawingCache();
img.setDrawingCacheEnabled(false);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
rrr.removeViewAt(0);
ImageView img_new=new ImageView(ImageSlideShow.this);
img_new.setImageDrawable(bmd);
img_new.setScaleType(ScaleType.CENTER);
rrr.addView(img_new, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I have Used Above code for Image rotating in ViewFlipper.
First time it is executed but second time it return NullPointerException...Error line:
Bitmap bitmap =Bitmap.createBitmap(img.getDrawingCache());
hi use this code and hopefully this works
basically your image view is null so first inintiallise it
ImageView img= new ImageView();
img=(ImageView) rrr.getChildAt(0);
img.setRotation(90);
img.setDrawingCacheEnabled(true);
img.buildDrawingCache();
if(img.isDrawingCacheEnabled()){
Bitmap bitmap =img.getDrawingCache(true);
}img.destroyDrawingCache();
img.setDrawingCacheEnabled(false);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
bao);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
rrr.removeViewAt(0);
ImageView img_new=new ImageView(ImageSlideShow.this);
img_new.setImageDrawable(bmd);
img_new.setScaleType(ScaleType.CENTER);
rrr.addView(img_new, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I got Solution For this problem
ImageView img=(ImageView) rrr.getChildAt(0);
Matrix matrix = new Matrix();
matrix.set(img.getImageMatrix());
matrix.postRotate(90, img.getWidth() / 2, img.getHeight() / 2);
img.setImageMatrix(matrix);
above code complete working for image rotation and you must set android:scaleType="matrix" in your imageview.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting a view to Bitmap without displaying it in Android?
I am trying to convert the view into bitmap from following reference link
link text
Now the problem is how can i get the bitmap that is being converted from view only. in the example author has used relativelayout.dispatchDraw(c) but this line is giving me compile time error i.e.
The method dispatchDraw(Canvas) from
the type ViewGroup is not visible
Here is my code and i have written the following code inside onCreate function
Canvas c=null;
//Create Layout
RelativeLayout relativeView ;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeView = new RelativeLayout(this);
relativeView.setLayoutParams(lp);
//Background of Layout
Bitmap viewBgrnd = BitmapFactory.decodeResource(getResources(),R.drawable.bgblack);
relativeView.setBackgroundDrawable(new BitmapDrawable(viewBgrnd));
//associated with canvas
Bitmap returnedBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
c = new Canvas(returnedBitmap);
Paint paint = new Paint();
//Create Imageview that holds image
ImageView newImage = new ImageView(this);
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bgpink);
newImage.setImageBitmap(srcBitmap);
TextView newText = new TextView(this);
newText.setText("This is the text that its going to appear");
c.drawBitmap(viewBgrnd, 0, 0, paint);
relativeView.layout(100, 0, 256, 256);
relativeView.addView(newImage);
relativeView.addView(newText);
// here i am getting compile time error so for timing i have replaced this line
// with relativeView.draw(c);
relativeView.dispatchDraw(c);
Here the returnedBitmap should contain the image of (ImageView and TextView) but this bitmap contains only bacground bitmap of relativeView i.e. bgblack
here is my solution:
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
Enjoy :)
This works for me:
Bitmap viewCapture = null;
theViewYouWantToCapture.setDrawingCacheEnabled(true);
viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());
theViewYouWantToCapture.setDrawingCacheEnabled(false);
I believe the view has to be visible (ie. getVisiblity() == View.VISIBLE). If you are trying to capture it but hide it to the user at the same time, you could move it off screen or put something over it.