Android中使用CircleImageView和Cardview制作圓形頭像的方法
圓形頭像在我們的日常使用的app中很常見(jiàn),因?yàn)閳A形的頭像比較美觀.
使用圓形圖片的方法可能有我們直接將圖片裁剪成圓形再在app中使用,還有就是使用自定義View對(duì)我們?cè)O(shè)置的任何圖片自動(dòng)裁剪成圓形。
效果圖:

這里使用github上CircleImageView
github:https://github.com/hdodenhof/CircleImageView
CardView顧名思義卡片式的View,CardView繼承的是FrameLayout,所以擺放內(nèi)部控件的時(shí)候需要注意一下
可以設(shè)置陰影,圓角,等等
這里的CircleImageView還可以為頭像設(shè)置描邊。
我們新建一個(gè)項(xiàng)目,選擇Navigation Drawer Activity自動(dòng)生成初始布局。
修改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.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/darth_vader" app:civ_border_width="2dp" /> </LinearLayout>
再修改content_main,添加RecyclerView,記得導(dǎo)包
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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: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.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </RelativeLayout>
添加item布局,CardView,記得導(dǎo)包
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView android: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"> <TextView android:id="@+id/tv" android:textColor="#fff" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </android.support.v7.widget.CardView>
接下來(lái)在MainActivity添加代碼,使用我們的CardView
1. 添加成員變量和數(shù)據(jù)源
private RecyclerView mRecyclerView;
private String[] data={"2014 年,隨著 Google 推出了全新的設(shè)計(jì)語(yǔ)言 Material Design,還迎來(lái)了新的 Android 支持庫(kù) v7,其中就包含了 Material Design 設(shè)計(jì)語(yǔ)言中關(guān)于 Card 卡片概念的實(shí)現(xiàn) —— CardView。"
,"經(jīng)歷了相當(dāng)長(zhǎng)的一段時(shí)間相信許多 Android 開(kāi)發(fā)者都已經(jīng)應(yīng)用了這個(gè)控件,現(xiàn)在才寫(xiě)這篇文章可能有點(diǎn)晚,但對(duì)于剛剛開(kāi)始使用的開(kāi)發(fā)者以及其他已經(jīng)使用了一段時(shí)間但做出來(lái)效果不好的同學(xué)應(yīng)該能幫上點(diǎn)小忙。"
,"Google 在 Android Lollipop 中引入了 Material Design 設(shè)計(jì)中的陰影(Elevation)和 Z 軸位移,其目的就是突出界面中不同元素之間的層次關(guān)系"
,"明年夏天,自由球員布雷克-格里芬可能重返俄克拉何馬城與拉塞爾-威斯布魯克聯(lián)手。如果實(shí)現(xiàn),雷霆隊(duì)能真正意義上地威脅勇士隊(duì)嗎?"};
2.創(chuàng)建ViewHolder
class MyHolder extends RecyclerView.ViewHolder{
private TextView mTextView;
public MyHolder(View itemView) {
super(itemView);
mTextView= (TextView) itemView.findViewById(R.id.tv);
}
}
3.創(chuàng)建Adapter
class MyAdapter extends RecyclerView.Adapter<MyHolder>{
@Override
public 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;
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.mTextView.setText(data[position]);
}
@Override
public int getItemCount() {
return data.length;
}
}
4.oncreate()方法里設(shè)置Adapter
mRecyclerView= (RecyclerView) findViewById(R.id.rv); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setAdapter(new MyAdapter());
以上所述是小編給大家介紹的Android中使用CircleImageView和Cardview制作圓形頭像的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android控件CardView實(shí)現(xiàn)卡片效果
- Android使用CardView實(shí)現(xiàn)圓角對(duì)話框
- Android控件CardView實(shí)現(xiàn)卡片布局
- Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁(yè)動(dòng)畫(huà)的方法
- Android使用CardView作為RecyclerView的Item并實(shí)現(xiàn)拖拽和左滑刪除
- Android CardView詳解及使用方法和實(shí)例
- Android應(yīng)用開(kāi)發(fā)中CardView的初步使用指南
- Android MaterialCardView的使用介紹與示例
相關(guān)文章
Android開(kāi)發(fā)實(shí)現(xiàn)Gallery畫(huà)廊效果的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)Gallery畫(huà)廊效果的方法,結(jié)合具體實(shí)例形式分析了Android使用Gallery實(shí)現(xiàn)畫(huà)廊功能的具體操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06
Android開(kāi)發(fā)自學(xué)筆記(二):工程文件剖析
這篇文章主要介紹了Android開(kāi)發(fā)自學(xué)筆記(二):工程文件剖析,本文講解了AndroidManifest.xml、src文件夾、res文件夾等文件的作用,需要的朋友可以參考下2015-04-04
使用Broadcast實(shí)現(xiàn)Android組件間的通信
這篇文章主要為大家詳細(xì)介紹了使用Broadcast實(shí)現(xiàn)Android組件間的通信,感興趣的小伙伴們可以參考一下2016-06-06
Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕
本文通過(guò)實(shí)例代碼給大家詳解Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕,對(duì)alertdialog退出按鈕相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
Android帶依賴(lài)樹(shù)的aar是如何生成的(推薦)
這篇文章主要介紹了Android打帶依賴(lài)樹(shù)的aar的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
詳解Android——藍(lán)牙技術(shù) 帶你實(shí)現(xiàn)終端間數(shù)據(jù)傳輸
藍(lán)牙技術(shù)在智能硬件方面有很多用武之地,本篇文章主要介紹了Android——藍(lán)牙技術(shù),實(shí)現(xiàn)兩個(gè)終端間數(shù)據(jù)的傳輸,有興趣的朋友可以了解一下。2016-12-12
Android 使用Vitamio打造自己的萬(wàn)能播放器(6)——在線播放(播放列表)
本文主要介紹Android Vitamino在線播放列表,這里給大家提供效果圖和實(shí)例代碼以便大家參考學(xué)習(xí),希望能幫助開(kāi)發(fā)Android視頻播放的朋友2016-07-07
Android編程實(shí)現(xiàn)ActionBar的home圖標(biāo)動(dòng)畫(huà)切換效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)ActionBar的home圖標(biāo)動(dòng)畫(huà)切換效果,涉及Android布局、樣式、Activity及菜單相關(guān)操作技巧,需要的朋友可以參考下2017-01-01

