clear the canvas draw programmatically Android - android

well i'v problem with canvas.draw() i'm having multiple frames to overlay on the same bitmap . and i succeed to do this. but the problem is when now it comes to apply another frame using the same methods but different border , the border overlay the old ones.
void hm(){
Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.vignette2);
int width = bmp.getWidth();
int height = bmp.getHeight();
change = Bitmap.createScaledBitmap(change, width, height, false);
Canvas canvas = new Canvas(change);
Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0,null);
//canvas.drawBitmap(k, 0, 0, null);
view.setImageBitmap(change);
}
and here is the other method:
void hm1(){
Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.white);
int width = bmp.getWidth();
int height = bmp.getHeight();
change = Bitmap.createScaledBitmap(change, width, height, false);
Canvas canvas = new Canvas(change);
Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0,null);
//canvas.drawBitmap(k, 0, 0, null);
view.setImageBitmap(change);
}
now when i click on button1 the overlay applies to the view. and when i click on button2 it also applies to the view but it doesn't destroy the old border("the Overlay image").
i know that i should use different bitmap to handle every view of the imageview. but i'm using save button which will save the Bitmap change which means i want to apply the borders on this image and display it. without overlaying the old borders.
for example i'm using QuickActionand here is how i'm trying to accomplish the clicks.
if (actionId == border0){
hm();
}
if (actionId == border1 ){
hm1();
}
if (actionId == border2 ){
}
it works but as i said it overlay the old ones .
any help will be thankful.
thanks in advance.
Solved it tomorrow i will post the Answer :).

Well you create a new bitmap based on the old one with this line:
change = Bitmap.createScaledBitmap(change, width, height, false);
So make sure that you somehow reset the change bitmap with the original:
change = Bitmap.createScaledBitmap(originalBitmap, width, height, false);

Related

Save mapView as image without rendering it on screen

I'm creating free activity monitoring app similar to Strava. Using vtm-android as map library.
I would like save org.oscim.android.MapView as image, without rendering it to screen.
I found several solutions, how to save view as image, but unfortunately none of them is working when when view is not "attached" to existing component tree.
My expectation was to initialize MapView like this:
final String MAP_FILE = "somemap.map";
MapView mapView = new MapView(mContext);
final org.oscim.map.Map map = mapView.map();
MapFileTileSource tileSource = new MapFileTileSource();
String mapPath = new File(Environment.getExternalStorageDirectory(), MAP_FILE).getAbsolutePath();
if (tileSource.setMapFile(mapPath)) {
// Vector layer
VectorTileLayer tileLayer = map.setBaseMap(tileSource);
// Render theme
map.setTheme(VtmThemes.DEFAULT);
double latitude = 49.1625214;
double longitude = 20.2644075;
// Note: this map position is specific to Berlin area
map.setMapPosition(latitude, longitude, 1 << 12);
}
mapView.layout(0, 0, 200, 200);
//Get the dimensions of the view so we can re-layout the view at its current size
//and create a bitmap of the same size
int width = view.getWidth();
int height = view.getHeight();
int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
//Cause the view to re-layout
view.measure(measuredWidth, measuredHeight);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
//Create a bitmap backed Canvas to draw the view into
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas
view.draw(canvas);
ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(byteBuffer);
//save bytes from bytebuffer to file or db
But instead of map image, I get full transparent image.
To take screen shot you need render it on screen. Without rendering screenshot is not possible
mapView.getDrawingCache()
It will return you bitmap of current displaying.
Bitmap capturedScreen = Bitmap.createBitmap(mapView.getDrawingCache()); imageView.setImageBitmap(capturedScreen);
Here 'capturedScreen' will give you image of mapview.

how to getDrawingCache() with wallpaper

My application is a launcher, now I need a bitmap of current view.
Launcher's view can be retrived by getDrawingCache(), like following
Bitmap bitmap = Bitmap.createBitmap(screen_w, screen_h, Config.ARGB_8888 );
View decorview = getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
bitmap = decorview.getDrawingCache();
But the bitmap is only of launcher'view, NOT containing wallpaper.
How can I get the bitmap with wallpaper ?
OK, I had to say there is NO single API provide this function, user has to compound two bitmap, one is wallpaper part, the other is launcher's desktop view.
Generally, launcher extends wallpaper to several screens, so we have to get wallpaper's visible part.
Following is how I do:
//get wallpaper's visible part
//#param screenCount how many screens launcher has
//#param screenCurrent current screen index
Bitmap wallpaper =
((BitmapDrawable) WallpaperManager.getInstance(context).getDrawable()).getBitmap();
float step = 0;
step = (wallpaper.getWidth() - w) / (screenCount - 1);
Bitmap wallpaperCurrent =
Bitmap.createBitmap(wallpaper, (int)(screenCurrent * step), 0, w, h);
//get launcher's visible view bitmap
View decorview = context.getWindow().getDecorView();
decorview.buildDrawingCache();
Bitmap desktopBitmap = decorview.getDrawingCache();
//compound bitmaps
Bitmap compoundBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(compoundBitmap);
canvas.drawBitmap(wallpaperCurrent, 0, 0, null);
canvas.drawBitmap(desktopBitmap, 0, 0, null);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
// return compoundBitmap

Overlay an image on canvas

Is there possible to overlay an image on camera preview butt that image for example a coin should look like an original view, as I will move my camera it will remains in its place just like real objects as seen in camera.
I have done with overlaying a static image on camera preview but that's not my requirement.
please suggest.
thanks.
to overlay an image up to another image use this method.
public Bitmap overlayChange(Bitmap all, Bitmap scaledBorder) {
final int width = change.getWidth();
final int height = change.getHeight();
all = Bitmap.createScaledBitmap(change, width, height, true);
Bitmap mutableBitmap = all.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
scaledBorder = Bitmap.createScaledBitmap(border, width, height, true);
canvas.drawBitmap(scaledBorder, 0, 0, null);
return mutableBitmap;
}
and to use it simple
yourBitmap = overlayChange(yourBitmap , YourOtherBitmap);

Overlay Bitmap over another Android

hello guys i'm trying to get an image(Frame from resources) to overlay it over the original bitmap. so far i couldn't make my Bitmap goes into the frame as the frame always empty. the original bitmap is now showing inside the frame.
here is my code that i'm using to accomplish this.
Canvas canvas = new Canvas();
Bitmap border = null;
Bitmap scaledBorder = null;
border = BitmapFactory.decodeResource(getResources(), R.drawable.frame1);
int width = bmp.getWidth();
int height = bmp.getHeight();
scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0, new Paint());
view.setImageBitmap(scaledBorder);
bmp as my original Bitmap from Gallery or Camera. i can't find away to put them together. only the frame will appear but not the bmp.
thanks in advance.
thanks man i figured it out by own. using this
void hm1(){
Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.vignette2);
int width = bmp.getWidth();
int height = bmp.getHeight();
change = Bitmap.createScaledBitmap(change, width, height, false);
Canvas canvas = new Canvas(change);
Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0,null);
//canvas.drawBitmap(k, 0, 0, null);
view.setImageBitmap(change);
}
by adding this method on any click button , menu etc you can draw two bitmaps over each other.
P.S : Bitmap change is another bitmap from the original one as i don't want the user to apply the Overlay on the original method but on the changed one.
hope the answer helps someone. thanks
Bottom line, first you need to add your original image to the canvas, then the border, then place the canvas on the view. Your best bet is to do this in an onDraw() method. Something like this should work:
#Override
void onDraw (Canvas canvas)
{
canvas.drawBitmap(bmp,0,0,new Paint())
Bitmap border = null;
Bitmap scaledBorder = null;
border = BitmapFactory.decodeResource(getResources(), R.drawable.frame1);
int width = bmp.getWidth();
int height = bmp.getHeight();
scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0, new Paint());
}
Alternatively, you could call the draw() function from the view.
canvas.drawBitmap(bmp,0,0,new Paint())
Bitmap border = null;
Bitmap scaledBorder = null;
border = BitmapFactory.decodeResource(getResources(), R.drawable.frame1);
int width = bmp.getWidth();
int height = bmp.getHeight();
scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0, new Paint());
view.draw(canvas);

Retrieving the image and add caption to it

I wanted to create an app by taking images from the sd card and add caption to it. Also I wanted to move the caption as per our wish and place it somewhere as desired and save it .can you please suggest an idea for doing this. Also if we want to add a caption to the existing image from SD card then , do we need to have database for doing this. or can it be saved directly to the SD card.
Refer This Answer
You can put an EditText and Write into it and after writting you first convert it to Bitmap like
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache());
Now you can add created image bmp to your original image like this
call :
Bitmap combined = combineImages(bgBitmap,bmp);
public Bitmap combineImages(Bitmap background, Bitmap foreground) {
int width = 0, height = 0;
Bitmap cs;
width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight();
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, width, height, true);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawBitmap(foreground, matrix, null);
return cs;
}
Use canvas (Custom Views) for it..
I think this is the only way to make one view (Image in one canvas) on other view (caption in other canvas) on it. So in this you have to complete knowledge of how views draw on canvas and how to move to that views. And using canvas you can also smoothly move your views on screen.
After creating custom views you can save those views in Bitmap, now you can also combine those images to one bitmap..
(Here Customeviews are ImageViews, TextViews, EditText etc..)
All the best. :-)
USe like this.
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, width, height, true);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawText("title", x, y, paint);

Categories

Resources