RoundedImageView的使用

本文介绍了一种在Android应用中快速实现图片圆角显示的方法,通过使用开源项目RoundedImageView,开发者可以轻松地调整图片的圆角大小、边框宽度及颜色等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Android 将图片快速转换成圆角的方法

 使用开源项目  RoundedImageView

 github上面的开源项目 官方地址为:   https://github.com/vinc3m1/RoundedImageView 


效果如下:

     

 

 下面快速的集成进来

步骤分为3个

1: 去github上下载 工程

  https://github.com/vinc3m1/RoundedImageView  


2: 导入工程



3  在布局中使用它

  1. <com.makeramen.rounded.RoundedImageView  
  2.     android:id=”@+id/imageView1”  
  3.     android:layout_width=”wrap_content”  
  4.     android:layout_height=”wrap_content”  
  5.     android:layout_alignParentTop=”true”  
  6.     android:layout_alignParentBottom=”true”  
  7.     android:layout_alignParentRight=”true”  
  8.     android:layout_alignParentLeft=”true”  
  9.     android:padding=”10dip”  
  10.     android:src=”@drawable/photo1”  
  11.     android:scaleType=”center”  
  12.     makeramen:corner_radius=”30dip”  
  13.     makeramen:border_width=”2dip”  
  14.     makeramen:border_color=”#333333” />  
    <com.makeramen.rounded.RoundedImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:padding="10dip"
        android:src="@drawable/photo1"
        android:scaleType="center"
        makeramen:corner_radius="30dip"
        makeramen:border_width="2dip"
        makeramen:border_color="#333333" />





属性的意义:

   makeramen:border_width=”2dip”   表示图片的边框宽度为2个dp

 makeramen:  corner_radius表示为  图片转圆角的弧度

     修改    makeramen:corner_radius=”100dip”  

        当  corner_radius 设置为100dp 的时候   会呈现为圆形 .   

       (    注:  com.makeramen.rounded.RoundedImageView  控件的宽和高需要相等   )


修改了example中的    rounded_item.xml


  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <!–  
  3.     Copyright (C) 2013 Make Ramen, LLC 
  4. –>  
  5. <RelativeLayout  
  6.     xmlns:android=“http://schemas.android.com/apk/res/android”  
  7.     xmlns:makeramen=“http://schemas.android.com/apk/res/com.makeramen.rounded.example”  
  8.     android:layout_width=“match_parent”  
  9.     android:layout_height=“200dip”>  
  10.       
  11.     <com.makeramen.rounded.RoundedImageView  
  12.         android:id=“@+id/imageView1”  
  13.         android:layout_width=“200dp”  
  14.         android:layout_height=“200dp”  
  15.         android:padding=“10dip”  
  16.         android:src=“@drawable/photo1”  
  17.         android:scaleType=“center”  
  18.         makeramen:corner_radius=“100dp”  
  19.         makeramen:border_width=“2dip”  
  20.         makeramen:border_color=“#333333” />  
  21.     <TextView  
  22.         android:id=“@+id/textView3”  
  23.         android:layout_width=“wrap_content”  
  24.         android:layout_height=“wrap_content”  
  25.         android:layout_marginBottom=“36dp”  
  26.         android:layout_marginLeft=“36dp”  
  27.         android:layout_alignBottom=“@+id/imageView1”  
  28.         android:layout_alignLeft=“@+id/imageView1”  
  29.         android:background=“#7f000000”  
  30.         android:paddingLeft=“8dp”  
  31.         android:paddingRight=“8dp”  
  32.         android:textAppearance=“?android:attr/textAppearanceSmallInverse” />  
  33.   
  34.     <TextView  
  35.         android:id=“@+id/textView2”  
  36.         android:layout_width=“wrap_content”  
  37.         android:layout_height=“wrap_content”  
  38.         android:layout_above=“@+id/textView3”  
  39.         android:layout_alignLeft=“@+id/textView3”  
  40.         android:layout_marginBottom=“4dp”  
  41.         android:background=“#7f000000”  
  42.         android:paddingLeft=“8dp”  
  43.         android:paddingRight=“8dp”  
  44.         android:textAppearance=“?android:attr/textAppearanceMediumInverse” />  
  45.   
  46.     <TextView  
  47.         android:id=“@+id/textView1”  
  48.         android:layout_width=“wrap_content”  
  49.         android:layout_height=“wrap_content”  
  50.         android:layout_above=“@+id/textView2”  
  51.         android:layout_alignLeft=“@+id/textView2”  
  52.         android:layout_marginBottom=“4dp”  
  53.         android:background=“#7f000000”  
  54.         android:paddingLeft=“8dp”  
  55.         android:paddingRight=“8dp”  
  56.         android:textAppearance=“?android:attr/textAppearanceLargeInverse” />  
  57.   
  58. </RelativeLayout>  
<?xml version="1.0" encoding="utf-8"?>
<!-- 
    Copyright (C) 2013 Make Ramen, LLC
-->
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:makeramen="http://schemas.android.com/apk/res/com.makeramen.rounded.example"
    android:layout_width="match_parent"
    android:layout_height="200dip">

    <com.makeramen.rounded.RoundedImageView
        android:id="@+id/imageView1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:padding="10dip"
        android:src="@drawable/photo1"
        android:scaleType="center"
        makeramen:corner_radius="100dp"
        makeramen:border_width="2dip"
        makeramen:border_color="#333333" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="36dp"
        android:layout_marginLeft="36dp"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/imageView1"
        android:background="#7f000000"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:textAppearance="?android:attr/textAppearanceSmallInverse" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_alignLeft="@+id/textView3"
        android:layout_marginBottom="4dp"
        android:background="#7f000000"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:textAppearance="?android:attr/textAppearanceMediumInverse" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/textView2"
        android:layout_marginBottom="4dp"
        android:background="#7f000000"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:textAppearance="?android:attr/textAppearanceLargeInverse" />

</RelativeLayout>

如下图:



用代码创建 :

  1. RoundedImageView iv = new RoundedImageView(context);  
  2. iv.setScaleType(ScaleType.CENTER_CROP);  
  3. iv.setCornerRadius(10);  
  4. iv.setBorderWidth(2);  
  5. iv.setBorderColor(Color.DKGRAY);  
  6. iv.setRoundedBackground(true);  
  7. iv.setImageDrawable(drawable);  
  8. iv.setBackground(backgroundDrawable);  
  9. iv.setOval(true);  
RoundedImageView iv = new RoundedImageView(context);
iv.setScaleType(ScaleType.CENTER_CROP);
iv.setCornerRadius(10);
iv.setBorderWidth(2);
iv.setBorderColor(Color.DKGRAY);
iv.setRoundedBackground(true);
iv.setImageDrawable(drawable);
iv.setBackground(backgroundDrawable);
iv.setOval(true);


贴上部分源码:

  1. package com.makeramen;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.ColorStateList;  
  5. import android.content.res.Resources;  
  6. import android.content.res.TypedArray;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.drawable.Drawable;  
  9. import android.graphics.drawable.LayerDrawable;  
  10. import android.net.Uri;  
  11. import android.util.AttributeSet;  
  12. import android.util.Log;  
  13. import android.widget.ImageView;  
  14.   
  15. @SuppressWarnings(“UnusedDeclaration”)  
  16. public class RoundedImageView extends ImageView {  
  17.   
  18.   public static final String TAG = “RoundedImageView”;  
  19.   public static final float DEFAULT_RADIUS = 0f;  
  20.   public static final float DEFAULT_BORDER_WIDTH = 0f;  
  21.   private static final ScaleType[] SCALE_TYPES = {  
  22.       ScaleType.MATRIX,  
  23.       ScaleType.FIT_XY,  
  24.       ScaleType.FIT_START,  
  25.       ScaleType.FIT_CENTER,  
  26.       ScaleType.FIT_END,  
  27.       ScaleType.CENTER,  
  28.       ScaleType.CENTER_CROP,  
  29.       ScaleType.CENTER_INSIDE  
  30.   };  
  31.   
  32.   private float cornerRadius = DEFAULT_RADIUS;  
  33.   private float borderWidth = DEFAULT_BORDER_WIDTH;  
  34.   private ColorStateList borderColor =  
  35.       ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);  
  36.   private boolean isOval = false;  
  37.   private boolean mutateBackground = false;  
  38.   
  39.   private int mResource;  
  40.   private Drawable mDrawable;  
  41.   private Drawable mBackgroundDrawable;  
  42.   
  43.   private ScaleType mScaleType;  
  44.   
  45.   public RoundedImageView(Context context) {  
  46.     super(context);  
  47.   }  
  48.   
  49.   public RoundedImageView(Context context, AttributeSet attrs) {  
  50.     this(context, attrs, 0);  
  51.   }  
  52.   
  53.   public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {  
  54.     super(context, attrs, defStyle);  
  55.   
  56.     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyle, 0);  
  57.   
  58.     int index = a.getInt(R.styleable.RoundedImageView_android_scaleType, -1);  
  59.     if (index >= 0) {  
  60.       setScaleType(SCALE_TYPES[index]);  
  61.     } else {  
  62.       // default scaletype to FIT_CENTER  
  63.       setScaleType(ScaleType.FIT_CENTER);  
  64.     }  
  65.   
  66.     cornerRadius = a.getDimensionPixelSize(R.styleable.RoundedImageView_corner_radius, -1);  
  67.     borderWidth = a.getDimensionPixelSize(R.styleable.RoundedImageView_border_width, -1);  
  68.   
  69.     // don’t allow negative values for radius and border  
  70.     if (cornerRadius < 0) {  
  71.       cornerRadius = DEFAULT_RADIUS;  
  72.     }  
  73.     if (borderWidth < 0) {  
  74.       borderWidth = DEFAULT_BORDER_WIDTH;  
  75.     }  
  76.   
  77.     borderColor = a.getColorStateList(R.styleable.RoundedImageView_border_color);  
  78.     if (borderColor == null) {  
  79.       borderColor = ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);  
  80.     }  
  81.   
  82.     mutateBackground = a.getBoolean(R.styleable.RoundedImageView_mutate_background, false);  
  83.     isOval = a.getBoolean(R.styleable.RoundedImageView_oval, false);  
  84.   
  85.     updateDrawableAttrs();  
  86.     updateBackgroundDrawableAttrs(true);  
  87.   
  88.     a.recycle();  
  89.   }  
  90.   
  91.   @Override  
  92.   protected void drawableStateChanged() {  
  93.     super.drawableStateChanged();  
  94.     invalidate();  
  95.   }  
  96.   
  97.   /** 
  98.    * Return the current scale type in use by this ImageView. 
  99.    * 
  100.    * @attr ref android.R.styleable#ImageView_scaleType 
  101.    * @see android.widget.ImageView.ScaleType 
  102.    */  
  103.   @Override  
  104.   public ScaleType getScaleType() {  
  105.     return mScaleType;  
  106.   }  
  107.   
  108.   /** 
  109.    * Controls how the image should be resized or moved to match the size 
  110.    * of this ImageView. 
  111.    * 
  112.    * @param scaleType The desired scaling mode. 
  113.    * @attr ref android.R.styleable#ImageView_scaleType 
  114.    */  
  115.   @Override  
  116.   public void setScaleType(ScaleType scaleType) {  
  117.     assert scaleType != null;  
  118.   
  119.     if (mScaleType != scaleType) {  
  120.       mScaleType = scaleType;  
  121.   
  122.       switch (scaleType) {  
  123.         case CENTER:  
  124.         case CENTER_CROP:  
  125.         case CENTER_INSIDE:  
  126.         case FIT_CENTER:  
  127.         case FIT_START:  
  128.         case FIT_END:  
  129.         case FIT_XY:  
  130.           super.setScaleType(ScaleType.FIT_XY);  
  131.           break;  
  132.         default:  
  133.           super.setScaleType(scaleType);  
  134.           break;  
  135.       }  
  136.   
  137.       updateDrawableAttrs();  
  138.       updateBackgroundDrawableAttrs(false);  
  139.       invalidate();  
  140.     }  
  141.   }  
  142.   
  143.   @Override  
  144.   public void setImageDrawable(Drawable drawable) {  
  145.     mResource = 0;  
  146.     mDrawable = RoundedDrawable.fromDrawable(drawable);  
  147.     updateDrawableAttrs();  
  148.     super.setImageDrawable(mDrawable);  
  149.   }  
  150.   
  151.   @Override  
  152.   public void setImageBitmap(Bitmap bm) {  
  153.     mResource = 0;  
  154.     mDrawable = RoundedDrawable.fromBitmap(bm);  
  155.     updateDrawableAttrs();  
  156.     super.setImageDrawable(mDrawable);  
  157.   }  
  158.   
  159.   @Override  
  160.   public void setImageResource(int resId) {  
  161.     if (mResource != resId) {  
  162.       mResource = resId;  
  163.       mDrawable = resolveResource();  
  164.       updateDrawableAttrs();  
  165.       super.setImageDrawable(mDrawable);  
  166.     }  
  167.   }  
  168.   
  169.   @Override public void setImageURI(Uri uri) {  
  170.     super.setImageURI(uri);  
  171.     setImageDrawable(getDrawable());  
  172.   }  
  173.   
  174.   private Drawable resolveResource() {  
  175.     Resources rsrc = getResources();  
  176.     if (rsrc == null) { return null; }  
  177.   
  178.     Drawable d = null;  
  179.   
  180.     if (mResource != 0) {  
  181.       try {  
  182.         d = rsrc.getDrawable(mResource);  
  183.       } catch (Exception e) {  
  184.         Log.w(TAG, ”Unable to find resource: ” + mResource, e);  
  185.         // Don’t try again.  
  186.         mResource = 0;  
  187.       }  
  188.     }  
  189.     return RoundedDrawable.fromDrawable(d);  
  190.   }  
  191.   
  192.   @Override  
  193.   public void setBackground(Drawable background) {  
  194.     setBackgroundDrawable(background);  
  195.   }  
  196.   
  197.   private void updateDrawableAttrs() {  
  198.     updateAttrs(mDrawable);  
  199.   }  
  200.   
  201.   private void updateBackgroundDrawableAttrs(boolean convert) {  
  202.     if (mutateBackground) {  
  203.       if (convert) {  
  204.         mBackgroundDrawable = RoundedDrawable.fromDrawable(mBackgroundDrawable);  
  205.       }  
  206.       updateAttrs(mBackgroundDrawable);  
  207.     }  
  208.   }  
  209.   
  210.   private void updateAttrs(Drawable drawable) {  
  211.     if (drawable == null) { return; }  
  212.   
  213.     if (drawable instanceof RoundedDrawable) {  
  214.       ((RoundedDrawable) drawable)  
  215.           .setScaleType(mScaleType)  
  216.           .setCornerRadius(cornerRadius)  
  217.           .setBorderWidth(borderWidth)  
  218.           .setBorderColor(borderColor)  
  219.           .setOval(isOval);  
  220.     } else if (drawable instanceof LayerDrawable) {  
  221.       // loop through layers to and set drawable attrs  
  222.       LayerDrawable ld = ((LayerDrawable) drawable);  
  223.       for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {  
  224.         updateAttrs(ld.getDrawable(i));  
  225.       }  
  226.     }  
  227.   }  
  228.   
  229.   @Override  
  230.   @Deprecated  
  231.   public void setBackgroundDrawable(Drawable background) {  
  232.     mBackgroundDrawable = background;  
  233.     updateBackgroundDrawableAttrs(true);  
  234.     super.setBackgroundDrawable(mBackgroundDrawable);  
  235.   }  
  236.   
  237.   public float getCornerRadius() {  
  238.     return cornerRadius;  
  239.   }  
  240.   
  241.   public void setCornerRadius(int resId) {  
  242.     setCornerRadius(getResources().getDimension(resId));  
  243.   }  
  244.   
  245.   public void setCornerRadius(float radius) {  
  246.     if (cornerRadius == radius) { return; }  
  247.   
  248.     cornerRadius = radius;  
  249.     updateDrawableAttrs();  
  250.     updateBackgroundDrawableAttrs(false);  
  251.   }  
  252.   
  253.   public float getBorderWidth() {  
  254.     return borderWidth;  
  255.   }  
  256.   
  257.   public void setBorderWidth(int resId) {  
  258.     setBorderWidth(getResources().getDimension(resId));  
  259.   }  
  260.   
  261.   public void setBorderWidth(float width) {  
  262.     if (borderWidth == width) { return; }  
  263.   
  264.     borderWidth = width;  
  265.     updateDrawableAttrs();  
  266.     updateBackgroundDrawableAttrs(false);  
  267.     invalidate();  
  268.   }  
  269.   
  270.   public int getBorderColor() {  
  271.     return borderColor.getDefaultColor();  
  272.   }  
  273.   
  274.   public void setBorderColor(int color) {  
  275.     setBorderColor(ColorStateList.valueOf(color));  
  276.   }  
  277.   
  278.   public ColorStateList getBorderColors() {  
  279.     return borderColor;  
  280.   }  
  281.   
  282.   public void setBorderColor(ColorStateList colors) {  
  283.     if (borderColor.equals(colors)) { return; }  
  284.   
  285.     borderColor =  
  286.         (colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);  
  287.     updateDrawableAttrs();  
  288.     updateBackgroundDrawableAttrs(false);  
  289.     if (borderWidth > 0) {  
  290.       invalidate();  
  291.     }  
  292.   }  
  293.   
  294.   public boolean isOval() {  
  295.     return isOval;  
  296.   }  
  297.   
  298.   public void setOval(boolean oval) {  
  299.     isOval = oval;  
  300.     updateDrawableAttrs();  
  301.     updateBackgroundDrawableAttrs(false);  
  302.     invalidate();  
  303.   }  
  304.   
  305.   public boolean isMutateBackground() {  
  306.     return mutateBackground;  
  307.   }  
  308.   
  309.   public void setMutateBackground(boolean mutate) {  
  310.     if (mutateBackground == mutate) { return; }  
  311.   
  312.     mutateBackground = mutate;  
  313.     updateBackgroundDrawableAttrs(true);  
  314.     invalidate();  
  315.   }  
  316. }  
package com.makeramen;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

@SuppressWarnings("UnusedDeclaration")
public class RoundedImageView extends ImageView {

  public static final String TAG = "RoundedImageView";
  public static final float DEFAULT_RADIUS = 0f;
  public static final float DEFAULT_BORDER_WIDTH = 0f;
  private static final ScaleType[] SCALE_TYPES = {
      ScaleType.MATRIX,
      ScaleType.FIT_XY,
      ScaleType.FIT_START,
      ScaleType.FIT_CENTER,
      ScaleType.FIT_END,
      ScaleType.CENTER,
      ScaleType.CENTER_CROP,
      ScaleType.CENTER_INSIDE
  };

  private float cornerRadius = DEFAULT_RADIUS;
  private float borderWidth = DEFAULT_BORDER_WIDTH;
  private ColorStateList borderColor =
      ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
  private boolean isOval = false;
  private boolean mutateBackground = false;

  private int mResource;
  private Drawable mDrawable;
  private Drawable mBackgroundDrawable;

  private ScaleType mScaleType;

  public RoundedImageView(Context context) {
    super(context);
  }

  public RoundedImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView, defStyle, 0);

    int index = a.getInt(R.styleable.RoundedImageView_android_scaleType, -1);
    if (index >= 0) {
      setScaleType(SCALE_TYPES[index]);
    } else {
      // default scaletype to FIT_CENTER
      setScaleType(ScaleType.FIT_CENTER);
    }

    cornerRadius = a.getDimensionPixelSize(R.styleable.RoundedImageView_corner_radius, -1);
    borderWidth = a.getDimensionPixelSize(R.styleable.RoundedImageView_border_width, -1);

    // don't allow negative values for radius and border
    if (cornerRadius < 0) {
      cornerRadius = DEFAULT_RADIUS;
    }
    if (borderWidth < 0) {
      borderWidth = DEFAULT_BORDER_WIDTH;
    }

    borderColor = a.getColorStateList(R.styleable.RoundedImageView_border_color);
    if (borderColor == null) {
      borderColor = ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
    }

    mutateBackground = a.getBoolean(R.styleable.RoundedImageView_mutate_background, false);
    isOval = a.getBoolean(R.styleable.RoundedImageView_oval, false);

    updateDrawableAttrs();
    updateBackgroundDrawableAttrs(true);

    a.recycle();
  }

  @Override
  protected void drawableStateChanged() {
    super.drawableStateChanged();
    invalidate();
  }

  /**
   * Return the current scale type in use by this ImageView.
   *
   * @attr ref android.R.styleable#ImageView_scaleType
   * @see android.widget.ImageView.ScaleType
   */
  @Override
  public ScaleType getScaleType() {
    return mScaleType;
  }

  /**
   * Controls how the image should be resized or moved to match the size
   * of this ImageView.
   *
   * @param scaleType The desired scaling mode.
   * @attr ref android.R.styleable#ImageView_scaleType
   */
  @Override
  public void setScaleType(ScaleType scaleType) {
    assert scaleType != null;

    if (mScaleType != scaleType) {
      mScaleType = scaleType;

      switch (scaleType) {
        case CENTER:
        case CENTER_CROP:
        case CENTER_INSIDE:
        case FIT_CENTER:
        case FIT_START:
        case FIT_END:
        case FIT_XY:
          super.setScaleType(ScaleType.FIT_XY);
          break;
        default:
          super.setScaleType(scaleType);
          break;
      }

      updateDrawableAttrs();
      updateBackgroundDrawableAttrs(false);
      invalidate();
    }
  }

  @Override
  public void setImageDrawable(Drawable drawable) {
    mResource = 0;
    mDrawable = RoundedDrawable.fromDrawable(drawable);
    updateDrawableAttrs();
    super.setImageDrawable(mDrawable);
  }

  @Override
  public void setImageBitmap(Bitmap bm) {
    mResource = 0;
    mDrawable = RoundedDrawable.fromBitmap(bm);
    updateDrawableAttrs();
    super.setImageDrawable(mDrawable);
  }

  @Override
  public void setImageResource(int resId) {
    if (mResource != resId) {
      mResource = resId;
      mDrawable = resolveResource();
      updateDrawableAttrs();
      super.setImageDrawable(mDrawable);
    }
  }

  @Override public void setImageURI(Uri uri) {
    super.setImageURI(uri);
    setImageDrawable(getDrawable());
  }

  private Drawable resolveResource() {
    Resources rsrc = getResources();
    if (rsrc == null) { return null; }

    Drawable d = null;

    if (mResource != 0) {
      try {
        d = rsrc.getDrawable(mResource);
      } catch (Exception e) {
        Log.w(TAG, "Unable to find resource: " + mResource, e);
        // Don't try again.
        mResource = 0;
      }
    }
    return RoundedDrawable.fromDrawable(d);
  }

  @Override
  public void setBackground(Drawable background) {
    setBackgroundDrawable(background);
  }

  private void updateDrawableAttrs() {
    updateAttrs(mDrawable);
  }

  private void updateBackgroundDrawableAttrs(boolean convert) {
    if (mutateBackground) {
      if (convert) {
        mBackgroundDrawable = RoundedDrawable.fromDrawable(mBackgroundDrawable);
      }
      updateAttrs(mBackgroundDrawable);
    }
  }

  private void updateAttrs(Drawable drawable) {
    if (drawable == null) { return; }

    if (drawable instanceof RoundedDrawable) {
      ((RoundedDrawable) drawable)
          .setScaleType(mScaleType)
          .setCornerRadius(cornerRadius)
          .setBorderWidth(borderWidth)
          .setBorderColor(borderColor)
          .setOval(isOval);
    } else if (drawable instanceof LayerDrawable) {
      // loop through layers to and set drawable attrs
      LayerDrawable ld = ((LayerDrawable) drawable);
      for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) {
        updateAttrs(ld.getDrawable(i));
      }
    }
  }

  @Override
  @Deprecated
  public void setBackgroundDrawable(Drawable background) {
    mBackgroundDrawable = background;
    updateBackgroundDrawableAttrs(true);
    super.setBackgroundDrawable(mBackgroundDrawable);
  }

  public float getCornerRadius() {
    return cornerRadius;
  }

  public void setCornerRadius(int resId) {
    setCornerRadius(getResources().getDimension(resId));
  }

  public void setCornerRadius(float radius) {
    if (cornerRadius == radius) { return; }

    cornerRadius = radius;
    updateDrawableAttrs();
    updateBackgroundDrawableAttrs(false);
  }

  public float getBorderWidth() {
    return borderWidth;
  }

  public void setBorderWidth(int resId) {
    setBorderWidth(getResources().getDimension(resId));
  }

  public void setBorderWidth(float width) {
    if (borderWidth == width) { return; }

    borderWidth = width;
    updateDrawableAttrs();
    updateBackgroundDrawableAttrs(false);
    invalidate();
  }

  public int getBorderColor() {
    return borderColor.getDefaultColor();
  }

  public void setBorderColor(int color) {
    setBorderColor(ColorStateList.valueOf(color));
  }

  public ColorStateList getBorderColors() {
    return borderColor;
  }

  public void setBorderColor(ColorStateList colors) {
    if (borderColor.equals(colors)) { return; }

    borderColor =
        (colors != null) ? colors : ColorStateList.valueOf(RoundedDrawable.DEFAULT_BORDER_COLOR);
    updateDrawableAttrs();
    updateBackgroundDrawableAttrs(false);
    if (borderWidth > 0) {
      invalidate();
    }
  }

  public boolean isOval() {
    return isOval;
  }

  public void setOval(boolean oval) {
    isOval = oval;
    updateDrawableAttrs();
    updateBackgroundDrawableAttrs(false);
    invalidate();
  }

  public boolean isMutateBackground() {
    return mutateBackground;
  }

  public void setMutateBackground(boolean mutate) {
    if (mutateBackground == mutate) { return; }

    mutateBackground = mutate;
    updateBackgroundDrawableAttrs(true);
    invalidate();
  }
}

小记: 太方便了  真心感谢开源者

每日精进一点点  谢谢



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值