圆形ImageView

自定义控件实现圆形ImageView

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 作者:${孙淇文} on 2016/6/16 14:16
 * 邮箱:nice_sqw@163.com
 */
public class MyRoundedImageView  extends ImageView{

    //
//    //中间值用来设置用户头像的颜色
    public int BACK_DEFAULT = Color.parseColor("#95d3f5");

    //当前的用户的名字
    public String username = "";

    //设置当前的字段内容
    public void addTile(String username) {
        this.username = username;
        invalidate();
    }

    //设置字体颜色的
    public void addTile(String username, int namecolor) {
        this.username = username;
        this.BACK_DEFAULT = namecolor;
        invalidate();
    }

    public MyRoundedImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

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

    public MyRoundedImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint line = new Paint();
        line.setColor(Color.WHITE);
        line.setTextSize(getWidth() / 2);
        line.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fontMetrics = line.getFontMetrics();

        //计算文字高度
        float fontHeight = fontMetrics.bottom - fontMetrics.top;
        //计算文字baseline
        float textBaseY = getHeight() - (getHeight() - fontHeight) / 2 - fontMetrics.bottom;


        canvas.drawText("", getWidth() / 4, getHeight() / 2 + getWidth() / 4, line);

        int w = getWidth(), h = getHeight();
        //画圆圈的画笔
        Paint circle = new Paint();
        circle.setColor(BACK_DEFAULT);
        circle.setStrokeWidth(w / 2); //设置圆环的宽度
        circle.setAntiAlias(true);  //消除锯齿


        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }
        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }


        if (!"".equals(username) && null != username) {

            //画自定义的圆
            canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2, circle);

            if (username.length() > 0) {

                Rect targetRect = new Rect(50, 50, 1000, 200);

                float strwidth = line.measureText(username.substring(0, 1));
                Paint.FontMetrics fm = line.getFontMetrics();
                float hight = (float) Math.ceil(fm.descent - fm.ascent);

                if (username.substring(0, 1).matches("^[a-zA-Z]*")) {
                    if (username.length() >= 2) {
                        strwidth = line.measureText(username.substring(0, 1));
                        if (username.substring(0, 2).matches("^[a-zA-Z]*")) {
//                            strwidth = line.measureText(username.substring(0, 2));
//                            canvas.drawLine(0, (float) ((getHeight()) / 2 - (hight / 2) * 0.7), getWidth(), (float) ((getHeight()) / 2 - (hight / 2) * 0.7), line);
//                            canvas.drawLine(0, (getHeight()) / 2, getWidth(), (getHeight()) / 2, line);
//                            canvas.drawLine(0, (float) ((getHeight()) / 2 + (hight / 2) * 0.7), getWidth(), (float) ((getHeight()) / 2 + (hight / 2) * 0.7), line);
                            canvas.drawText(username.substring(0, 1), (getWidth()) / 2, (float) ((getHeight() - hight) / 2 + hight * 0.7), line);
                        } else {
                            canvas.drawText(username.substring(0, 1), getWidth() / 2, (float) ((getHeight() - hight) / 2 + hight * 0.7), line);
                        }

                    } else {
                        canvas.drawText(username.substring(0, 1), (getWidth()) / 2, (float) ((getHeight() - hight) / 2 + hight * 0.7), line);

                    }
                } else {
                    canvas.drawText(username.substring(0, 1), (getWidth()) / 2, (getHeight()) / 2 + hight / 4, line);

                }


            } else {
                canvas.drawText("", getWidth() / 4, (float) ((getHeight() / 4) * 2.8), line);

            }


        } else {
            BitmapDrawable bd = (BitmapDrawable) drawable;
            Bitmap b = bd.getBitmap();
            Bitmap bitmap = b.copy(Config.ARGB_8888, true);

            Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
            canvas.drawBitmap(roundBitmap, 0, 0, null);
            canvas.drawText("", getWidth() / 4, getHeight() / 2 + getWidth() / 4 - 20, line);
        }
    }

    public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
        Bitmap sbmp;
        if (bmp.getWidth() != radius || bmp.getHeight() != radius)
            sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
        else
            sbmp = bmp;
        Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),
                sbmp.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xffa19774;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f,
                sbmp.getWidth() / 2 + 0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(sbmp, rect, rect, paint);


        return output;
    }


    //获取文字的高度的方法
    public int getHigh(float fontSize) {
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        Paint.FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.ascent);
    }

    //获取文字的宽度的方法
    public float getWidth(String displayText) {
        Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(Color.WHITE);
// Define the string.
// Measure the width of the text string.
        float textWidth = mTextPaint.measureText(displayText);

        return textWidth;
    }
}
  • 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、付费专栏及课程。

余额充值