Come ruotare un drawable con anti-aliasing abilitato
Domanda
Ho bisogno di ruotare un ImageView di qualche grado. Sto facendo questo da sottoclassi ImageView e sovraccarico onDraw()
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.scale(0.92f,0.92f);
canvas.translate(14, 0);
canvas.rotate(1,0,0);
super.onDraw(canvas);
canvas.restore();
}
Il problema è che l'immagine che risultati mostra un mazzo di jaggies.
http://img.skitch.com/20100608-gmdy4sexsm1c71di9rgiktdjhu.png
Come posso antialias un ImageView che ho bisogno di ruotare al fine di eliminare scalettature? C'è un modo migliore per fare questo?
Soluzione
Se sapete che il vostro è un Drawable BitmapDrawable, è possibile utilizzare l'anti-aliasing in Paint della bitmap di fare qualcosa di simile al seguente:
/**
* Not as full featured as ImageView.onDraw(). Does not handle
* drawables other than BitmapDrawable, crop to padding, or other adjustments.
*/
@Override
protected void onDraw(Canvas canvas) {
final Drawable d = getDrawable();
if( d!=null && d instanceof BitmapDrawable && ((BitmapDrawable)d).getBitmap()!=null ) {
final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
final int paddingLeft = getPaddingLeft();
final int paddingTop = getPaddingTop();
canvas.save();
// do my rotation and other adjustments
canvas.scale(0.92f,0.92f);
canvas.rotate(1,0,0);
if( paddingLeft!=0 )
canvas.translate(paddingLeft,0);
if( paddingTop!=0 )
canvas.translate(0,paddingTop);
canvas.drawBitmap( ((BitmapDrawable)d).getBitmap(),0,0,p );
canvas.restore();
}
}
Altri suggerimenti
Set antialias e filterBitmap alla vernice che sorteggio è l'immagine bitmap. Ho avuto un problema simile e questo ha funzionato per me:
Paint bitmapPaint = new Paint();
bitmapPaint.setAntiAlias(true);
bitmapPaint.setFilterBitmap(true);
ho il seguente per farlo funzionare:
In AndroidManifest.xml
ho permesso di accelerazione hardware <application android:hardwareAccelerated="true">
.
Invece di usare un costume draw-metodo, ho cambiato lo strato-tipo del genitore-vista con accelerazione hardware con anti-aliasing abilitato e ha aggiunto le mie subviews come segue:
Paint layerPaint = new Paint();
layerPaint.setAntiAlias(true);
layerPaint.setFilterBitmap(true);
layerPaint.setDither(true);
RelativeLayout parentLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.myLayout, null);
parentLayout.setLayerType(View.LAYER_TYPE_HARDWARE, layerPaint);
parentLayout.addView(myChildView);
Devo aggiungere che il clipping non può essere disattivata quando si utilizza questo metodo.
Questa rotazione risolve solo una parte del problema - bordi frastagliati, ma l'immagine è diventata invece strano
.ifound ancora solo una soluzione: nel metodo OnDraw o la spedizione pareggio
Bitmap bmp = ((BitmapDrawable)image.getDrawable()).getBitmap();
double scale = (0.0+bmp.getWidth()+40.0)/getWidth();
if (scale > 1){
bmp = Bitmap.createScaledBitmap(bmp, getWidth()-40, (int) (bmp.getHeight()/scale), true);
}
canvas.drawColor(Color.TRANSPARENT);
canvas.save();
canvas.translate(20, yShift);
int left = borderSize;
int top = borderSize;
int right = bmp.getWidth() - borderSize;
int bottom = bmp.getHeight() - borderSize;
if(showText){
mPaint.setColor(Color.WHITE);
canvas.rotate(getAngel());
canvas.drawRect(left, top, right, bottom, mPaint);
canvas.translate(0,0);
textFrame.draw(canvas);
}else{
// rotaed bitmap
mMatrix.setRotate(getAngel());
// draw bitmap after creation of new rotated bitmap from rotated matrix
canvas.drawBitmap(Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mMatrix, true)
, 0, 0, mPaint);
}
canvas.restore();
@ soluzione Primoz990 ha funzionato per me :) Ho anche attivato dithering, quindi il mio codice finale che rimuove i bordi frastagliati di rotazione è questo:
Paint bitmapPaint = new Paint();
bitmapPaint.setAntiAlias(true);
bitmapPaint.setFilterBitmap(true);
bitmapPaint.setDither(true);