Android开发中经常会用到圆形图片,比如在用户头像设置,现在提供三种主要实现方式:

方案一:使用第三方图像框架 Fresco


1、添加依赖
dependencies {
      compile 'com.facebook.fresco:fresco:0.14.1'
}

2、初始化
public class MyApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}


3、修改 manifest
android:name=".MyApplication"

4、添加网络权限
 <uses-permission android:name="android.permission.INTERNET"/>


5、文件布局

需要添加命名空间

 xmlns:fresco="http://schemas.android.com/apk/res-auto"


引入

<com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/drawee_img"
        fresco:roundAsCircle="true"
        fresco:roundedCornerRadius="20dp"
        android:layout_width="80dp"
        android:layout_height="80dp" />


6、使用

    SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.drawee_img);
        Uri uri = Uri.parse("http://p7.qhimg.com/dr/200_200_/t01b2e3a907f6ecc29d.jpg");
        draweeView.setImageURI(uri);


方案二:自定义View

public class CircleImageView extends ImageView {
    private Paint mPaint; //画笔

    private int mRadius; //圆形图片的半径

    private float mScale; //图片的缩放比例

    public CircleImageView(Context context) {
        super(context);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //因为是圆形图片,所以应该让宽高保持一致
        int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
        mRadius = size / 2;
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        mPaint = new Paint();
        Bitmap bitmap = drawableToBitmap(getDrawable());

        //初始化BitmapShader,传入bitmap对象
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        //计算缩放比例
        mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());

        Matrix matrix = new Matrix();
        matrix.setScale(mScale, mScale);
        bitmapShader.setLocalMatrix(matrix);


        mPaint.setShader(bitmapShader);

        //画圆形,指定好中心点坐标、半径、画笔
        canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
    }

    //写一个drawble转BitMap的方法
    private Bitmap drawableToBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            return bd.getBitmap();
        }
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, w, h);
        drawable.draw(canvas);
        return bitmap;
    }
}


使用:

 <mgzxc.cn.module2.CircleImageView
        android:id="@+id/image1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:src="@drawable/gangtiexia"
        />


方案三:进行图片剪切


方式一
public static Bitmap createCircleImage(Bitmap source) { 
      int length = source.getWidth() < source.getHeight() ? source.getWidth() : source.getHeight(); 
      Paint paint = new Paint(); 
      paint.setAntiAlias(true); 
      Bitmap target = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888); 
      Canvas canvas = new Canvas(target); 
      canvas.drawCircle(length / 2, length / 2, length / 2, paint); 
      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
      canvas.drawBitmap(source, 0, 0, paint); 
      return target; 
 } 


方式二:
public static Bitmap createCircleImage(Bitmap source) { 
         int width = source.getWidth();
         int height = source.getHeight();
         float raduis = Math.min(width, height) * 0.5f;
         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
         //paint.setColor(Color.RED);
         //画布设置遮罩效果
         paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
         //处理图像数据
         Bitmap bitmap = Bitmap.createBitmap(width, height, source.getConfig());
         Canvas canvas = new Canvas(bitmap);
         //bitmap的显示由画笔paint来决定
          canvas.drawCircle(width * 0.5f, height *0.5f, raduis, paint);
         return bitmap;
} 


与Picasso结合使用:

 Picasso.with(mContext).load(Constant.format(headUrl)).transform(new Transformation() {
            @Override
            public Bitmap transform(Bitmap source) {
                source=createCircleImage(source);
                source.recycle();
                return bitmap;
            }

            //Picasso缓存bitmap,下次获取bitmap, 通过key
            //第一次调用用key 存入
            //第二次调用用key去取
            @Override
            public String key() {

                Log.d(TAG, "key() called");
                return "key";
            }
        }).into(meHeader);


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值