当前位置: 移动技术网 > IT编程>移动开发>Android > Android中使用CircleImageView和Cardview制作圆形头像的方法

Android中使用CircleImageView和Cardview制作圆形头像的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

圆形头像在我们的日常使用的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.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,记得导包

<?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,记得导包

<?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. 添加成员变量和数据源

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>{
@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()方法里设置adapter

mrecyclerview= (recyclerview) findviewbyid(r.id.rv);
mrecyclerview.setlayoutmanager(new linearlayoutmanager(this));
mrecyclerview.setadapter(new myadapter());

以上所述是小编给大家介绍的android中使用circleimageview和cardview制作圆形头像的方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网