欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年09月01日 11:20:53   作者:zerocoin  
這篇文章主要介紹了Android中使用CircleImageView和Cardview制作圓形頭像的方法,簡單介紹了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)文章

最新評論