Android中使用CircleImageView和Cardview製作圓形頭像的方法_Android

來源:互聯網
上載者:User

圓形頭像在我們的日常使用的app中很常見,因為圓形的頭像比較美觀.

使用圓形圖片的方法可能有我們直接將圖片裁剪成圓形再在app中使用,還有就是使用自訂View對我們設定的任何圖片自動裁剪成圓形。

效果圖:

這裡使用github上CircleImageView

github:https://github.com/hdodenhof/CircleImageView

CardView顧名思義卡片式的View,CardView繼承的是FrameLayout,所以擺放內部控制項的時候需要注意一下

可以設定陰影,圓角,等等

這裡的CircleImageView還可以為頭像設定描邊。

我們建立一個項目,選擇Navigation Drawer Activity自動產生初始布局。

修改nav_header_main,添加圓角頭像

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="@dimen/nav_header_height"android:background="@drawable/side_nav_bar"android:gravity="bottom"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:theme="@style/ThemeOverlay.AppCompat.Dark"><de.hdodenhof.circleimageview.CircleImageViewxmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/profile_image"android:layout_width="96dp"android:layout_height="96dp"android:src="@drawable/darth_vader"app:civ_border_width="2dp"/></LinearLayout> 

再修改content_main,添加RecyclerView,記得導包

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns: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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:context="com.example.xw.design2.MainActivity"tools:showIn="@layout/app_bar_main"><android.support.v7.widget.RecyclerViewandroid:id="@+id/rv"android:layout_width="match_parent"android:layout_height="match_parent"></android.support.v7.widget.RecyclerView></RelativeLayout>

添加item布局,CardView,記得導包

<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.CardViewandroid:layout_height="wrap_content"android:layout_width="match_parent"xmlns:card_view="http://schemas.android.com/apk/res-auto"card_view:contentPadding="10dp"card_view:cardBackgroundColor="#303069"card_view:cardCornerRadius="10dp"card_view:cardPreventCornerOverlap="true"card_view:cardUseCompatPadding="true"xmlns:android="http://schemas.android.com/apk/res/android"><TextViewandroid:id="@+id/tv"android:textColor="#fff"android:layout_width="wrap_content"android:layout_height="wrap_content"/></android.support.v7.widget.CardView> 

接下來在MainActivity添加代碼,使用我們的CardView

1. 新增成員變數和資料來源

private RecyclerView mRecyclerView;private String[] data={"2014 年,隨著 Google 推出了全新的設計語言 Material Design,還迎來了新的 Android 支援庫 v7,其中就包含了 Material Design 設計語言中關於 Card 卡片概念的實現 —— CardView。","經曆了相當長的一段時間相信許多 Android 開發人員都已經應用了這個控制項,現在才寫這篇文章可能有點晚,但對於剛剛開始使用的開發人員以及其他已經使用了一段時間但做出來效果不好的同學應該能幫上點小忙。","Google 在 Android Lollipop 中引入了 Material Design 設計中的陰影(Elevation)和 Z 軸位移,其目的就是突出介面中不同元素之間的層次關係","明年夏天,自由球員布雷克-格裡芬可能重返奧克拉荷馬城與拉塞爾-威斯布魯克聯手。如果實現,雷霆隊能真正意義上地威脅勇士隊嗎?"};

2.建立ViewHolder

class MyHolder extends RecyclerView.ViewHolder{private TextView mTextView;public MyHolder(View itemView) {super(itemView);mTextView= (TextView) itemView.findViewById(R.id.tv);}}

3.建立Adapter

class MyAdapter extends RecyclerView.Adapter<MyHolder>{@Overridepublic MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {LayoutInflater layoutInflater=LayoutInflater.from(getApplicationContext());View v=layoutInflater.inflate(R.layout.item,parent,false);MyHolder holder=new MyHolder(v);return holder;}@Overridepublic void onBindViewHolder(MyHolder holder, int position) {holder.mTextView.setText(data[position]);}@Overridepublic int getItemCount() {return data.length;}}

4.oncreate()方法裡設定Adapter

mRecyclerView= (RecyclerView) findViewById(R.id.rv);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));mRecyclerView.setAdapter(new MyAdapter());

以上所述是小編給大家介紹的Android中使用CircleImageView和Cardview製作圓形頭像的方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.