I'd like to use several CustomViews in my appwidget and thus want to create a bitmap out of every CustomView.
So i tried that with
Bitmap customBitmap = customView.getDrawingCache(true);
remoteViews.setImageViewBitmap(R.id.imageView1, customBitmap );
but it doesn't work at all.
Are there any suggestions?
yes, it is drawn on a canvas
I would do something like this:
public Bitmap createCustomView(){
Bitmap bitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// draw on the canvas:
// ...
return bitmap;
}
and then set the Bitmap to the ImageView:
remoteViews.setImageViewBitmap(R.id.imageView1, createCustomView() );
Related
Bitmap bitmap = Bitmap.createBitmap(canvasLL.getawidth(), canvasLL.getHeight(), Bitmap.Config.RGB_5);
Canvas canvas = new Canvas(bitmap)
View.draw(canvas);
This how i had create a bitmap using the things draw on canvas. But i want to check that if something draw or not on the canvas. Any help will be really appreciated.
Bitmap.getGenerationId() can be used to check if a bitmap has been modified.
Bitmap bitmap = Bitmap.createBitmap(canvasLL.getWidth(), canvasLL.getHeight(), Bitmap.Config.RGB_565);
final int oldBitmapId = bitmap.getGenerationId();
Canvas canvas = new Canvas(bitmap);
View.draw(canvas);
final int newBitmapId = bitmap.getGenerationId();
if (oldBitmapId != newBitmapId) {
// a Bitmap has been changed
}
I want to take a picture of a view in Activity and share this picture.
First I use this method :
private Bitmap getShareViewShot() {
this.setDrawingCacheEnabled(true);
this.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(this.getDrawingCache(), 0, mSharePageRoot.getTop(), this.getWidth(), mSharePageRoot.getBottom());
this.setDrawingCacheEnabled(false);
return bitmap;
}
But if the view is in a ScrollView and the view has scrolled outside the screen, this method can't get a whole picture of this view.
So I change this method to below :
private Bitmap getShareViewShot() {
Bitmap bitmap = Bitmap.createBitmap(mSharePageRoot.getWidth(), mSharePageRoot.getHeight(),
Bitmap.Config.ARGB_8888);
mSharePageRoot.layout(0, 0, mSharePageRoot.getLayoutParams().width, mSharePageRoot.getLayoutParams().height);
final Canvas canvas = new Canvas(bitmap);
mSharePageRoot.draw(canvas);
return bitmap;
}
But another question comes: this view has no backgroud, so the picture I get has a black backgroud.In my code ,the Activity have a network image as the backgroud, so this means this method can't get the backgroud of the activity.
Please forgive me for my bad English.
Is there any other method ? thanks.
Yes, there is another method. There is a library by google called ASL(Android screenshot library). It's pretty simple.
https://code.google.com/archive/p/android-screenshot-library/wikis/DeveloperGuide.wiki
You can draw background by canvas, like this:
private Bitmap getShareViewShot() {
Bitmap bitmap = Bitmap.createBitmap(mSharePageRoot.getWidth(), mSharePageRoot.getHeight(),
Bitmap.Config.ARGB_8888);
mSharePageRoot.layout(0, 0, mSharePageRoot.getLayoutParams().width, mSharePageRoot.getLayoutParams().height);
final Canvas canvas = new Canvas(bitmap);
// the bitmap will have a white background
canvas.drawColor(0xffffffff);
mSharePageRoot.draw(canvas);
return bitmap;
}
I am trying to add a color filer in a drawable and then convert it in Bitmap. The problem is when convert the drawable into bitmap it loses it's color filter.I used drawable in imageview and its have the color filter but using bitmap in imageview doesn't have any color effect. Why this happen ? Thanks in advance.
Use a canvas to blit the Drawable back onto a bitmap:
Canvas canvas;
Drawable drawable = <yourDrawable created from wherever>;
Bitmap bmp = <your Bitmap which is the same width/height as the drawable>
// blit the drawable onto the bitmap using a Canvas
canvas = new Canvas(bmp);
drawable.draw(canvas);
http://developer.android.com/guide/topics/graphics/2d-graphics.html
I had the same problem and I figured it out finally!
We need to do following thing to get the bitmap with color filter applied on it.
image_view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(image_view.getDrawingCache());
I've faced the same issue and finally found a solution.
Drawable can have multiple states thus you might be drawing wrong state.
You should switch it to proper mode before drawing:
Drawable drawable = icon.loadDrawable(getContext());
if (drawable == null) {
return;
}
drawable.setState(new int[] {android.R.attr.state_enabled});
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
I am trying to create an image save and retrieve feature in android. The code I have for creating a jpg file from canvas is as below
Bitmap bitmap = Bitmap.createBitmap( view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
I am trying to read the jpg file and create image on canvas using
Bitmap bMap = BitmapFactory.decodeStream(buf);
Bitmap workingBitmap = Bitmap.createBitmap(bMap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
view.mybitmap = mutableBitmap;
view.onDraw(view.Canvas);
The code I have in onDraw is
canvas.drawBitmap(view.mybitmap, 0, 0, view.myPaint);
This draws the bitmap on canvas correctly from the stored jpg file but I am not able to draw anything on the canvas after that. Has it loaded a immutable bitmap image on canvas which I am not able to edit?
Any help will be appreciated! Thanks!
You shouldn't need to be calling the ondraw method directly like you are doing currently. For a custom view class all you should need to be calling is view.invalidate(); This will auto call the ondraw method and re-draw the canvas. You will probably need to update the logic in your ondraw method to handle the cases where you want to draw additional things on the canvas.
I'm drawing a bitmap in a canvas and I want to have the result in a new bitmap, but I still have a black screen as result.
This is my code, part of the onDraw(Canvas canvas) method:
if (bitmapTemplate == null) {
canvasBis = new Canvas();
bitmapTemplate = Bitmap.createBitmap(canvas.getWidth()+30,canvas.getHeight(),Bitmap.Config.ARGB_8888);
drawZones(canvasBis,bitmapTemplate);
}
bitmapRes = Bitmap.createBitmap(canvas.getWidth()+30,canvas.getHeight(),Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmapRes);
canvas.drawBitmap(bitmapTemplate, matrix, null);
My goal is to have a new bitmap (bitmapRes) by applying a matrix on an existing bitmap (bitmapTemplate). With this code I always have a black screen, but when I remove the line canvas.setBitmap(bitmapRes), I have a result but not in a new bitmap. Any ideas please? Maybe transparency? Thanks in advance.
drawZones draws some zones in bitmapTemplate.
You should try using the Canvas(Bitmap) constructor instead of the empty constructor. This sets the bitmap to the canvas for you.