android-圓形頭像

來源:互聯網
上載者:User

標籤:android   content   package   width   一個   source   drawable   ...   wrap   

引子:

  拍攝的照片,軟體截的圖,一般都是以矩形的形式呈現,然而很多時候android開發,需要做出一個圓形映像作為 使用者的頭像。

  有兩種方式可供選擇,

  1)直接使用androidSdk提供的RoundedBitmapDrawable.java類 對drawable設定圓角角度,可以產生圓形效果。

  2)CircleImageView是github上 的 一個第三方開源項目,它提供了圓形映像的專業處理方法。

 

  兩者的區別如下:

      1)RoundedBitmapDrawable 是通過設定圓角角度來造成圓形效果,所以它除了產生正圓效果,還能產生 圓角效果。這一點,CircleImageView做不到。

        2)RoundedBitmapDrawable 不能直接定義邊框效果(厚度,顏色等),但是CircleImageVIew可以。

樣本:RoundedBitmapDrawable:

layout.xml中:

1 <ImageView2         android:id="@+id/imageView3"3         android:layout_width="wrap_content"4         android:layout_height="wrap_content"5         android:padding="5dp"/>

 

 java代碼中:請重點看紅色注釋部分;

 

 1 package com.example.myroundimageview; 2  3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 import android.os.Bundle; 6 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; 7 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; 8 import android.support.v7.app.AppCompatActivity; 9 import android.widget.ImageView;10 11 public class MainActivity extends AppCompatActivity {12 13     @Override14     protected void onCreate(Bundle savedInstanceState) {15         super.onCreate(savedInstanceState);16         setContentView(R.layout.layout);17 18         Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.me);//1、取得圖片資源的bitmap對象19         Bitmap dst;//2、將長方形圖片裁剪成正方形圖片20         if (src.getWidth() >= src.getHeight()) {// 所謂的裁剪,就是用Bitmap的create方法,指定寬高和源bitmap21             dst = Bitmap.createBitmap(src, src.getWidth() / 2 - src.getHeight() / 2, 0, src.getHeight(), src.getHeight());22         } else {23             dst = Bitmap.createBitmap(src, 0, src.getHeight() / 2 - src.getWidth() / 2, src.getWidth(), src.getWidth());24         }25         //這樣,就得到了一個方形的圖26         RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), dst);// 3、再啟用RoundedBitmapDrawable設定圓角和消除鋸齒27         roundedBitmapDrawable.setCornerRadius(dst.getWidth() / 2); //設定圓角半徑為正方形邊長的一半28         roundedBitmapDrawable.setAntiAlias(true);//圖片的裁剪通常會造成鋸齒,這裡要設定消除鋸齒29         ImageView image3 = (ImageView) findViewById(R.id.imageView3);//4、將處理之後的drawable對象設定到imageView中30         image3.setImageDrawable(roundedBitmapDrawable);31     }32 }

 

 

 

CircleImageView:

在androidStudio中引用 它的庫:

 

dependencies {    ....    implementation ‘de.hdodenhof:circleimageview:2.2.0‘}

 

在layout.xml中:

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/itsme"
app:civ_border_color="@android:color/holo_green_dark"
app:civ_border_overlay="true"
app:civ_border_width="2dp"
app:civ_circle_background_color="@color/colorPrimaryDark"
app:civ_fill_color="@color/colorAccent"/>

至於這個CircleImageView的內部程式碼分析,這裡留個位置,後面來填。

······

 

android-圓形頭像

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.