9
votes

Comment faire pivoter un anti-aliasing activé

J'ai besoin de faire pivoter une vision d'image de quelques degrés. Je le fais en sous-classement ImageView et surcharge ONDRAW () CODE>

@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();
}


0 commentaires

5 Réponses :


5
votes

Si vous savez que votre tirable est un bitmapDrawable, vous pouvez utiliser anti-aliasing dans la peinture de bitmap pour faire quelque chose comme ce qui suit:

/**
 * 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 commentaires

0
votes

Cette rotation résout une seule partie des bords problématiques - mais l'image est devenue plutôt étrange.

Ifound seulement une solution encore: Dans la méthode ONDRAW ou DISPATCH DREACH P>

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


0 commentaires

5
votes

Définir des antiélielles et filtrerbitmap sur la peinture qui dessine le bitmap. J'ai eu un problème similaire et cela a fonctionné pour moi:

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


0 commentaires

0
votes

@ la solution de Primoz990 a fonctionné pour moi :) J'ai également activé le dithithithithithithithithithérant, alors mon code final qui supprime les bords déchiquetés en rotation est-ce: xxx


0 commentaires

1
votes

J'ai fait ce qui suit pour le faire fonctionner:

in androidmanifest.xml code> idraware accélération-accélération code>. P>

Au lieu d'utiliser une méthode de dessin personnalisée, j'ai changé le type de couche de la vue parent sur le matériel accéléré avec une anti-aliasing activé et ajouté mes sous-visions comme suit: P>

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


0 commentaires