6

ImageView角の丸い画像を描画するために使用するサブクラスがあります。コードはこの回答に基づいており、次のとおりです。

public class ImageViewRoundedCorners extends ImageView {
    ...
    @Override
    protected void onDraw(Canvas canvas) {
        Bitmap scaledBitmap = Bitmap.createBitmap(getMeasuredWidth(),
                                                  getMeasuredHeight(),
                                                  Bitmap.Config.ARGB_8888); 
        Canvas scaledCanvas = new Canvas(scaledBitmap);
        super.onDraw(scaledCanvas); 
        drawRoundedCornerBitmap(canvas, scaledBitmap, 
                                getMeasuredWidth(), getMeasuredHeight());

        scaledBitmap.recycle();
    }

    protected void drawRoundedCornerBitmap(Canvas outputCanvas, Bitmap input, int w, int h) {
        Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        mPaint.reset();
        mPaint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);

        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawPath(mClipPath, mPaint);

        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(input, 0, 0, mPaint);

        outputCanvas.drawBitmap(output, 0, 0, null);
    }
}

このコードを使用すると、画像は適切に丸みを帯びた角で描画されます。の最初の 2 行での割り当てを避けるために、最初にに渡されたキャンバスである にdrawRoundedCornerBitmap直接描画したいと考えています。新しい実装は次のようになります。outputCanvasonDraw

protected void drawRoundedCornerBitmap(...) {
    mPaint.reset();
    mPaint.setAntiAlias(true);
    outputCanvas.drawARGB(0, 0, 0, 0);

    mPaint.setStyle(Paint.Style.FILL);
    outputCanvas.drawPath(mClipPath, mPaint);

    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    outputCanvas.drawBitmap(input, 0, 0, mPaint);
}

何らかの理由で、このコードは Porter-Duff モードを無視しているように見え、代わりに通常の (丸みを帯びていない) 角で画像を描画します。これはなぜですか?Bitmap元のコードを機能させる中間体への描画とは何ですか?

4

1 に答える 1

0

ドローアブルを作成する Romain Guy が作成してくれました。私たちはリンク ファクトリーではありませんが、彼のブログ投稿では、リンク ファクトリーについて非常に広範囲に説明されており、これを行う効率的な方法が提供されています。丸い角

本当の基本原則は、 を作成し、カスタムを描画BitmapShaderするオブジェクトにアタッチして、それをに適用するだけです。PaintDrawableDrawableImageView

ドローアブルを使用するということは、Image がキャンバスに 1 回だけペイントされることを意味します。つまり、イメージの描画は 1 回だけ行われ、ImageView はドローアブルをスケーリングするだけです。

于 2013-01-28T15:19:47.517 に答える