圆形ImageView

step1: 获取裁剪后的圆形图片

public class CircleDrawableUtils {

/** 
     * 获取裁剪后的圆形图片 
     *  
     * @param radius 
     * 半径 
     */  
    public static Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {  
        Bitmap scaledSrcBmp;  
        Bitmap squareBitmap;     
        int diameter = radius * 2;  
  
        // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片  
        int bmpWidth = bmp.getWidth();  
        int bmpHeight = bmp.getHeight();  
        int squareWidth = 0, squareHeight = 0;  
        int x = 0, y = 0;  
       
        // 高大于宽  
        if (bmpHeight > bmpWidth) {
            squareWidth = squareHeight = bmpWidth;  
            x = 0;  
            y = (bmpHeight - bmpWidth) / 2;  
            squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,      // 截取正方形图片 
                    squareHeight);  
        } 
        // 宽大于高  
        else if (bmpHeight < bmpWidth) {
            squareWidth = squareHeight = bmpHeight;  
            x = (bmpWidth - bmpHeight) / 2;  
            y = 0;  
            squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,  
                    squareHeight);  
        } 
        else 
            squareBitmap = bmp;  
 
  
        if (squareBitmap.getWidth() != diameter  
                || squareBitmap.getHeight() != diameter) {  
            scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter,   //进行缩放
                    diameter, true);  
        } 
        else 
            scaledSrcBmp = squareBitmap;  
         
        Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),  
                scaledSrcBmp.getHeight(), Config.ARGB_8888);  
        Canvas canvas = new Canvas(output);  
  
        Paint paint = new Paint();  
        Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(),  
                scaledSrcBmp.getHeight());  
        
        //图像处理
        paint.setAntiAlias(true);  
        paint.setFilterBitmap(true);  
        paint.setDither(true);  
        canvas.drawARGB(0, 0, 0, 0);  
        
        canvas.drawCircle(scaledSrcBmp.getWidth() / 2,  
                scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2,  
                paint);  
        
        //设置圆形和方形区域相交显示效果
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
        canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);  
        bmp = null;  
        squareBitmap = null;  
        scaledSrcBmp = null;  
        return output;  
    }  
}

step2:圆形ImageView

public class CircleImageView extends ImageView {
 
    Path path;
    public PaintFlagsDrawFilter mPaintFlagsDrawFilter;// 毛边过滤
    Paint paint;
     
    public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        init();
    }
 
    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }
 
    public CircleImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }
    public void init(){
        mPaintFlagsDrawFilter = new PaintFlagsDrawFilter(0,
                Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setColor(Color.WHITE);
         
    }
     
    @Override
    protected void onDraw(Canvas cns) {
        // TODO Auto-generated method stub
        float h = getMeasuredHeight()- 3.0f;
        float w = getMeasuredWidth()- 3.0f;
        if (path == null) {
            path = new Path();
            path.addCircle(
                    w/2.0f
                    , h/2.0f
                    , (float) Math.min(w/2.0f, (h / 2.0))
                    , Path.Direction.CCW);
            path.close();
        }
        cns.drawCircle(w/2.0f, h/2.0f,  Math.min(w/2.0f, h / 2.0f) + 1.5f, paint);
         int saveCount = cns.getSaveCount();
        cns.save();
        cns.setDrawFilter(mPaintFlagsDrawFilter);
        cns.clipPath(path,Region.Op.REPLACE);
        cns.setDrawFilter(mPaintFlagsDrawFilter);
        cns.drawColor(Color.WHITE);
        super.onDraw(cns);
        cns.restoreToCount(saveCount);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.创建一个圆形ImageView类 ``` public class CircleImageView extends ImageView { private Paint borderPaint; private int borderWidth; private int borderColor; public CircleImageView(Context context, AttributeSet attrs) { super(context, attrs); borderPaint = new Paint(); borderPaint.setAntiAlias(true); borderPaint.setStyle(Paint.Style.STROKE); borderWidth = 2; borderColor = Color.WHITE; } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { super.onDraw(canvas); return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); Bitmap bitmapScaled = Bitmap.createScaledBitmap(bitmap, getWidth(), getHeight(), false); Bitmap bitmapCircle = getCircleBitmap(bitmapScaled); canvas.drawBitmap(bitmapCircle, 0, 0, null); borderPaint.setStrokeWidth(borderWidth); borderPaint.setColor(borderColor); canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - borderWidth / 2, borderPaint); } private Bitmap getCircleBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = bitmap.getWidth() / 2; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } public void setBorderWidth(int borderWidth) { this.borderWidth = borderWidth; invalidate(); } public void setBorderColor(int borderColor) { this.borderColor = borderColor; invalidate(); } } ``` 2.在布局文件中添加CircleImageView ``` <com.example.CircleImageView android:id="@+id/circleImageView" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" android:src="@drawable/arrow" app:borderColor="@color/colorAccent" app:borderWidth="4dp" /> ``` 3.在Activity中设置点击事件,用动画旋转圆形ImageView ``` public class MainActivity extends AppCompatActivity { private CircleImageView circleImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); circleImageView = findViewById(R.id.circleImageView); circleImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startRotateAnimation(); } }); } private void startRotateAnimation() { RotateAnimation rotateAnimation = new RotateAnimation(0, 360, circleImageView.getWidth() / 2, circleImageView.getHeight() / 2); rotateAnimation.setDuration(3000); rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); rotateAnimation.setFillAfter(true); circleImageView.startAnimation(rotateAnimation); } } ``` 4.效果演示 ![circle_image_view.gif](https://cdn.jsdelivr.net/gh/anhaochen/source/circle_image_view.gif)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值