麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

Android自定義ImageView實現圓角功能

2019-10-21 21:32:27
字體:
來源:轉載
供稿:網友

使用自定義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);  }} 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美成人性色区 | 羞羞答答www网站进入 | 成人免费一区二区三区 | 少妇一级淫片免费看 | 久久久久999 | 久久精品一区视频 | 成人免费看视频 | 国产69精品99久久久久久宅男 | 久久久无码精品亚洲日韩按摩 | 99ri在线| 欧美福利视频一区二区 | 在线观看中文字幕国产 | 欧美黄色一级片视频 | 中国a级黄色片 | 精品一区二区在线观看视频 | 欧美成人高清视频 | 精品国产乱码久久久久久久久 | 91久久精品一 | 精品小视频 | 国产福利视频在线观看 | 日韩精品久久久 | 一级成人黄色片 | 久久经典国产视频 | 欧美高清一级片 | 国产精品久久久久久婷婷天堂 | 草草影院地址 | 色视频一区二区 | 久久久久久久久久91 | 高清做爰免费无遮网站挡 | 日本在线视频免费 | chinese 军人 gay xx 呻吟 | 深夜福利久久久 | 欧美中文字幕一区二区三区亚洲 | 黄色免费av| 国产在线精品一区二区三区 | 欧美一区二区三区久久精品视 | 久久精品视频在线 | wwwxxx国产| 色就色 综合偷拍区91网 | 国产妇女乱码一区二区三区 | 色999久久久精品人人澡69 |