zoukankan      html  css  js  c++  java
  • [转]android 自定义圆形imageview控件

     

     

    首先,定义定义圆形Imageview类:

    [java] view plaincopy
     
    1. import android.content.Context;  
    2.   
    3. import android.graphics.Bitmap;  
    4. import android.graphics.Bitmap.Config;  
    5. import android.graphics.Canvas;  
    6. import android.graphics.Color;  
    7. import android.graphics.Paint;  
    8. import android.graphics.PorterDuff.Mode;  
    9. import android.graphics.PorterDuffXfermode;  
    10. import android.graphics.Rect;  
    11. import android.graphics.drawable.BitmapDrawable;  
    12. import android.graphics.drawable.Drawable;  
    13. import android.util.AttributeSet;  
    14. import android.widget.ImageView;  
    15.   
    16. public class RoundImageView extends ImageView {  
    17.   
    18. public RoundImageView(Context context) {  
    19.     super(context);  
    20.     // TODO Auto-generated constructor stub  
    21. }  
    22.   
    23. public RoundImageView(Context context, AttributeSet attrs) {  
    24.     super(context, attrs);  
    25. }  
    26.   
    27. public RoundImageView(Context context, AttributeSet attrs, int defStyle) {  
    28.     super(context, attrs, defStyle);  
    29. }  
    30.   
    31. @Override  
    32. protected void onDraw(Canvas canvas) {  
    33.   
    34.     Drawable drawable = getDrawable();  
    35.   
    36.     if (drawable == null) {  
    37.         return;  
    38.     }  
    39.   
    40.     if (getWidth() == 0 || getHeight() == 0) {  
    41.         return;   
    42.     }  
    43.       
    44.     Bitmap b =  ((BitmapDrawable)drawable).getBitmap();  
    45.       
    46.     if(null == b)  
    47.     {  
    48.         return;  
    49.     }  
    50.       
    51.     Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);  
    52.   
    53.     int w = getWidth(), h = getHeight();  
    54.   
    55.   
    56.     Bitmap roundBitmap =  getCroppedBitmap(bitmap, w);  
    57.     canvas.drawBitmap(roundBitmap, 0,0, null);  
    58.   
    59. }  
    60.   
    61. public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {  
    62.     Bitmap sbmp;  
    63.     if(bmp.getWidth() != radius || bmp.getHeight() != radius)  
    64.         sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);  
    65.     else  
    66.         sbmp = bmp;  
    67.     Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),  
    68.             sbmp.getHeight(), Config.ARGB_8888);  
    69.     Canvas canvas = new Canvas(output);  
    70.   
    71.     final int color = 0xffa19774;  
    72.     final Paint paint = new Paint();  
    73.     final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());  
    74.   
    75.     paint.setAntiAlias(true);  
    76.     paint.setFilterBitmap(true);  
    77.     paint.setDither(true);  
    78.     canvas.drawARGB(0, 0, 0, 0);  
    79.     paint.setColor(Color.parseColor("#BAB399"));  
    80.     canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,  
    81.             sbmp.getWidth() / 2+0.1f, paint);  
    82.     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
    83.     canvas.drawBitmap(sbmp, rect, rect, paint);  
    84.   
    85.   
    86.             return output;  
    87. }  
    88.   
    89. }  


    然后在别的布局文件中使用该控件即可,如:

    [html] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent"  
    5.     android:background="@drawable/side_right"  
    6.     android:gravity="center"  
    7.     android:orientation="vertical" >  
    8.   
    9.     <LinearLayout  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="wrap_content"  
    12.         android:gravity="center"  
    13.         android:layout_marginTop="35dip"  
    14.         android:orientation="vertical" >  
    15.   
    16.         <<span style="color:#ff0000;">com.founder.reader.view.RoundImageView</span>  
    17.             android:id="@+id/right_login_head"  
    18.             android:layout_width="60dip"  
    19.             android:layout_height="60dip"  
    20.             android:scaleType="centerInside"  
    21.             android:src="@drawable/user" />  
  • 相关阅读:
    thymeleaf时间戳转换
    alerttemplate 时间戳转换
    jQuery.Deferred exception: a.indexOf is not a function TypeError: a.indexOf is not a function
    区分数据是对象还是字符串
    eclipse中选取一列快捷键
    图片拉伸不变型
    这里ajax需要改成同步
    idea如何整理代码格式
    20170311-起早床
    20190310-解决头屑
  • 原文地址:https://www.cnblogs.com/ZhuRenWang/p/4903008.html
Copyright © 2011-2022 走看看