|
| 1 | +package org.yh.library.view; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Bitmap; |
| 5 | +import android.graphics.BitmapShader; |
| 6 | +import android.graphics.Canvas; |
| 7 | +import android.graphics.Color; |
| 8 | +import android.graphics.Matrix; |
| 9 | +import android.graphics.Paint; |
| 10 | +import android.graphics.RectF; |
| 11 | +import android.graphics.Shader; |
| 12 | +import android.graphics.drawable.BitmapDrawable; |
| 13 | +import android.graphics.drawable.Drawable; |
| 14 | +import android.util.AttributeSet; |
| 15 | +import android.util.TypedValue; |
| 16 | +import android.widget.ImageView; |
| 17 | + |
| 18 | +/** |
| 19 | + * 作者:游浩 on 2017/12/11 10:46 |
| 20 | + * https://github.com/android-coco/YhLibraryForAndroid |
| 21 | + * 邮箱:yh_android@163.com |
| 22 | + * 实现圆形、圆角,椭圆等自定义图片View。 |
| 23 | + */ |
| 24 | +public class YHImageViewRoundOval extends ImageView |
| 25 | +{ |
| 26 | + private Paint mPaint; |
| 27 | + |
| 28 | + private int mWidth; |
| 29 | + |
| 30 | + private int mHeight; |
| 31 | + |
| 32 | + private int mRadius;//圆半径 |
| 33 | + |
| 34 | + private RectF mRect;//矩形凹行大小 |
| 35 | + |
| 36 | + private int mRoundRadius;// 圆角大小 |
| 37 | + |
| 38 | + private BitmapShader mBitmapShader;//图形渲染 |
| 39 | + |
| 40 | + private Matrix mMatrix; |
| 41 | + |
| 42 | + private int mType;// 记录是圆形还是圆角矩形 |
| 43 | + |
| 44 | + public static final int TYPE_CIRCLE = 0;// 圆形 |
| 45 | + public static final int TYPE_ROUND = 1;// 圆角矩形 |
| 46 | + public static final int TYPE_OVAL = 2;//椭圆形 |
| 47 | + public static final int DEFAUT_ROUND_RADIUS = 10;//默认圆角大小 |
| 48 | + |
| 49 | + public YHImageViewRoundOval(Context context) |
| 50 | + { |
| 51 | + this(context, null); |
| 52 | + } |
| 53 | + |
| 54 | + public YHImageViewRoundOval(Context context, AttributeSet attrs) |
| 55 | + { |
| 56 | + this(context, attrs, 0); |
| 57 | + } |
| 58 | + |
| 59 | + public YHImageViewRoundOval(Context context, AttributeSet attrs, int defStyle) |
| 60 | + { |
| 61 | + super(context, attrs, defStyle); |
| 62 | + initView(); |
| 63 | + } |
| 64 | + |
| 65 | + private void initView() |
| 66 | + { |
| 67 | + mPaint = new Paint(); |
| 68 | + mPaint.setAntiAlias(true); |
| 69 | + mMatrix = new Matrix(); |
| 70 | + mRoundRadius = DEFAUT_ROUND_RADIUS; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) |
| 75 | + { |
| 76 | + super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 77 | + // 如果是绘制圆形,则强制宽高大小一致 |
| 78 | + if (mType == TYPE_CIRCLE) |
| 79 | + { |
| 80 | + mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight()); |
| 81 | + mRadius = mWidth / 2; |
| 82 | + setMeasuredDimension(mWidth, mWidth); |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void onDraw(Canvas canvas) |
| 89 | + { |
| 90 | + |
| 91 | + if (null == getDrawable()) |
| 92 | + { |
| 93 | + return; |
| 94 | + } |
| 95 | + setBitmapShader(); |
| 96 | + if (mType == TYPE_CIRCLE) |
| 97 | + { |
| 98 | + canvas.drawCircle(mRadius, mRadius, mRadius, mPaint); |
| 99 | + } else if (mType == TYPE_ROUND) |
| 100 | + { |
| 101 | + mPaint.setColor(Color.RED); |
| 102 | + canvas.drawRoundRect(mRect, mRoundRadius, mRoundRadius, mPaint); |
| 103 | + } else if (mType == TYPE_OVAL) |
| 104 | + { |
| 105 | + canvas.drawOval(mRect, mPaint); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected void onSizeChanged(int w, int h, int oldw, int oldh) |
| 111 | + { |
| 112 | + super.onSizeChanged(w, h, oldw, oldh); |
| 113 | + mRect = new RectF(0, 0, getWidth(), getHeight()); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * 设置BitmapShader |
| 118 | + */ |
| 119 | + private void setBitmapShader() |
| 120 | + { |
| 121 | + Drawable drawable = getDrawable(); |
| 122 | + if (null == drawable) |
| 123 | + { |
| 124 | + return; |
| 125 | + } |
| 126 | + Bitmap bitmap = drawableToBitmap(drawable); |
| 127 | + // 将bitmap作为着色器来创建一个BitmapShader |
| 128 | + mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); |
| 129 | + float scale = 1.0f; |
| 130 | + if (mType == TYPE_CIRCLE) |
| 131 | + { |
| 132 | + // 拿到bitmap宽或高的小值 |
| 133 | + int bSize = Math.min(bitmap.getWidth(), bitmap.getHeight()); |
| 134 | + scale = mWidth * 1.0f / bSize; |
| 135 | + |
| 136 | + } else if (mType == TYPE_ROUND || mType == TYPE_OVAL) |
| 137 | + { |
| 138 | + // 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值; |
| 139 | + scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight()); |
| 140 | + } |
| 141 | + // shader的变换矩阵,我们这里主要用于放大或者缩小 |
| 142 | + mMatrix.setScale(scale, scale); |
| 143 | + // 设置变换矩阵 |
| 144 | + mBitmapShader.setLocalMatrix(mMatrix); |
| 145 | + mPaint.setShader(mBitmapShader); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * drawable转bitmap |
| 151 | + * |
| 152 | + * @param drawable |
| 153 | + * @return |
| 154 | + */ |
| 155 | + private Bitmap drawableToBitmap(Drawable drawable) |
| 156 | + { |
| 157 | + if (drawable instanceof BitmapDrawable) |
| 158 | + { |
| 159 | + BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; |
| 160 | + return bitmapDrawable.getBitmap(); |
| 161 | + } |
| 162 | + int w = drawable.getIntrinsicWidth(); |
| 163 | + int h = drawable.getIntrinsicHeight(); |
| 164 | + Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 165 | + Canvas canvas = new Canvas(bitmap); |
| 166 | + drawable.setBounds(0, 0, w, h); |
| 167 | + drawable.draw(canvas); |
| 168 | + return bitmap; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * 单位dp转单位px |
| 173 | + */ |
| 174 | + public int dpTodx(int dp) |
| 175 | + { |
| 176 | + |
| 177 | + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
| 178 | + dp, getResources().getDisplayMetrics()); |
| 179 | + } |
| 180 | + |
| 181 | + public int getType() |
| 182 | + { |
| 183 | + return mType; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * 设置图片类型:圆形、圆角矩形、椭圆形 |
| 188 | + * |
| 189 | + * @param mType |
| 190 | + */ |
| 191 | + public void setType(int mType) |
| 192 | + { |
| 193 | + if (this.mType != mType) |
| 194 | + { |
| 195 | + this.mType = mType; |
| 196 | + invalidate(); |
| 197 | + } |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + public int getRoundRadius() |
| 202 | + { |
| 203 | + return mRoundRadius; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * 设置圆角大小 |
| 208 | + * |
| 209 | + * @param mRoundRadius |
| 210 | + */ |
| 211 | + public void setRoundRadius(int mRoundRadius) |
| 212 | + { |
| 213 | + if (this.mRoundRadius != mRoundRadius) |
| 214 | + { |
| 215 | + this.mRoundRadius = mRoundRadius; |
| 216 | + invalidate(); |
| 217 | + } |
| 218 | + |
| 219 | + } |
| 220 | +} |
0 commit comments