admin
2020-10-21 2a46240b7cb0c28e0215ee83e51434147fb09d55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.lcjian.library.drawable;
 
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
 
/**
 * Created With Android Studio User @47 Date 2014-07-28 Time 0:32
 */
public class CircleDrawable extends Drawable {
    public static final String TAG = "CircleDrawable";
 
    protected final Paint paint;
 
    protected final int margin;
    protected final BitmapShader bitmapShader;
    protected float radius;
    protected Bitmap oBitmap;// 原图
 
    public CircleDrawable(Bitmap bitmap) {
        this(bitmap, 0);
    }
 
    public CircleDrawable(Bitmap bitmap, int margin) {
        this.margin = margin;
        this.oBitmap = bitmap;
        bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(bitmapShader);
    }
 
    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        computeBitmapShaderSize();
        computeRadius();
 
    }
 
    @Override
    public void draw(Canvas canvas) {
        Rect bounds = getBounds();// 画一个圆圈
        canvas.drawCircle(bounds.width() / 2F, bounds.height() / 2F, radius, paint);
    }
 
    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
 
    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }
 
    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }
 
    /**
     * 计算Bitmap shader 大小
     */
    public void computeBitmapShaderSize() {
        Rect bounds = getBounds();
        if (bounds == null)
            return;
        // 选择缩放比较多的缩放,这样图片就不会有图片拉伸失衡
        Matrix matrix = new Matrix();
        float scaleX = bounds.width() / (float) oBitmap.getWidth();
        float scaleY = bounds.height() / (float) oBitmap.getHeight();
        float scale = scaleX > scaleY ? scaleX : scaleY;
        matrix.postScale(scale, scale);
        bitmapShader.setLocalMatrix(matrix);
    }
 
    /**
     * 计算半径的大小
     */
    public void computeRadius() {
        Rect bounds = getBounds();
        radius = bounds.width() < bounds.height() ? bounds.width() / 2F
                - margin : bounds.height() / 2F - margin;
    }
}