1. ホーム
  2. Android

Android画像角丸

2022-02-17 04:51:17

Androidでは、画像の角丸を付けたり、丸みを帯びた画像を表示するなどの二次加工がよく発生する

実装されたエフェクト画像。

方法1

角丸の画像表示は、サードパーティーのフレームワーク「Glide」を使って実現しており、以下の3つの方法で書かれています。

1.1、最初の実装。

RequestOptions options = new RequestOptions().error(R.drawable.img_load_failure).bitmapTransform(new RoundedCorners(30));//image rounded to 30
Glide.with(this).load(URL) //image address
                .apply(options)
                .into(ImagView);

1.2、2回目の実装。

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_launcher_background);
requestOptions.circleCropTransform();
requestOptions.transforms( new RoundedCorners(30));
Glide.with(this).load(URL) //image address
                .apply(options)
                .into(ImagView);

1.3、3回目の実装です。

RequestOptions options = new RequestOptions().centerCrop() .transform(new RoundTransform(this,30)); 
Glide.with(this).load(URL) //image address
                .apply(options)
                .into(ImagView);
public class RoundTransform extends BitmapTransformation { 
    private static float radius = 0f; 
    public RoundTransform(Context context) { 
        this(context, 4); 
    } 
   
    public RoundTransform(Context context, int dp) { 
        super(context); 
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp; 
    } 
   
    @Override 
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 
        Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight); 
        return roundCrop(pool, bitmap); 
    } 
   
    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { 
        if (source == null) return null; 
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); 
        if (result == null) { 
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap;) 
        } 
   
        Canvas canvas = new Canvas(result); 
        Paint paint = new Paint(); 
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); 
        paint.setAntiAlias(true); 
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); 
        canvas.drawRoundRect(rectF, radius, radius, paint); 
        return result; 
    } 
   
    public String getId() { 
        return getClass().getName() + Math.round(radius); 
    } 
   
    @Override 
    public void updateDiskCacheKey(MessageDigest messageDigest) { 
   
    }
}

<ImageView
        android:id="@+id/iv"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        />

ImageView iv = findViewById(R.id.iv); 
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fengjing);
        Bitmap outBitmap = getRoundBitmapByShader(bitmap, 500,300,20, 3);
        iv.setImageBitmap(outBitmap);
public class RoundRectImageView extends ImageView{

    private Paint paint;

    public RoundRectImageView(Context context) {
        this(context,null);
    }

    public RoundRectImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        paint = new Paint();
    }

    /**
     * Draw a rounded rectangle image
     * @author caizhiming
     */
    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (null ! = drawable) {
            Bitmap bitmap = getBitmapFromDrawable(drawable);
// Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 50,0);
            final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
            final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
            paint.reset();
            canvas.drawBitmap(b, rectSrc, rectDest, paint);

        } else {
            super.onDraw(canvas);
        }
    }

    /**
     * Convert a resource image to a Bitmap
     * @param drawable
     * Resource image
     * @return bitmap
     */
    public static Bitmap getBitmapFromDrawable(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() ! = PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                Config.Bitmap.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //drawable.setBounds(-4, -4, width + 4, height + 4);
        drawable.draw(canvas);
        return bitmap;
    }

    public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float widthScale = outWidth * 1f / width;
        float heightScale = outHeight * 1f / height;

        Matrix matrix = new Matrix();
        matrix.setScale(widthScale, heightScale);
        // Create the output bitmap
        Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        //create canvas and pass in desBitmap, so that the content drawn will be on desBitmap
        Canvas canvas = new Canvas(desBitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // Create shader
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader;)
        // Configure the shader with a matrix
        bitmapShader.setLocalMatrix(matrix);
        paint.setShader(bitmapShader);
        //create a rectangular area and set aside the border
        RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
        //draw the incoming bitmap to the rounded rectangle area
        canvas.drawRoundRect(rect, radius, radius, paint);

        if (boarder > 0) {
            //paint boarder
            Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FL

方法2

ImageViewをカスタマイズする。

/**
 * Rounded borders via BitmapShader
 * @param bitmap 
 * @param outWidth The width of the output image
 * @param outHeight The height of the output image
 * @param radius Rounded corner size
 * @param boarder width of the border
 */
public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
    if (bitmap == null) {
        return null;
    }
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();
    
    float widthScale = outWidth * 1f / width;
    float heightScale = outHeight * 1f / height;

    Matrix matrix = new Matrix();
    matrix.setScale(widthScale, heightScale);
    // Create the output bitmap
    Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
    //create canvas and pass in desBitmap, so that the content drawn will be on desBitmap
    Canvas canvas = new Canvas(desBitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // Create shader
    BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader;)
    // Configure the shader with a matrix
    bitmapShader.setLocalMatrix(matrix);
    paint.setShader(bitmapShader);
    //create a rectangular area and set aside the border
    RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
    //draw the incoming bitmap to the rounded rectangle area
    canvas.drawRoundRect(rect, radius, radius, paint);

    if (boarder > 0) {
        //paint boarder
        Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        boarderPaint.setColor(Color.GREEN);
        boarderPaint.setStyle(Paint.Style.STROKE);
        boarderPaint.setStrokeWidth(boarder);
        canvas.drawRoundRect(rect, radius, radius, boarderPaint);
    }
    return desBitmap;
}

/**
 * Rounded borders via BitmapShader
 * @param bitmap 
 * @param outWidth The width of the output image
 * @param outHeight The height of the output image
 * @param boarder border size
 */
public static Bitmap getCircleBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int boarder) {
int radius;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float widthScale = outWidth * 1f / width;
float heightScale = outHeight * 1f / height;

Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
if (outHeight > outWidth) {
    radius = outWidth / 2;
} else {
    radius = outHeight / 2;
}
// Create a canvas
Canvas canvas = new Canvas(desBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader;)
Matrix matrix = new Matrix();
matrix.setScale(widthScale, heightScale);
bitmapShader.setLocalMatrix(matrix);
paint.setShader(bitmapShader);
canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, paint);
if (boarder > 0) {
    //paint boarder
    Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    boarderPaint.setColor(Color.GREEN);
    boarderPaint.setStyle(Paint.Style.STROKE);
    boarderPaint.setStrokeWidth(boarder);
    canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, boarderPaint);
}
return desBitmap;
}

public class RoundRectImageView extends ImageView{

    private Paint paint;

    public RoundRectImageView(Context context) {
        this(context,null);
    }

    public RoundRectImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        paint = new Paint();
    }

    /**
     * Draw a rounded rectangle image
     * @author caizhiming
     */
    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (null ! = drawable) {
            Bitmap bitmap = getBitmapFromDrawable(drawable);
// Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 50,0);
            final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
            final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
            paint.reset();
            canvas.drawBitmap(b, rectSrc, rectDest, paint);

        } else {
            super.onDraw(canvas);
        }
    }

    /**
     * Convert a resource image to a Bitmap
     * @param drawable
     * Resource image
     * @return bitmap
     */
    public static Bitmap getBitmapFromDrawable(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
                .getOpacity() ! = PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                Config.Bitmap.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //drawable.setBounds(-4, -4, width + 4, height + 4);
        drawable.draw(canvas);
        return bitmap;
    }

    public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float widthScale = outWidth * 1f / width;
        float heightScale = outHeight * 1f / height;

        Matrix matrix = new Matrix();
        matrix.setScale(widthScale, heightScale);
        // Create the output bitmap
        Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        //create canvas and pass in desBitmap, so that the content drawn will be on desBitmap
        Canvas canvas = new Canvas(desBitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // Create shader
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader;)
        // Configure the shader with a matrix
        bitmapShader.setLocalMatrix(matrix);
        paint.setShader(bitmapShader);
        //create a rectangular area and set aside the border
        RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
        //draw the incoming bitmap to the rounded rectangle area
        canvas.drawRoundRect(rect, radius, radius, paint);

        if (boarder > 0) {
            //paint boarder
            Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FL

方法3

画像を操作するために、このメソッドもボーダーを追加します

/**
 * Rounded borders via BitmapShader
 * @param bitmap 
 * @param outWidth The width of the output image
 * @param outHeight The height of the output image
 * @param radius Rounded corner size
 * @param boarder width of the border
 */
public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) {
    if (bitmap == null) {
        return null;
    }
    int height = bitmap.getHeight();
    int width = bitmap.getWidth();
    
    float widthScale = outWidth * 1f / width;
    float heightScale = outHeight * 1f / height;

    Matrix matrix = new Matrix();
    matrix.setScale(widthScale, heightScale);
    // Create the output bitmap
    Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
    //create canvas and pass in desBitmap, so that the content drawn will be on desBitmap
    Canvas canvas = new Canvas(desBitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // Create shader
    BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader;)
    // Configure the shader with a matrix
    bitmapShader.setLocalMatrix(matrix);
    paint.setShader(bitmapShader);
    //create a rectangular area and set aside the border
    RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);
    //draw the incoming bitmap to the rounded rectangle area
    canvas.drawRoundRect(rect, radius, radius, paint);

    if (boarder > 0) {
        //paint boarder
        Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        boarderPaint.setColor(Color.GREEN);
        boarderPaint.setStyle(Paint.Style.STROKE);
        boarderPaint.setStrokeWidth(boarder);
        canvas.drawRoundRect(rect, radius, radius, boarderPaint);
    }
    return desBitmap;
}

円やボーダーを実装する。

/**
 * Rounded borders via BitmapShader
 * @param bitmap 
 * @param outWidth The width of the output image
 * @param outHeight The height of the output image
 * @param boarder border size
 */
public static Bitmap getCircleBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int boarder) {
int radius;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float widthScale = outWidth * 1f / width;
float heightScale = outHeight * 1f / height;

Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
if (outHeight > outWidth) {
    radius = outWidth / 2;
} else {
    radius = outHeight / 2;
}
// Create a canvas
Canvas canvas = new Canvas(desBitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader;)
Matrix matrix = new Matrix();
matrix.setScale(widthScale, heightScale);
bitmapShader.setLocalMatrix(matrix);
paint.setShader(bitmapShader);
canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, paint);
if (boarder > 0) {
    //paint boarder
    Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    boarderPaint.setColor(Color.GREEN);
    boarderPaint.setStyle(Paint.Style.STROKE);
    boarderPaint.setStrokeWidth(boarder);
    canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, boarderPaint);
}
return desBitmap;
}

<ブロッククオート

ご参考になれば

フォローやいいね!をしてみませんか?あなたのちょっとした行動が私の大きな支えです