Android中使用CircleImageView和Cardview制作圆形头像的方法
作者:zerocoin 时间:2022-04-19 05:41:35
圆形头像在我们的日常使用的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制作圆形头像的方法网站的支持!
来源:http://www.cnblogs.com/xurui1995/archive/2016/08/31/5827779.html
标签:circleimageview,cardview
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#使用Json.Net进行序列化和反序列化及定制化
2023-07-10 19:11:35
Java的Swing编程中使用SwingWorker线程模式及顶层容器
2021-09-09 08:45:06
Java:泛型知识知多少
2023-11-24 23:08:44
![](https://img.aspxhome.com/file/2023/8/60188_0s.jpg)
Java synchronized轻量级锁实现过程浅析
2022-05-08 07:28:55
![](https://img.aspxhome.com/file/2023/6/84606_0s.png)
详解C#中使用对象或集合的初始值设定项初始化的操作
2021-12-25 20:04:52
JAVA面试题 简谈你对synchronized关键字的理解
2022-09-17 17:06:05
Java实现为Word每一页设置不同图片水印的效果
2023-01-30 07:10:04
![](https://img.aspxhome.com/file/2023/0/85830_0s.jpg)
android 多点触摸图片缩放的具体实现方法
2023-03-14 05:49:31
工作中禁止使用Executors快捷创建线程池原理详解
2021-11-24 20:55:48
![](https://img.aspxhome.com/file/2023/9/75439_0s.png)
Java Swing JPanel面板的使用方法
2023-02-13 12:04:49
![](https://img.aspxhome.com/file/2023/2/80892_0s.png)
教你如何监控 Java 线程池运行状态的操作(必看)
2023-02-09 04:34:13
![](https://img.aspxhome.com/file/2023/9/63079_0s.jpg)
使用@Order控制配置类/AOP/方法/字段的加载顺序详解
2023-09-05 17:34:35
![](https://img.aspxhome.com/file/2023/1/58421_0s.png)
java实现基于SMTP发送邮件的方法
2023-04-06 12:22:07
23种设计模式(19)java责任链模式
2021-10-19 15:04:00
![](https://img.aspxhome.com/file/2023/7/89487_0s.jpg)
Flutter应用集成极光推送的实现示例
2023-06-24 03:51:04
![](https://img.aspxhome.com/file/2023/2/90172_0s.jpg)
Java LinkedHashMap深入分析源码
2022-07-31 03:05:43
![](https://img.aspxhome.com/file/2023/2/62092_0s.png)
Java字节与字符流永久存储json数据
2022-12-27 21:10:13
详解如何让Spring MVC显示自定义的404 Not Found页面
2023-12-12 15:48:47
![](https://img.aspxhome.com/file/2023/1/72571_0s.jpg)
spring profile 多环境配置管理详解
2023-01-23 17:53:58
pom文件中${project.basedir}的使用
2021-12-24 04:24:11
![](https://img.aspxhome.com/file/2023/4/63494_0s.jpg)