http://blog.csdn.net/nupt123456789/article/details/24886451?utm_source=tuicool&utm_medium=referral

在使用这个自定义的控件时,需要添加属性声明,

而且在布局中使用时,引用图片时需要使用的android:src="@drawable/ic_launcher" ,而不是android:background="@drawable/ic_launcher"


如何为circleImageView动态设置src, circleImageView是自定义的圆形ImageView

setImageBitmap(Bitmap bm);
setImageDrawable(Drawable d);
setImageResource(int resId);

// 自定义控件里面,有声明好的方法,可以进行设置的!
icon.setImageResource(R.drawable.yi);


1.自定义圆形控件github地址:https://github.com/hdodenhof/CircleImageView

主要的类:

[java] view plain copy
  1. packagede.hdodenhof.circleimageview;
  2. importedu.njupt.zhb.main.R;
  3. importandroid.content.Context;
  4. importandroid.content.res.TypedArray;
  5. importandroid.graphics.Bitmap;
  6. importandroid.graphics.BitmapShader;
  7. importandroid.graphics.Canvas;
  8. importandroid.graphics.Color;
  9. importandroid.graphics.Matrix;
  10. importandroid.graphics.Paint;
  11. importandroid.graphics.RectF;
  12. importandroid.graphics.Shader;
  13. importandroid.graphics.drawable.BitmapDrawable;
  14. importandroid.graphics.drawable.ColorDrawable;
  15. importandroid.graphics.drawable.Drawable;
  16. importandroid.util.AttributeSet;
  17. importandroid.widget.ImageView;
  18. publicclassCircleImageViewextendsImageView{
  19. privatestaticfinalScaleTypeSCALE_TYPE=ScaleType.CENTER_CROP;
  20. privatestaticfinalBitmap.ConfigBITMAP_CONFIG=Bitmap.Config.ARGB_8888;
  21. privatestaticfinalintCOLORDRAWABLE_DIMENSION=1;
  22. privatestaticfinalintDEFAULT_BORDER_WIDTH=0;
  23. privatestaticfinalintDEFAULT_BORDER_COLOR=Color.BLACK;
  24. privatefinalRectFmDrawableRect=newRectF();
  25. privatefinalRectFmBorderRect=newRectF();
  26. privatefinalMatrixmShaderMatrix=newMatrix();
  27. privatefinalPaintmBitmapPaint=newPaint();
  28. privatefinalPaintmBorderPaint=newPaint();
  29. privateintmBorderColor=DEFAULT_BORDER_COLOR;
  30. privateintmBorderWidth=DEFAULT_BORDER_WIDTH;
  31. privateBitmapmBitmap;
  32. privateBitmapShadermBitmapShader;
  33. privateintmBitmapWidth;
  34. privateintmBitmapHeight;
  35. privatefloatmDrawableRadius;
  36. privatefloatmBorderRadius;
  37. privatebooleanmReady;
  38. privatebooleanmSetupPending;
  39. publicCircleImageView(Contextcontext){
  40. super(context);
  41. }
  42. publicCircleImageView(Contextcontext,AttributeSetattrs){
  43. this(context,attrs,0);
  44. }
  45. publicCircleImageView(Contextcontext,AttributeSetattrs,intdefStyle){
  46. super(context,attrs,defStyle);
  47. super.setScaleType(SCALE_TYPE);
  48. TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.CircleImageView,defStyle,0);
  49. mBorderWidth=a.getDimensionPixelSize(R.styleable.CircleImageView_border_width,DEFAULT_BORDER_WIDTH);
  50. mBorderColor=a.getColor(R.styleable.CircleImageView_border_color,DEFAULT_BORDER_COLOR);
  51. a.recycle();
  52. mReady=true;
  53. if(mSetupPending){
  54. setup();
  55. mSetupPending=false;
  56. }
  57. }
  58. @Override
  59. publicScaleTypegetScaleType(){
  60. returnSCALE_TYPE;
  61. }
  62. @Override
  63. publicvoidsetScaleType(ScaleTypescaleType){
  64. if(scaleType!=SCALE_TYPE){
  65. thrownewIllegalArgumentException(String.format("ScaleType%snotsupported.",scaleType));
  66. }
  67. }
  68. @Override
  69. protectedvoidonDraw(Canvascanvas){
  70. if(getDrawable()==null){
  71. return;
  72. }
  73. canvas.drawCircle(getWidth()/2,getHeight()/2,mDrawableRadius,mBitmapPaint);
  74. canvas.drawCircle(getWidth()/2,getHeight()/2,mBorderRadius,mBorderPaint);
  75. }
  76. @Override
  77. protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
  78. super.onSizeChanged(w,h,oldw,oldh);
  79. setup();
  80. }
  81. publicintgetBorderColor(){
  82. returnmBorderColor;
  83. }
  84. publicvoidsetBorderColor(intborderColor){
  85. if(borderColor==mBorderColor){
  86. return;
  87. }
  88. mBorderColor=borderColor;
  89. mBorderPaint.setColor(mBorderColor);
  90. invalidate();
  91. }
  92. publicintgetBorderWidth(){
  93. returnmBorderWidth;
  94. }
  95. publicvoidsetBorderWidth(intborderWidth){
  96. if(borderWidth==mBorderWidth){
  97. return;
  98. }
  99. mBorderWidth=borderWidth;
  100. setup();
  101. }
  102. @Override
  103. publicvoidsetImageBitmap(Bitmapbm){
  104. super.setImageBitmap(bm);
  105. mBitmap=bm;
  106. setup();
  107. }
  108. @Override
  109. publicvoidsetImageDrawable(Drawabledrawable){
  110. super.setImageDrawable(drawable);
  111. mBitmap=getBitmapFromDrawable(drawable);
  112. setup();
  113. }
  114. @Override
  115. publicvoidsetImageResource(intresId){
  116. super.setImageResource(resId);
  117. mBitmap=getBitmapFromDrawable(getDrawable());
  118. setup();
  119. }
  120. privateBitmapgetBitmapFromDrawable(Drawabledrawable){
  121. if(drawable==null){
  122. returnnull;
  123. }
  124. if(drawableinstanceofBitmapDrawable){
  125. return((BitmapDrawable)drawable).getBitmap();
  126. }
  127. try{
  128. Bitmapbitmap;
  129. if(drawableinstanceofColorDrawable){
  130. bitmap=Bitmap.createBitmap(COLORDRAWABLE_DIMENSION,COLORDRAWABLE_DIMENSION,BITMAP_CONFIG);
  131. }else{
  132. bitmap=Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),BITMAP_CONFIG);
  133. }
  134. Canvascanvas=newCanvas(bitmap);
  135. drawable.setBounds(0,0,canvas.getWidth(),canvas.getHeight());
  136. drawable.draw(canvas);
  137. returnbitmap;
  138. }catch(OutOfMemoryErrore){
  139. returnnull;
  140. }
  141. }
  142. privatevoidsetup(){
  143. if(!mReady){
  144. mSetupPending=true;
  145. return;
  146. }
  147. if(mBitmap==null){
  148. return;
  149. }
  150. mBitmapShader=newBitmapShader(mBitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
  151. mBitmapPaint.setAntiAlias(true);
  152. mBitmapPaint.setShader(mBitmapShader);
  153. mBorderPaint.setStyle(Paint.Style.STROKE);
  154. mBorderPaint.setAntiAlias(true);
  155. mBorderPaint.setColor(mBorderColor);
  156. mBorderPaint.setStrokeWidth(mBorderWidth);
  157. mBitmapHeight=mBitmap.getHeight();
  158. mBitmapWidth=mBitmap.getWidth();
  159. mBorderRect.set(0,0,getWidth(),getHeight());
  160. mBorderRadius=Math.min((mBorderRect.height()-mBorderWidth)/2,(mBorderRect.width()-mBorderWidth)/2);
  161. mDrawableRect.set(mBorderWidth,mBorderWidth,mBorderRect.width()-mBorderWidth,mBorderRect.height()-mBorderWidth);
  162. mDrawableRadius=Math.min(mDrawableRect.height()/2,mDrawableRect.width()/2);
  163. updateShaderMatrix();
  164. invalidate();
  165. }
  166. privatevoidupdateShaderMatrix(){
  167. floatscale;
  168. floatdx=0;
  169. floatdy=0;
  170. mShaderMatrix.set(null);
  171. if(mBitmapWidth*mDrawableRect.height()>mDrawableRect.width()*mBitmapHeight){
  172. scale=mDrawableRect.height()/(float)mBitmapHeight;
  173. dx=(mDrawableRect.width()-mBitmapWidth*scale)*0.5f;
  174. }else{
  175. scale=mDrawableRect.width()/(float)mBitmapWidth;
  176. dy=(mDrawableRect.height()-mBitmapHeight*scale)*0.5f;
  177. }
  178. mShaderMatrix.setScale(scale,scale);
  179. mShaderMatrix.postTranslate((int)(dx+0.5f)+mBorderWidth,(int)(dy+0.5f)+mBorderWidth);
  180. mBitmapShader.setLocalMatrix(mShaderMatrix);
  181. }
  182. }

自定义的属性:res/values/attrs.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="CircleImageView">
  4. <attrname="border_width"format="dimension"/>
  5. <attrname="border_color"format="color"/>
  6. </declare-styleable>
  7. </resources>

使用时的布局文件:

在引用圆形自定义View的时候,注意xml的属性设置,http://www.tuicool.com/articles/mQNFJ3注意:红色的属性;

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <RelativeLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="0dp"
  10. android:layout_weight="1"
  11. android:padding="@dimen/base_padding"
  12. android:background="@color/light">
  13. <de.hdodenhof.circleimageview.CircleImageView
  14. android:layout_width="160dp"
  15. android:layout_height="160dp"
  16. android:layout_centerInParent="true"
  17. android:src="@drawable/demo"
  18. app:border_width="2dp"
  19. app:border_color="@color/dark"/>
  20. </RelativeLayout>
  21. <RelativeLayout
  22. android:layout_width="match_parent"
  23. android:layout_height="0dp"
  24. android:layout_weight="1"
  25. android:padding="@dimen/base_padding"
  26. android:background="@color/dark">
  27. <de.hdodenhof.circleimageview.CircleImageView
  28. android:layout_width="160dp"
  29. android:layout_height="160dp"
  30. android:layout_centerInParent="true"
  31. android:src="@drawable/lena"
  32. app:border_width="2dp"
  33. app:border_color="@color/light"/>
  34. </RelativeLayout>
  35. </LinearLayout>

效果:



Demo下载:http://download.csdn.net/detail/nuptboyzhb/7284553


注意:有些开发者可能也发现了,如果我们需要一个圆形的ImageButton的话,其实,我们没有必要自己写。如果ImageButton的图标是固定不变的,我们完全可以让设计师给我设计一个圆形的图片,然后直接设置再ImageButton上就可以了。但是,请注意,我们这里的圆形ImageView是自定义控件,也即是:无论你设置的图片是什么样的,显示出来的就是圆的。比如:易信中用户头像的设置,无论用户拍什么的照片,显示出来都是一个圆的。

拓展阅读

还有一个更加强大的RoundedImageView,还支持圆角,椭圆等等。

https://github.com/vinc3m1/RoundedImageView


更多相关文章

  1. android TextView的字体颜色设置的多种方法
  2. TextView关于xml属性用法(待完善)
  3. android selector
  4. Android(安卓)控件之Gallery图片集
  5. GridView的属性
  6. Android(安卓)Studio中与网站通信
  7. 转:Android(安卓)设置EditText光标颜色及粗细
  8. Android(安卓)如何实现带滚动条的TextView,在更新文字时自动滚动
  9. android shape的使用

随机推荐

  1. android 自定义Android菜单背景的代码
  2. 浅析Android中的消息机制
  3. Android(安卓)-- 屏幕亮度
  4. android获取手机电量
  5. 如何下载Android(安卓)kernel内核源代码,
  6. 快捷方式工具类
  7. Android一个等待进度条的例子
  8. android 网络获取图片并存储
  9. android判断当前设备的支持哪些Feature
  10. Android全屏设置方法