天天看點

【轉載】 android 圓角圓形圖檔ShapedImageView不到100行代碼

此文章作為筆記,摘自:ShapedImageView  

感謝作者的分享,對開發很有幫助!

  • 簡潔,不到100行代碼
  • 支援

    圓形

    圓角矩形

  • 支援

    TransitionDrawable

【轉載】 android 圓角圓形圖檔ShapedImageView不到100行代碼

一、studio

dependencies

compile 'cn.gavinliu.android.lib:ShapedImageView:0.4'
           

二、eclipse

建立class 你懂得  下載下傳https://github.com/gavinliu/ShapedImageView/blob/master/ShapedImageView/src/main/java/cn/gavinliu/android/lib/shapedimageview/ShapedImageView.java

attr

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="ShapedImageView">
        <attr name="shape_mode" format="enum">
            <enum name="round_rect" value="1" />
            <enum name="circle" value="2" />
        </attr>
        <attr name="round_radius" format="dimension" />
    </declare-styleable>

</resources>      

在Layout檔案 頭上加

xmlns:app="http://schemas.android.com/apk/res-auto"
           

Circle

<cn.gavinliu.android.lib.shapedimageview.ShapedImageView
    android:id="@+id/image1"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    app:shape_mode="circle" />      

Round Rect

<cn.gavinliu.android.lib.shapedimageview.ShapedImageView
    android:id="@+id/image2"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    app:round_radius="20dp"
    app:shape_mode="round_rect" />