Github最火開源項目-一分鐘學會自定義ImageView外貌

開源地址:https://github.com/open-android/RoundedImageView

運行效果

使用步驟

1. 在project的build.gradle添加如下代碼(如下圖)

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

2. 在Module的build.gradle添加依賴

compile 'com.github.open-android:RoundedImageView:v1.0.0'

3. 複製如下代碼到xml

    <com.itheima.roundedimageview.RoundedImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/icon_33"
        android:scaleType="fitCenter"
        app:riv_corner_radius="10dip"
        app:riv_border_width="1dip"
        app:riv_border_color="#333333"
        app:riv_oval="false" />

    <!--
        關鍵屬性解釋:

        app:riv_corner_radius : 四周角度
        app:riv_border_width :  描邊寬度
        app:riv_border_color : 描邊顏色
        app:riv_oval="false"  : 是否是圓型 , 若爲true, 則上面的radius可不用設置。-->

細節

  • 當然也可以使用代碼來控制

    RoundedImageView riv = new RoundedImageView(context);
    riv.setScaleType(ScaleType.CENTER_CROP); //縮放居中
    riv.setCornerRadius((float) 10); // 四周角度
    riv.setBorderWidth((float) 2); //描邊大小
    riv.setBorderColor(Color.DKGRAY); //描邊顏色
    riv.setImageDrawable(drawable); //圖片設置
    riv.setBackground(backgroundDrawable); //背景設置
    riv.setOval(true); //是否爲橢圓
    
  • 當然也可以配合Picasso來對圖片進行藝術處理 (黑白照)

    Transformation transformation = new RoundedTransformationBuilder()
      .borderColor(Color.BLACK)
      .borderWidthDp(3)
      .cornerRadiusDp(30)
      .oval(false)
      .build();
    
    Picasso.with(context)
        .load(url)
        .fit()
        .transform(transformation)
        .into(imageView);
    

歡迎關注微信公衆號

微信公衆號名稱:Android乾貨程序員

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章