在做电话本或者其他应用时,显示联系人头像的策略一般是这样的:先判断是否有头像图片,如果有,则直接显示图片;如果没有,则显示联系人的名字的第一个字,将这个文字作为头像,并添加背景颜色。如果设置的文字的长度大于1,则具有clip效果,即超出圆形的部分将被clip掉。
compile 'cn.carbs.android:AvatarImageView:1.0.4'
暂时不支持wrap_content模式
使用shader + matrix的方式显示圆形图片,其中圆形是有shader产生的,而居中的调整方式是由matrix设置的。
(1)设置图片或者文字的方法:
AvatarImageView aiv = (AvatarImageView) this.findViewById(R.id.aiv);
//设置图像:
aiv.setImageResource(R.drawable.id_014);
//或者:
aiv.setDrawable(drawable);
//或者:
aiv.setBitmap(bitmap);
//或者:
aiv.setImageDrawable(drawable);
//设置文字:
aiv.setTextAndColor("安", AvatarImageView.COLORS[0]);//直接设置颜色,如果设置的文字为多个字符,则会具有clip效果,单个字符没有clip效果
//或者:
aiv.setTextAndColorSeed("安","安卓");//“安卓”字样作为产生backgroundcolor的seed
(2)与Glide的结合:
Glide
.with(activity)
.load(picurl)
.centerCrop()
.crossFade()
.into(aiv);
1.添加依赖
compile 'cn.carbs.android:AvatarImageView:1.0.3'
2.layout文件中添加此view
<cn.carbs.android.avatarimageview.library.AvatarImageView
android:id="@+id/item_avatar"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
app:aiv_BoarderWidth="2dp"//边框宽度
app:aiv_ShowBoarder="false"//是否显示边框
app:aiv_TextSizeRatio="0.4"/>//文字大小与原型直径大小的比例
AvatarImageView on Android platform can display circle text or circle image
not support wrap_content pattern temporarily
(1)How to set image or text
AvatarImageView aiv = (AvatarImageView) this.findViewById(R.id.aiv);
//set image:
aiv.setImageResource(R.drawable.id_014);
//or:
aiv.setDrawable(drawable);
//or:
aiv.setBitmap(bitmap);
//or:
aiv.setImageDrawable(drawable);
//set text:
aiv.setTextAndColor("Android", AvatarImageView.COLORS[0]);//set backgroundcolor directly, if the text's length > 1, will have clip effect
//or
aiv.setTextAndColorSeed("Android","Android platform");//"Android platform" as the backgroundcolor seed
(2)use with Glide:
Glide
.with(activity)
.load(picurl)
.centerCrop()
.crossFade()
.into(aiv);
1.add dependence
compile 'cn.carbs.android:AvatarImageView:1.0.4'
2.add into xml layout file
<cn.carbs.android.avatarimageview.library.AvatarImageView
android:id="@+id/item_avatar"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
app:aiv_BoarderWidth="2dp"//
app:aiv_ShowBoarder="false"//
app:aiv_TextSizeRatio="0.4"/>//equals textsize divides circle diameter
Copyright 2016 Carbs.Wang (AvatarImageView)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.