问题
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?
回答1:
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);
回答2:
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
回答3:
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);
来源:https://stackoverflow.com/questions/13048953/bitmap-drawable-map-overlay-item-pixelizes-after-rotation-in-android