Android自定义ImageView实现圆角功能

使用自定义ImageView,实现圆角功能,供大家参考,具体内容如下

1.自定义属性attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="RoundCornerImageView">
    <attr name="radius" format="dimension" />
    <attr name="left_top_radius" format="dimension" />
    <attr name="right_top_radius" format="dimension" />
    <attr name="right_bottom_radius" format="dimension" />
    <attr name="left_bottom_radius" format="dimension" />
  </declare-styleable>
</resources>

2.自定义RoundCornerImageView,继承AppCompatImageView

public class RoundCornerImageView extends AppCompatImageView {
  private float width, height;
  private int defaultRadius = 0;
  private int radius;
  private int leftTopRadius;
  private int rightTopRadius;
  private int rightBottomRadius;
  private int leftBottomRadius;

  public RoundCornerImageView(Context context) {
    this(context, null);
    init(context, null);
  }

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

  public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
  }

  private void init(Context context, AttributeSet attrs) {
    if (Build.VERSION.SDK_INT < 18) {
      setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    // 读取配置
    TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView);
    radius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_radius, defaultRadius);
    leftTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_top_radius, defaultRadius);
    rightTopRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_top_radius, defaultRadius);
    rightBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_right_bottom_radius, defaultRadius);
    leftBottomRadius = array.getDimensionPixelOffset(R.styleable.RoundCornerImageView_left_bottom_radius, defaultRadius);

    //如果四个角的值没有设置,那么就使用通用的radius的值。
    if (defaultRadius == leftTopRadius) {
      leftTopRadius = radius;
    }
    if (defaultRadius == rightTopRadius) {
      rightTopRadius = radius;
    }
    if (defaultRadius == rightBottomRadius) {
      rightBottomRadius = radius;
    }
    if (defaultRadius == leftBottomRadius) {
      leftBottomRadius = radius;
    }
    array.recycle();
  }

  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    width = getWidth();
    height = getHeight();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    //这里做下判断,只有图片的宽高大于设置的圆角距离的时候才进行裁剪
    int maxLeft = Math.max(leftTopRadius, leftBottomRadius);
    int maxRight = Math.max(rightTopRadius, rightBottomRadius);
    int minWidth = maxLeft + maxRight;
    int maxTop = Math.max(leftTopRadius, rightTopRadius);
    int maxBottom = Math.max(leftBottomRadius, rightBottomRadius);
    int minHeight = maxTop + maxBottom;
    if (width >= minWidth && height > minHeight) {
      Path path = new Path();
      //四个角:右上,右下,左下,左上
      path.moveTo(leftTopRadius, 0);
      path.lineTo(width - rightTopRadius, 0);
      path.quadTo(width, 0, width, rightTopRadius);

      path.lineTo(width, height - rightBottomRadius);
      path.quadTo(width, height, width - rightBottomRadius, height);

      path.lineTo(leftBottomRadius, height);
      path.quadTo(0, height, 0, height - leftBottomRadius);

      path.lineTo(0, leftTopRadius);
      path.quadTo(0, 0, leftTopRadius, 0);

      canvas.clipPath(path);
    }
    super.onDraw(canvas);
  }

}

3.布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="voicedemo.iflytek.com.roundimage.MainActivity">

  <voicedemo.iflytek.com.roundimage.RoundCornerImageView
    android:id="@+id/iv_avatar"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="50dp"
    android:scaleType="centerCrop"
    app:left_top_radius="20dp"
    app:right_top_radius="20dp"
    />
</LinearLayout>

4.调用

public class MainActivity extends AppCompatActivity {

  String avatarUrl = "19e9d4c0a8f1cd033ecac3692_th.jpg";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView ivAvatar = findViewById(R.id.iv_avatar);

    Glide.with(this).load(avatarUrl).into(ivAvatar);

  }
} 

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

(0)

相关推荐

  • Android自定义圆角ImageView

    废话不多说了,直接给大家贴代码了. java类如下: import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.gra

  • Android 圆角 ImageView类可设置弧度(代码简单)

    废话不多说了,直接给大家贴代码了,具体代码如下所示: public class RoundImageView extends ImageView { private Paint paint; private int roundWidth = 50; private int roundHeight = 50; private Paint paint2; public RoundImageView(Context context, AttributeSet attrs, int defStyle)

  • Android实现自定义ImageView的圆角矩形图片效果

    android中的ImageView只能显示矩形的图片,这样一来不能满足我们其他的需求,比如要显示圆角矩形的图片,这个时候,我们就需要自定义ImageView了,其原理就是首先获取到图片的Bitmap,然后进行裁剪对应的圆角矩形的bitmap,然后在onDraw()进行绘制圆角矩形图片输出. 效果图如下: 自定义的圆形的ImageView类的实现代码如下: package com.xc.xcskin.view; import android.content.Context; import and

  • Android实现圆角矩形和圆形ImageView的方式

    Android中实现圆角矩形和圆形有很多种方式,其中最常见的方法有ImageLoader设置Option和自定义View. 1.ImageLoader加载图片 public static DisplayImageOptions getRoundOptions() { DisplayImageOptions options = new DisplayImageOptions.Builder() // 是否设置为圆角,弧度为多少,当弧度为90时显示的是一个圆 .displayer(new Round

  • Android自定义带圆角的ImageView

    最近有一个实现一个带有圆角的ImageView的需求,在网上找了找三方,虽然Demo都是正确的,但是移植过来就不可以了,因为请求链接的时候用的是xUtils中Bitmap来进行解析的,这样就总是会报类型转换异常的错误. 就这样只能自己定义一个了. Demo: package com.yizooo.yizooo.ui; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bi

  • Android ImageView绘制圆角效果

    前言 Android 开发中,我们经常需要实现图片的圆形/圆角的效果,我们可以使用两种方式来实现这样的效果.一种是使用Xfermode,另一种是BitmapShader来实现.下面我将分别介绍这两种用法. 使用Xfermode的方式实现 使用该方式的关键代码,如下: private Bitmap creataBitmap(Bitmap bitmap) { //用指定的一个Bitmap来构建一个画布 Bitmap target = Bitmap.createBitmap(1000,1000, Bi

  • Android自定义控件之圆形、圆角ImageView

    一.问题在哪里? 问题来源于app开发中一个很常见的场景--用户头像要展示成圆的:  二.怎么搞? 机智的我,第一想法就是,切一张中间圆形透明.四周与底色相同.尺寸与头像相同的蒙板图片,盖在头像上不就完事了嘛,哈哈哈! 在背景纯色的前提下,这的确能简单解决问题,但是如果背景没有这么简单呢? 在这种不规则背景下,有两个问题: 1).背景图常常是适应手机宽度缩放,而头像的尺寸又是固定宽高DP的,所以固定的蒙板图片是没法保证在不同机型上都和背景图案吻合的. 2).在这种非纯色背景下,哪天想调整一下头像

  • Android中通过反射实现圆角ImageView代码实例

    private void init(){ paint = new Paint(Paint.ANTI_ALIAS_FLAG); roundRect = new RectF(0, 0, getWidth() , getHeight()); radius = 40; mPorterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN) ; } 继承ImageView,在构造方法中调用,初始化Paint和Xfermode. protec

  • Android自定义圆角ImageView控件

    目前一些比较火的图片加载库虽然支持圆角加载,若你是接的别人作了一半的项目,刚好别人用的图片加载库刚好不支持圆角加载,那么这颗控件你值得拥有.(支持网络图片的加载) 1.创建CustomImageView 类在你的项目中(源码如下) import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Bitmap.

  • Android自定义ImageView实现圆角功能

    使用自定义ImageView,实现圆角功能,供大家参考,具体内容如下 1.自定义属性attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RoundCornerImageView"> <attr name="radius" format="dimension&

  • android自定义imageview实现圆角图片

    本文实例为大家分享了android自定义imageview实现圆角图片的具体代码,供大家参考,具体内容如下 自定义图片的属性,对图片进行圆角切割 实现效果图: (1)在activity_main.xml文件布局,非常简单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android

  • Android 自定义imageview实现图片缩放实例详解

    Android 自定义imageview实现图片缩放实例详解 觉得这个自定义的imageview很好用 性能不错  所以拿出来分享给大家  因为不会做gif图  所以项目效果 就不好贴出来了  把代码贴出来 1.项目结构图 2.Compat.class package com.suo.image; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; import android.view.View; pu

  • Android自定义View展开菜单功能的实现

    先给大家展示下效果图,如果大家感觉不错,请参考实现代码. 思路 1.下角Button的父View加入一个FrameLayout,也就是图中全屏透明灰色部分. 菜单没有弹出的时候设置为不可见. 设置FrameLayout点击事件,点击的时候缩回菜单. 对应init() 2.rameLayout中加入菜单按钮,也就是弹出的那三个. 菜单没有弹出的时候设置为不可见. 对应addElement()和freshElement() 3.右下角的按钮,旋转图标(也可以不旋转). 对应setRotateAnim

  • Android自定义processor实现bindView功能的实例

    一.简介 在现阶段的Android开发中,注解越来越流行起来,比如ButterKnife,Retrofit,Dragger,EventBus等等都选择使用注解来配置.按照处理时期,注解又分为两种类型,一种是运行时注解,另一种是编译时注解,运行时注解由于性能问题被一些人所诟病.编译时注解的核心依赖APT(Annotation Processing Tools)实现,原理是在某些代码元素上(如类型.函数.字段等)添加注解,在编译时编译器会检查AbstractProcessor的子类,并且调用该类型的

  • Android自定义view实现标签栏功能(只支持固定两个标签)

    实现效果图 主要代码 完整源代码 class TabView(context: Context, attributeSet: AttributeSet?) : LinearLayout(context, attributeSet) { private lateinit var firstTab: View private lateinit var secondTab: View private val firstTabIndex = 0 private val secondTabIndex =

  • Android 自定义图片地图坐标功能的实现

    一.前言 最近项目要求实现一个在自定义地图图片上添加坐标信息的功能,类似于在图片做标注的功能.如下图所示.坐标的位置是相对于图片宽高的百分比 二.思路 改功能主要分为三个视图,1.继承FrameLayout作为父容器:2.添加一个铺满父布局的ImageView显示地图图片:3.动态添加自定义坐标视图 三.代码实现 1. 自定义坐标视图 <?xml version="1.0" encoding="utf-8"?> <androidx.constrai

  • Android自定义ImageView实现点击两张图片切换效果

    笔者在做一个项目中遇到的一个小阻碍,于是就实现了这个ImageView达到开发需求 情景需求 > 点击实现图片的切换 可能有人会说了,这还不简单?为ImageView设置点击事件,然后通过重写的onClick(View v)方法判断定义的某一个flag进行图片的切换,伪代码如下: private boolean flag; public void onClick(View v){ if(flag){ mImageView.setImageResource(R.drawable.xx1); }el

  • Android自定义View画圆功能

    本文实例为大家分享了Android自定义View画圆的具体代码,供大家参考,具体内容如下 引入布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&q

随机推荐