实例介绍
【实例简介】
【实例截图】
【核心代码】
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | package davidzoomimageviewrounddemo.qq986945193.com.davidzoomimageviewrounddemo.view; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.NinePatchDrawable; import android.util.AttributeSet; import android.widget.ImageView; import davidzoomimageviewrounddemo.qq986945193.com.davidzoomimageviewrounddemo.R; /** * @author :程序员小冰 * @新浪微博 :http://weibo.com/mcxiaobing * @CSDN博客: http://blog.csdn.net/qq_21376985 * @交流Qq :986945193 * @GitHub:https://github.com/QQ986945193 */ public class RoundImageView extends ImageView { private int mBorderThickness = 0 ; private Context mContext; private int defaultColor = 0xFFFFFFFF ; // 如果只有其中一个有值,则只画一个圆形边框 private int mBorderOutsideColor = 0 ; // 图片的外边界 private int mBorderInsideColor = 0 ; // 图片的内边界 // 控件默认长、宽 private int defaultWidth = 0 ; private int defaultHeight = 0 ; public RoundImageView(Context context) { super (context); mContext = context; } public RoundImageView(Context context, AttributeSet attrs) { super (context, attrs); mContext = context; setCustomAttributes(attrs); } public RoundImageView(Context context, AttributeSet attrs, int defStyle) { super (context, attrs, defStyle); mContext = context; setCustomAttributes(attrs); } private void setCustomAttributes(AttributeSet attrs) { TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.RoundImageView); // 边界的宽度 mBorderThickness = a.getDimensionPixelSize( R.styleable.RoundImageView_border_thickness, 0 ); // 外边界的颜色 mBorderOutsideColor = a.getColor( R.styleable.RoundImageView_border_outside_color, defaultColor); // 内边界的颜色 mBorderInsideColor = a.getColor( R.styleable.RoundImageView_border_inside_color, defaultColor); } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null ) { return ; } if (getWidth() == 0 || getHeight() == 0 ) { return ; } this .measure( 0 , 0 ); if (drawable.getClass() == NinePatchDrawable. class ) return ; Bitmap b = ((BitmapDrawable) drawable).getBitmap(); Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true ); if (defaultWidth == 0 ) { defaultWidth = getWidth(); } if (defaultHeight == 0 ) { defaultHeight = getHeight(); } // 保证重新读取图片后不会因为图片大小而改变控件宽、高的大小(针对宽、高为wrap_content布局的imageview,但会导致margin无效) // if (defaultWidth != 0 && defaultHeight != 0) { // LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( // defaultWidth, defaultHeight); // setLayoutParams(params); // } int radius = 0 ; if (mBorderInsideColor != defaultColor && mBorderOutsideColor != defaultColor) { // 定义画两个边框,分别为外圆边框和内圆边框 radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2 - 2 * mBorderThickness; // 画内圆 drawCircleBorder(canvas, radius mBorderThickness / 2 , mBorderInsideColor); // 画外圆 drawCircleBorder(canvas, radius mBorderThickness mBorderThickness / 2 , mBorderOutsideColor); } else if (mBorderInsideColor != defaultColor && mBorderOutsideColor == defaultColor) { // 定义画一个边框 radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2 - mBorderThickness; drawCircleBorder(canvas, radius mBorderThickness / 2 , mBorderInsideColor); } else if (mBorderInsideColor == defaultColor && mBorderOutsideColor != defaultColor) { // 定义画一个边框 radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2 - mBorderThickness; drawCircleBorder(canvas, radius mBorderThickness / 2 , mBorderOutsideColor); } else { // 没有边框 radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2 ; } Bitmap roundBitmap = getCroppedRoundBitmap(bitmap, radius); canvas.drawBitmap(roundBitmap, defaultWidth / 2 - radius, defaultHeight / 2 - radius, null ); } /** * 获取裁剪后的圆形图片 * * @param radius * 半径 */ public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) { Bitmap scaledSrcBmp; int diameter = radius * 2 ; // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片 int bmpWidth = bmp.getWidth(); int bmpHeight = bmp.getHeight(); int squareWidth = 0 , squareHeight = 0 ; int x = 0 , y = 0 ; Bitmap squareBitmap; 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(), Bitmap.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(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(scaledSrcBmp, rect, rect, paint); // bitmap回收(recycle导致在布局文件XML看不到效果) // bmp.recycle(); // squareBitmap.recycle(); // scaledSrcBmp.recycle(); bmp = null ; squareBitmap = null ; scaledSrcBmp = null ; return output; } /** * 边缘画圆 * */ private void drawCircleBorder(Canvas canvas, int radius, int color) { Paint paint = new Paint(); /* 去锯齿 */ paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); paint.setColor(color); /* 设置paint的 style 为STROKE:空心 */ paint.setStyle(Paint.Style.STROKE); /* 设置paint的外框宽度 */ paint.setStrokeWidth(mBorderThickness); canvas.drawCircle(defaultWidth / 2 , defaultHeight / 2 , radius, paint); } } |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论