Marker in MapView disappears after rotating it - android

public Drawable rotateDrawable(int angle)
{
Bitmap arrowBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.arrowscalled);
// Create blank bitmap of equal size
Bitmap canvasBitmap = arrowBitmap.copy(Bitmap.Config.ARGB_8888, true);
canvasBitmap.eraseColor(0x00000000);
// Create canvas
Canvas canvas = new Canvas(canvasBitmap);
// Create rotation matrix
Matrix rotateMatrix = new Matrix();
rotateMatrix.setRotate(angle, canvas.getWidth()/2, canvas.getHeight()/2);
// Draw bitmap onto canvas using matrix
canvas.drawBitmap(arrowBitmap, rotateMatrix, null);
return new BitmapDrawable(canvasBitmap);
}
Whenever I call this the marker dissapears instead of rotating. What am I doing wrong?

Instead of going through the hassle of creating a new Canvas and all that use Bitmap.createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter). When you create your rotation matrix, use half the width and height passed to the createBitmap function. After this create your BitmapDrawable.

Related

Translating using Matrix Android

I am trying to translate a image using matrix my code is
Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.calednder_c);
Matrix matrix = new Matrix();
matrix.setTranslate(500,300 );
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
matrix, true);
circle.setImageBitmap(rotated);
when i am calling my animate method nothing happening .while i wrote preRotate(angle); it rotate and it scale also but not translate why what i am missing
Thanks in advance
Here is the respective snippet from the createBitmap source code:
final boolean transformed = !m.rectStaysRect();
RectF deviceR = new RectF();
m.mapRect(deviceR, dstR);
neww = Math.round(deviceR.width());
newh = Math.round(deviceR.height());
bitmap = createBitmap(neww, newh, transformed ? Config.ARGB_8888 : newConfig,
transformed || source.hasAlpha());
canvas.translate(-deviceR.left, -deviceR.top);
canvas.concat(m);
paint = new Paint();
paint.setFilterBitmap(filter);
if (transformed) {
paint.setAntiAlias(true);
}
It makes the created bitmap just big enough to hold all the desired pixels from the original bitmap. This is caused by the new size and the canvas.translate call.
The net effect is that translations in the matrix are ignored.

error rotate image in android , image missing

I have code to rotate image like this
private void rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
// setup rotation degree
matrix.postRotate(degree);
// return new bitmap rotated using matrix
Bitmap ro = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
mImageView.setImageBitmap(ro);
recycleBitmap();
}
It work to rotate , but if I click rotate button continuously , my Image is not vissible , How to solve this? thanks
Try setting the postScale before the postRotate
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
What you are doing in your code is changing the martrix value each time you click on the rotate button.Changing values continuously may destroy the structure of the image.You can adjust or scale the matrix.Try this'
private void rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
matrix.postRotate(degree,px,py);//(px,py)is pivot point with reference to which the image is rotated
// return new bitmap rotated using matrix
Bitmap ro = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
mImageView.setImageBitmap(ro);
recycleBitmap();
}

Bitmap drawable (Map Overlay Item) pixelizes after rotation in android

I'm trying to rotate an overlay drawable to represent an overlay item in Android.
I use this:
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.map_pin);
Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()),
(bmpOriginal.getHeight()),
Bitmap.Config.ARGB_8888);
Matrix matrix = new Matrix();
matrix.setRotate((float) lock.getDirection(),(float) (bmpOriginal.getWidth()/2),
(float)(bmpOriginal.getHeight()/2));
Canvas tempCanvas = new Canvas(targetBitmap);
tempCanvas.drawBitmap(bmpOriginal, matrix, null);
Drawable d = new BitmapDrawable(getResources(),targetBitmap);
//overlayitem.setMarker(drawable);
mapItemizedOverlay =
new MyItemizedOverlay<MyItemizedOverlayItem>(d, mapView);
The problem is that the image quality worsens. Pixelization happens. Any solution?
This code worked perfectly with me
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.map_pin);
Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()),
(bmpOriginal.getHeight()),
Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setAntiAlias(true);
p.setDither(true);
p.setFilterBitmap(true);
Matrix matrix = new Matrix();
matrix.setRotate((float) lock.getDirection(),(float) (bmpOriginal.getWidth()/2),
(float)(bmpOriginal.getHeight()/2));
RectF rectF = new RectF(0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight());
matrix.mapRect(rectF);
targetBitmap = Bitmap.createBitmap((int)rectF.width(), (int)rectF.height(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(targetBitmap);
tempCanvas.drawBitmap(bmpOriginal, matrix, p);
You could try to apply a paint with antialiasing but if its about pixels within the bitmap the only chance is filtering and android has its limits. Depending on the bitmap you might never be able to get it really smooth, i did try a lot but never found anything that would give it a real nice finish
public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)
the filter boolean should be true, thats all that you can do for filtering afaik
void android.graphics.Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
here you can apply a paint, you can experiment with settings on this paint, but anti-alias will work on the edges of the bitmap. Shader settings might work on the bitmap as well but there are no shaders that can fix the rotation inaccuracy
Add the following:
Paint p = new Paint();
p.setAntiAlias(true);
p.setDither(true);
p.setFilterBitmap(true);
change the following:
tempCanvas.drawBitmap(bmpOriginal, matrix, null);
to:
tempCanvas.drawBitmap(bmpOriginal, matrix, p);

Rotating Bitmap Not Rotating

I have tried several hours to rotate a bitmap with no success. I have read numerous articles about this subject on this web site and it seems the prefered solution involves creating a temporary canvas. Well I did this and I still do not see a roated bitmap.
My bitmap is a 40x40 blue square and I am trying to rotate it 45 degrees. Thats not asking for much is it? When the code runs, the bitmap that appears on the screen is the non-rotated original. ( I have also tried a translate with no success as well)
Here is my code:
// Load the bitmap resource
fillBMP2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.test1);
// Copy to a mutable bitmap
mb = fillBMP2.copy(Bitmap.Config.ARGB_8888, true);
// Create canvas to draw to
Canvas offscreenCanvas = new Canvas (mb);
// Create Matrix so I can rotate it
Matrix matrix = new Matrix();
matrix.setRotate (45);
offscreenCanvas.setMatrix (matrix);
// Send the contents of the canvas into a bitmap
offscreenCanvas.setBitmap(mb);
Later in an OnDraw I do the following:
canvas.drawBitmap(mb, 200, 200, null);
Any ideas what I am doing wrong? Seems like it should work.
Thanks
Try using this
Matrix matrix = new Matrix();
matrix.setRotate(15);
canvas.drawBitmap(bmp, matrix, paint);
setRotation method takes in a float representing
the degrees of rotation.
Try this...
Matrix matrix = new Matrix();
float px = 200;
float py = 200;
matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getWidth()/2);
matrix.postRotate(45);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, paint);
You definitely want to use transformations: check out this link.
Basically it's this:
// save the canvas
ctx.save();
// move origin to center
ctx.translate(x,y);
// rotate
ctx.rotate(angle * (Math.PI / 180));
// draw image
ctx.drawImage(image, x, y, w, h, .w/2, h/2, w, h);
// restore
ctx.restore();

Drawing rotated bitmap with anti alias

I tried to paint a rotated bitmap with anti alias turned on, but it still has alias and it's not smooth, any help?
I did as following:
final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setAntiAlias(true);
canvas.rotate(-mValues[0]);
canvas.drawBitmap(compass, -compass.getWidth()/2,-compass.getHeight()/2,p);
Paint.setAntiAlias() is for text.
You want p.setFilterBitmap(true);.
In case you're rotating without a canvas (with createBitmap), set filter to true.
Example:
private static Bitmap rotateBitmap(Bitmap srcImage, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
Bitmap rotated = Bitmap.createBitmap(srcImage, 0, 0, srcImage.getWidth(), srcImage.getHeight(), matrix, true/*set true for anti-alias*/);
srcImage.recycle(); // discard original image
return rotated;
}

Categories

Resources