I am trying to make an app like HBD wish sharing app but I want to add text to the image and share it as an image file. add text in the image in Relative Layout is simple but I want to share that image and text as an image file. my app maybe it looks like a certificate generator(sololearn app for example)
Thank You
Just pass the id of the relative layout in here and it will return a bitmap of that entire view. You can then save that as a PNG file and you're good to go.
private Bitmap getBitmapFromView(View view) {
view.setDrawingCacheEnabled(true);
Bitmap returnedBitmap = view.getDrawingCache();
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;
}
Related
I'm working on Editor Application, I need to save FrameLayout in Internal Storage,
Here I have done convert Layout to bitmap code, It's Working Fine but it gives low quality image.
Here is my code and output...
private Bitmap viewToImage(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;
}
Any suggestions to produce HD image will be appreciated
This i get low quality image
This is original image that i want in output
Thanks in advance!
I want to save GIF from view or layout in android. please help.
Right now in my app, i am save image from view by following code
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
so can i save gif image from layout or any other way to save my view as GIF?
I want to use the code of this link: How to take a screenshot and share it programmatically to take screenshots of my application. But I want the picture which is produced to use it to change my background, like this Layout.setBackgroundResource(resid).
How I can do this?
Where I will find the image's path to use it to change Background?
First you need the view you want a screen shot of:
View anyView = findViewById(R.id.anyView);
Then use this method to take a screenshot:
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
like this:
Bitmap screenshot = screenShot(anyView);
Bitmaps can't be set as a background image so lets convert it to a drawable:
Drawable drawableScreenshot = new BitmapDrawable(getResources(), screenshot);
Now set it to be the View background:
anyView.setBackgroundDrawable(drawableScreenshot);
I used ScrollView in my application. It contains a LinearLayout as child and some other views likes
TextViews,Buttons etc... as sub-views. Now I want to post the content of scrollview as an image on FaceBook wall.I could not find out a suitable post.
**My question is how to post ScrollView/Layout content on facebook wall?...**
Thanks in Advance
use this function to convert your view in to bitmap image
public Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(480, 640,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;
}
then post bitmap image on facebook wall
hope help you..
I changed above code to
public Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight() , 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;
}
It works fine and got bitmap of all content.
I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.
I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.
Could I get the whole bitmap attached to a ImageView?
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
I think that's what you wanted.
If your drawble is not always an instanceof BitmapDrawable
Note: ImageView should be set before you do this.
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
Your bitmap is stored in bitmap.
Voila!
Easiest way is to set tag in ImageView.
imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap);
To get Tag from it
imageView.getTag();