I want to rotate an overlay item (a drawable) according to my bearing onLocationChanged. How do I do this? I tried with the drawable, with the itemizedOverlay, and with the overlayItem, but no success.
public void bearingRotation(float boatBearing){
Bitmap bm = ((BitmapDrawable)drawableCurrPos).getBitmap();
Matrix mat = new Matrix();
mat.postRotate(boatBearing);
Bitmap bMapRotate = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), mat, true);
drawableCurrPos = new BitmapDrawable(bMapRotate);
}
I would suggest that you convert that drawable to bitmap.
Bitmap bm = ((BitmapDrawable)drawable).getBitmap();
Matrix mat = new Matrix();
mat.postRotate(90);
Bitmap bMapRotate = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), mat, true);
Convert this bMapRotate using BitmapDrawable and apply that as a pin pointer to your location.
Related
I wanted to use porterduff multiplication mode to the PNG image, but the background became Black, only the background in the PNG extension picture becomes Black, how can I fix this?
The background should be transparent when I use the multiplication mode I want.
This line of code did not work:
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I myself wrote code as below but it didn't work and didn't show any pictures.
private Bitmap MultiplyBitmap(Bitmap bitmapmultiply){
Bitmap bitmap = Bitmap.createBitmap(bitmapmultiply.getWidth(), bitmapmultiply.getHeight(), Bitmap.Config.ARGB_8888);
Canvas cnvs = new Canvas(bitmap);
Paint pnt = new Paint();
pnt.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
cnvs.drawBitmap(bitmapmultiply, 0, 0, pnt);
Bitmap multiplybitmap = Bitmap.createBitmap(bitmapmultiply.getWidth(), bitmapmultiply.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(multiplybitmap);
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmapmultiply,0,0, null);
canvas.drawBitmap(bitmap, 0, 0, paint);
return multiplybitmap;
}
How can I remove the black background using Java? Thanks
Please, refer to the that PoterDuff.Mode link from Android webstie where you can find a lot of examples.
You could try something like this (just adapt the code for your needs):
Kotlin
val bmpDrawable = bmp.toDrawable(res)
bmpDrawable = DrawableCompat.setTint(bmpDrawable, Color.BLACK)
val tintedBmp = bmpDrawable.bitmap
return jokeDao.insert(entity.toDbJoke())
Java
Drawable bmpDrawable = bmp.toDrawable(res)
DrawableCompat.setTint(bmpDrawable, Color.BLACK)
Bitmap tintedBmp = bmpDrawable.bitmap
we need to merge two images with background large image and in front of that a small bitmap and I am using this code then the result is like this what is wrong and how to solve this problem. here the image of the small image is not visible but in small image place a large image is placed
**
Java code
**
Bitmap backgroundimage = BitmapFactory.decodeResource(getApplicationContext().getResources(),
R.drawable.rec_);
BitmapDrawable mBitmapDrawable= new BitmapDrawable( overlay(bitmap,backgroundimage));
mBitmapDrawable.setGravity(CENTER);
toolbar_layout.setBackground(mBitmapDrawable);
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
I am trying to slice (crop) part of my image to another so it can be worked on separately. I have found contours and now trying to save every contour in new Mat but it is giving error
Mat crop;
Imgproc.findContours(m, contours, new Mat() ,Imgproc.RETR_EXTERNAL , Imgproc.CHAIN_APPROX_SIMPLE);
for(int i=0; i <contours.size();i++)
{
Rect rect = Imgproc.boundingRect(contours.get(i));
crop = m.submat(rect);
}
Utils.matToBitmap(crop, bm);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageBitmap(bm);
Here m is my Mat where image is saved
Error:
What I always do in this situation is to create a new mat using the constructor with a rect:
Mat cropped = new Mat(mOriginal, boudingRect);
Edit:
Your bitmap should also have the same size:
bm = Bitmap.createBitmap(crop.size().width,crop.size().height, Bitmap.Config.ARGB_8888);
I have two images that I want to merge into one. (Eg "House.png" on top of "street.png")
How do i achieve this in Android? I just want to merge the images and export them to a file.
This example Sets the image to an ImageView but i wish to export it.
This other example does not work in Android since the classes are not available.
I'd try something like:
public static Bitmap mergeImages(Bitmap bottomImage, Bitmap topImage) {
final Bitmap output = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage
.getHeight(), Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawBitmap(bottomImage, 0, 0, paint);
canvas.drawBitmap(topImage, 0, 0, paint);
return output;
}
(not tested, I just wrote it here, might be some simple errors in there)
Basically what you do is create a 3rd empty bitmap, draw the bottom image on it and then draw the top image over it.
As for saving to a file, here are a few examples: Save bitmap to location
You can do like this...............
public Bitmap Overlay(Bitmap Bitmap1, Resources paramResources, Bitmap Bitmap2, int alpha)
{
Bitmap bmp1 = Bitmap.createScaledBitmap(Bitmap2, Bitmap1.getWidth(), Bitmap1.getHeight(), true);
Bitmap bmp2 = Bitmap.createBitmap(Bitmap1.getWidth(), Bitmap1.getHeight(), Bitmap1.getConfig());
Paint localPaint = new Paint();
localPaint.setAlpha(alpha);
Canvas localCanvas = new Canvas(bmp2);
Matrix localMatrix = new Matrix();
localCanvas.drawBitmap(Bitmap1, localMatrix, null);
localCanvas.drawBitmap(bmp1, localMatrix, localPaint);
bmp1.recycle();
System.gc();
return bmp2;
}
I cant get my head around this problem I have. I have 2 images which were added to a canvas and will be treated as one object, now I need to return this canvas as bitmap/drawable, since
Here is code how I added 2 bitmaps into a canvas
Bitmap image1=BitmapFactory.decodeResource(getResources() ,R.drawable.icon1);
Bitmap image2=BitmapFactory.decodeResource(getResources() R.drawable.icon2);
Rect srcRect = new Rect(0, 0, image.getWidth(), image.getHeight());
Rect dstRect = new Rect(srcRect);
dstRect.offset(15, 0);
canvas.drawBitmap(image, srcRect, dstRect, null);
dstRect.offset(image.getWidth(), 0);
canvas.drawBitmap(image2, srcRect, dstRect, null);
//return???????????
Please someone help.
Tnx in advance!
You can create a Bitmap to draw into.
Bitmap image1=BitmapFactory.decodeResource(getResources() ,R.drawable.icon1);
Bitmap image2=BitmapFactory.decodeResource(getResources() R.drawable.icon2);
Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);//Create the canvas to your image
Rect srcRect = new Rect(0, 0, image.getWidth(), image.getHeight());
Rect dstRect = new Rect(srcRect);
dstRect.offset(15, 0);
canvas.drawBitmap(image, srcRect, dstRect, null); //draw on it
dstRect.offset(image.getWidth(), 0);
canvas.drawBitmap(image2, srcRect, dstRect, null);
return result;//result will have the drawed images from the canvas
Where did you get the canvas from? If from a bitmap, then that obj will now have whatever you drew on the canvas applied to it.
Canvas is just a way to draw onto a bitmap or drawable that it is backed by. So if you create a Bitmap called result and then get your canvas from that you can just return that.
As in..
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
...do your stuff...
return result;