2010-06-08 46 views
8

我需要旋转一个ImageView几度。我通过继承的ImageView和重载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(); 
} 

问题这样做是导致图像显示一群锯齿的。

http://img.skitch.com/20100608-gmdy4sexsm1c71di9rgiktdjhu.png

我怎样才能消除锯齿的ImageView的,我需要以消除锯齿旋转?有一个更好的方法吗?

回答

4

如果你知道你的绘制对象是BitmapDrawable,你可以使用抗锯齿位图中的油漆做类似如下:

/** 
* 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(); 
    } 
} 
0

这种旋转只是解决一个问题的一部分 - 锯齿边缘,但图像变得更加奇怪。

ifound但只有一个解决办法:在 的OnDraw或调度抽签方法

 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();  
3

设置反锯齿和filterBitmap到绘制的位图的油漆。 我有一个类似的问题,这个工作对我来说:

Paint bitmapPaint = new Paint(); 
bitmapPaint.setAntiAlias(true); 
bitmapPaint.setFilterBitmap(true); 
0

@ Primoz990的解决方案为我:)我也能抖动,所以我最终的代码,消除了对旋转的锯齿状边缘是这样的:

Paint bitmapPaint = new Paint(); 
bitmapPaint.setAntiAlias(true); 
bitmapPaint.setFilterBitmap(true); 
bitmapPaint.setDither(true); 
1

我做了以下才能正常工作:

AndroidManifest.xml我启用硬件加速<application android:hardwareAccelerated="true">

代替使用自定义绘制法的,我改变了层型的父视图的到硬件加速抗锯齿启用并加入我的子视图如下:

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); 

我不得不添加使用此方法时无法禁用裁剪。