写这个是在项目迭代的时候遇到的,一开始都是圆形的头像,今天突然说~要把所有的头像改成圆角矩形....
我一开始没注意到这个,就把所有有关头像调用的部分整合了一下
如图~
那就不多bb了。直接上代码
这是一个形状的分类?
public class HeaderShap {
/*
* 圆形,上下文,文本组件,图片链接
* */
public void ShapCircle(Context context, TextView tv_default, ImageView iv_avatar, String avatar) {
RequestOptions options = RequestOptions.bitmapTransform(new CropCircleTransformation());
glide(context,tv_default,iv_avatar,avatar,options);
}
/*
* 圆角矩形,上下文,文本组件,图片链接
* */
public void ShapRounded(Context context, TextView tv_default, ImageView iv_avatar, String avatar) {
//RequestOptions options = RequestOptions.bitmapTransform(new RoundedCorners(20));
//第一个是上下文,第二个是圆角的弧度
RequestOptions options = new RequestOptions()
.transform(new GlideRoundTransform(context,20));
glide(context,tv_default,iv_avatar,avatar,options);
}
//共同部分的代码封装一下
private void glide(Context context, TextView tv_default, ImageView iv_avatar, String avatar,RequestOptions options){
Glide.with(context)
.load(avatar)
//.placeholder(R.drawable.ic_default_image)
.error(R.drawable.default_avatar_background)
.apply(options)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
tv_default.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource
dataSource, boolean isFirstResource) {
tv_default.setVisibility(View.INVISIBLE);
return false;
}
})
.into(iv_avatar);
}
/*
* 标准矩形,上下文,文本组件,图片链接
* */
public void ShapRectangle(Context context, TextView tv_default, ImageView iv_avatar, String avatar) {
RequestOptions options = RequestOptions.bitmapTransform(new RoundedCorners(0));
glide(context, tv_default, iv_avatar, avatar, options);
}
}
继承glide自定义的样式,不用这个设置矩形的话,会被缩放,显示的不是矩形,会显示成圆角的缩放图,如果原图是长方形,那就是长方行圆角~这么讲能理解吧
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundTransform(Context context) {
this(context, 4);
}
public GlideRoundTransform(Context context, int dp) {
super();
this.radius = dp;
//this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, bitmap);
}
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
public String getId() {
return getClass().getName() + Math.round(radius);
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
}
?默认的圆角图,颜色自己改一下,我是根据主题色来的
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid android:color="@color/color_theme"/>
<corners android:radius="10dp"/>
<stroke
android:width="0.5dp"
android:color="@color/color_theme"/>
</shape>
</item>
</layer-list>
最后就是调用了
第一个参数是上下文,第二个是默认会显示的昵称,第三个是显示图片的控件,第四个是图片地址(如果没有,显示的就是默认的)
HeaderShap shap = new HeaderShap();
shap.ShapRounded(context,tv_udefault_avatar,iv_avatar,avatar);
最后头像的xml也发一下吧,这个其实很简单,imagview显示的是默认,昵称和将要显示的图片都暂时隐藏,没有图片地址的时候,昵称会显示,就和钉钉那种差不多,这篇文章,上半年我是写过,只是没封装,简单使用,各位可以向上翻看一下~
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_avatar"
android:background="#00000000"
android:layout_width="40dp"
android:layout_height="40dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="40dp"
android:layout_height="40dp">
<TextView
android:id="@+id/user_id"
android:layout_width="0dp"
android:layout_height="0dp"
tools:ignore="Suspicious0dp" />
<ImageView
android:padding="4dp"
android:id="@+id/iv_avatar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/default_avatar_background"
/>
<TextView
android:visibility="invisible"
android:id="@+id/tv_default_avatar"
android:text="京怀"
android:textColor="@color/white"
android:textSize="11dp"
android:gravity="center"
android:layout_marginTop="-40dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
|