Android中使用CircleImageView和Cardview制作圓形頭像的方法
圓形頭像在我們的日常使用的app中很常見,因為圓形的頭像比較美觀.
使用圓形圖片的方法可能有我們直接將圖片裁剪成圓形再在app中使用,還有就是使用自定義View對我們設(shè)置的任何圖片自動裁剪成圓形。
效果圖:
這里使用github上CircleImageView
github:https://github.com/hdodenhof/CircleImageView
CardView顧名思義卡片式的View,CardView繼承的是FrameLayout,所以擺放內(nèi)部控件的時候需要注意一下
可以設(shè)置陰影,圓角,等等
這里的CircleImageView還可以為頭像設(shè)置描邊。
我們新建一個項目,選擇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.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>
接下來在MainActivity添加代碼,使用我們的CardView
1. 添加成員變量和數(shù)據(jù)源
private RecyclerView mRecyclerView; private String[] data={"2014 年,隨著 Google 推出了全新的設(shè)計語言 Material Design,還迎來了新的 Android 支持庫 v7,其中就包含了 Material Design 設(shè)計語言中關(guān)于 Card 卡片概念的實現(xiàn) —— CardView。" ,"經(jīng)歷了相當(dāng)長的一段時間相信許多 Android 開發(fā)者都已經(jīng)應(yīng)用了這個控件,現(xiàn)在才寫這篇文章可能有點晚,但對于剛剛開始使用的開發(fā)者以及其他已經(jīng)使用了一段時間但做出來效果不好的同學(xué)應(yīng)該能幫上點小忙。" ,"Google 在 Android Lollipop 中引入了 Material Design 設(shè)計中的陰影(Elevation)和 Z 軸位移,其目的就是突出界面中不同元素之間的層次關(guān)系" ,"明年夏天,自由球員布雷克-格里芬可能重返俄克拉何馬城與拉塞爾-威斯布魯克聯(lián)手。如果實現(xiàn),雷霆隊能真正意義上地威脅勇士隊嗎?"};
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制作圓形頭像的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Android實現(xiàn)瘋狂連連看游戲之加載界面圖片和實現(xiàn)游戲Activity(四)
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)瘋狂連連看游戲之加載界面圖片和實現(xiàn)游戲Activity,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android使用ContentProvider初始化SDK庫方案小結(jié)
這篇文章主要介紹了Android使用ContentProvider初始化SDK庫方案總結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Android 照片選擇區(qū)域功能實現(xiàn)示例
這篇文章主要介紹了Android 照片選擇區(qū)域功能實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Android Studio 中的Gradle構(gòu)建系統(tǒng)示例
這篇文章主要介紹了Android Studio 中的Gradle構(gòu)建系統(tǒng)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Android+Flutter實現(xiàn)彩虹圖案的繪制
彩虹,是氣象中的一種光學(xué)現(xiàn)象,當(dāng)太陽光照射到半空中的水滴,光線被折射及反射,在天空上形成拱形的七彩光譜。接下來,我們就自己手動繪制一下彩虹圖案吧2022-11-11