2

アプリの iPhone バージョンから何かを模倣しようとしています。私は正方形の画像を持っていて、それを白い枠で囲まれた円で表示したいと考えています。このような

ここに画像の説明を入力

これを行う方法はありますか?

4

3 に答える 3

0

画像をテクスチャとしてレンダリングする BitmapShader を持つ Paint オブジェクトを含むカスタム Drawable クラスを使用して、この効果、またはそれに非常に近い効果を実現できます。これは私が使用しているコードです (同じ手法を使用して角の丸い画像を描画する、Romain の Guy の投稿からわずかに改作されています)。

class CircularDrawable extends Drawable
{
    private float mCircleRadius;
    private final RectF mBackgroundRect = new RectF();
    private final Paint mBackgroundPaint;
    private final BitmapShader mBitmapShader;
    private final Paint mPaint;
    private final int mMargin;

    CircularDrawable(Bitmap bitmap, int margin, int backgroundColor)
    {
        mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(mBitmapShader);

        mMargin = margin;
        mBackgroundPaint = new Paint();
        mBackgroundPaint.setColor(backgroundColor);
    }

    @Override
    protected void onBoundsChange(Rect bounds)
    {
        super.onBoundsChange(bounds);
        mBackgroundRect.set(bounds);
        mCircleRadius = Math.min(bounds.width() / 2 - mMargin, bounds.height() / 2 - mMargin);
    }

    @Override
    public void draw(Canvas canvas)
    {
        canvas.drawRect(mBackgroundRect, mBackgroundPaint);
        canvas.drawCircle(mBackgroundRect.width() / 2, mBackgroundRect.height() / 2, mCircleRadius, mPaint);
    }

    @Override
    public int getOpacity()
    {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(int alpha)
    {
        mPaint.setAlpha(alpha);
        mBackgroundPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf)
    {
        mPaint.setColorFilter(cf);
        mBackgroundPaint.setColorFilter(cf);
    }       
}

描画したいビットマップがあれば、それから CircularDrawable を構築するだけです

new CircularDrawable(bitmap, margin, Color.WHITE);
于 2014-05-10T03:07:56.047 に答える
-1

カスタム ビューを作成し、必要なものをキャンバスに描画します。境界線を描画し、次に白い円を描画し、次にイメージを描画します。簡単なキャンバス呼び出しがいくつかあります。画像を円形の領域にクリップする必要がある場合は、画像の描画を行う前にクリッピング領域を設定するだけです。

于 2014-05-10T03:44:34.217 に答える