Android自定义圆角ImageView控件

 更新时间:2017年05月05日 14:21:04   作者:東方月初  
这篇文章主要为大家详细介绍了Android自定义圆角ImageView的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

目前一些比较火的图片加载库虽然支持圆角加载,若你是接的别人作了一半的项目,刚好别人用的图片加载库刚好不支持圆角加载,那么这颗控件你值得拥有.(支持网络图片的加载)

1.创建CustomImageView 类在你的项目中(源码如下)

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.towatt.charge.towatt.R;

/**
 * @author Mr.lynn
 * @version 1.0<br>
 * 图片圆角实现
 */
public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
 private Paint paint;
 private Paint paintBorder;
 private Bitmap mSrcBitmap;
 /**
  * 圆角的弧度
  */
 private float mRadius;
 private boolean mIsCircle;

 public CustomImageView(final Context context) {
  this(context, null);
 }

 public CustomImageView(Context context, AttributeSet attrs) {
  this(context, attrs, R.attr.customImageViewStyle);
 }

 public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  TypedArray ta = context.obtainStyledAttributes(attrs,
    R.styleable.CustomImageView, defStyle, 0);
  mRadius = ta.getDimension(R.styleable.CustomImageView_radius, 0);
  mIsCircle = ta.getBoolean(R.styleable.CustomImageView_circle, false);
  int srcResource = attrs.getAttributeResourceValue(
    "http://schemas.android.com/apk/res/android", "src", 0);
  if (srcResource != 0)
   mSrcBitmap = BitmapFactory.decodeResource(getResources(),
     srcResource);
  ta.recycle();
  paint = new Paint();
  paint.setAntiAlias(true);
  paintBorder = new Paint();
  paintBorder.setAntiAlias(true);
 }

 @Override
 public void onDraw(Canvas canvas) {
  int width = canvas.getWidth() - getPaddingLeft() - getPaddingRight();
  int height = canvas.getHeight() - getPaddingTop() - getPaddingBottom();
  Bitmap image = drawableToBitmap(getDrawable());
  if (mIsCircle) {
   Bitmap reSizeImage = reSizeImageC(image, width, height);
   canvas.drawBitmap(createCircleImage(reSizeImage, width, height),
     getPaddingLeft(), getPaddingTop(), null);

  } else {

   Bitmap reSizeImage = reSizeImage(image, width, height);
   canvas.drawBitmap(createRoundImage(reSizeImage, width, height),
     getPaddingLeft(), getPaddingTop(), null);
  }
 }

 /**
  * 画圆角
  *
  * @param source
  * @param width
  * @param height
  * @return
  */
 private Bitmap createRoundImage(Bitmap source, int width, int height) {
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888);
  Canvas canvas = new Canvas(target);
  RectF rect = new RectF(0, 0, width, height);
  canvas.drawRoundRect(rect, mRadius, mRadius, paint);
  // 核心代码取两个图片的交集部分
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  canvas.drawBitmap(source, 0, 0, paint);
  return target;
 }

 /**
  * 画圆
  *
  * @param source
  * @param width
  * @param height
  * @return
  */
 private Bitmap createCircleImage(Bitmap source, int width, int height) {

  Paint paint = new Paint();
  paint.setAntiAlias(true);
  Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888);
  Canvas canvas = new Canvas(target);
  canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2,
    paint);
  // 核心代码取两个图片的交集部分
  paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  canvas.drawBitmap(source, (width - source.getWidth()) / 2,
    (height - source.getHeight()) / 2, paint);
  return target;

 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int width = MeasureSpec.getSize(widthMeasureSpec);
  int height = MeasureSpec.getSize(heightMeasureSpec);
  setMeasuredDimension(width, height);
 }

 /**
  * drawable转bitmap
  *
  * @param drawable
  * @return
  */
 private Bitmap drawableToBitmap(Drawable drawable) {
  if (drawable == null) {
   if (mSrcBitmap != null) {
    return mSrcBitmap;
   } else {
    return null;
   }
  } else if (drawable instanceof BitmapDrawable) {
   return ((BitmapDrawable) drawable).getBitmap();
  }
  Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
    drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
  drawable.draw(canvas);
  return bitmap;
 }

 /**
  * 重设Bitmap的宽高
  *
  * @param bitmap
  * @param newWidth
  * @param newHeight
  * @return
  */
 private Bitmap reSizeImage(Bitmap bitmap, int newWidth, int newHeight) {
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  // 计算出缩放比
  float scaleWidth = ((float) newWidth) / width;
  float scaleHeight = ((float) newHeight) / height;
  // 矩阵缩放bitmap
  Matrix matrix = new Matrix();

  matrix.postScale(scaleWidth, scaleHeight);
  return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
 }

 /**
  * 重设Bitmap的宽高
  *
  * @param bitmap
  * @param newWidth
  * @param newHeight
  * @return
  */
 private Bitmap reSizeImageC(Bitmap bitmap, int newWidth, int newHeight) {
  int width = bitmap.getWidth();
  int height = bitmap.getHeight();
  int x = (newWidth - width) / 2;
  int y = (newHeight - height) / 2;
  if (x > 0 && y > 0) {
   return Bitmap.createBitmap(bitmap, 0, 0, width, height, null, true);
  }

  float scale = 1;

  if (width > height) {
   // 按照宽度进行等比缩放
   scale = ((float) newWidth) / width;

  } else {
   // 按照高度进行等比缩放
   // 计算出缩放比
   scale = ((float) newHeight) / height;
  }
  Matrix matrix = new Matrix();
  matrix.postScale(scale, scale);
  return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
 }
}


2.在values目录下创建attrs.xml(若是已存在该文件)直接复制如下代码既可

<resources>
 <declare-styleable name="Theme">
  <attr name="customImageViewStyle" format="reference" />
 </declare-styleable>
 <!-- 自定义圆角ImageView -->
 <declare-styleable name="CustomImageView">
  <attr name="circle" format="boolean" />
  <attr name="radius" format="dimension" />
 </declare-styleable>
</resources>


3.正确的使用方式(在布局文件中)

注意此路径是你控件所在包

 <com.xxx.xxx.view.CustomImageView
  android:layout_marginTop="5dp"
  android:layout_width="77dp"
  android:layout_height="77dp"
  lynn:radius="@dimen/size_10dp"
  android:src="@drawable/position_icon"
  android:id="@+id/iv_build_icon" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android draw9patch 图片制作与使用详解

    Android draw9patch 图片制作与使用详解

    本文主要介绍Android draw9patch的图片使用和制作,这里详细说明如何操作,并帮大家整理了几句口令,帮助大家记住,有兴趣的小伙伴可以参考下
    2016-09-09
  • 详解Android中解析XML的方法

    详解Android中解析XML的方法

    XML在各种开发中都广泛应用,Android也不例外。这篇文章主要介绍了详解Android中解析XML的方法,有需要的可以了解一下。
    2016-11-11
  • Android Studio 中aidl的自定义类的使用详解

    Android Studio 中aidl的自定义类的使用详解

    这篇文章主要介绍了Android Studio 中aidl的自定义类的使用详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • 一文了解Android ViewModelScope 如何自动取消协程

    一文了解Android ViewModelScope 如何自动取消协程

    这篇文章主要介绍了一文了解Android ViewModelScope 如何自动取消协程,文章围绕主题站展开详细的内容介绍,具有一定参考价值,感兴趣的小伙伴可以参考一下
    2022-07-07
  • android编程实现类似于支付宝余额快速闪动效果的方法

    android编程实现类似于支付宝余额快速闪动效果的方法

    这篇文章主要介绍了android编程实现类似于支付宝余额快速闪动效果的方法,涉及Android时间函数的相关使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • Flutter持久化存储之数据库存储(sqflite)详解

    Flutter持久化存储之数据库存储(sqflite)详解

    这篇文章主要给大家介绍了关于Flutter持久化存储之数据库存储的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Flutter具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • Android中Canvas的常用方法总结

    Android中Canvas的常用方法总结

    在Android自定义View的时候,我们经常需要绘制一些自己想要的效果。这里就需要使用Canvas对象。下面这篇文章将Canvas对象常用方法做个笔记,方便自己和大家以后使用的时候查阅,下面来一起看看吧。
    2016-09-09
  • Android如何从实现到封装一个MVP详解

    Android如何从实现到封装一个MVP详解

    原生的 MVC 框架遇到大规模的应用,就会变得代码难读,不好维护,无法测试的囧境。因此,Android 开发方面也有很多对应的框架来解决这些问题。所以这篇文章主要给大家介绍了关于Android如何从实现到封装一个MVP的相关资料,需要的朋友可以参考下。
    2017-09-09
  • Flutter listview如何实现下拉刷新上拉加载更多功能

    Flutter listview如何实现下拉刷新上拉加载更多功能

    这篇文章主要给大家介绍了关于Flutter listview如何实现下拉刷新上拉加载更多功能的相关资料,对于新闻列表数据的更新和加载更多是必不可少的,而实现下拉刷新与上划加载更多的方式有很多种,需要的朋友可以参考下
    2021-08-08
  • 在Android环境下WebView中拦截所有请求并替换URL示例详解

    在Android环境下WebView中拦截所有请求并替换URL示例详解

    这篇文章主要介绍了在Android环境下WebView中拦截所有请求并替换URL示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07

最新评论