方法1:Outline getView(R.id.image_view_1).setClipToOutline(true);getView(R.id.image_view_1).setOutlineProvider(new ViewOutl
getView(R.id.image_view_1).setClipToOutline(true);getView(R.id.image_view_1).setOutlineProvider(new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), radius); }});
1) 要设置圆角的View.setClipToOutline(true)
2)要设置圆角的View.setOutlineProvider(ViewOutlineProvider provider)
ViewOutlineProvider 只有1个抽象方法,通过Outline.setRoundRect设置圆角:
public abstract void getOutline(View view, Outline outline);
1)对View设置,可View、ViewGroup
2)对4个角同时设置,不能单独设置单个角
1)对需要设置圆角的View,使用CardView包裹
2)对CardView设置,设置圆角、去除阴影和padding
app:cardCornerRadius="@dimen/dp_24"app:cardElevation="0dp"app:cardUseCompatPadding="false"
1)对View设置,可View、ViewGroup
2)对4个角同时设置,不能单独设置单个角
1)自定义shape,设置radius
1)对生成的图片进行设置
2)可对4角同时设置,也可对4角单独设置
//GradientDrawable if(true) { //同时设置四个角 GradientDrawable drawable = new GradientDrawable(); drawable.setShape(GradientDrawable.RECTANGLE); drawable.setCornerRadius(radius); drawable.setColor(0xFFEEA2A4); ((ImageView) getView(R.id.image_view_4)).setImageDrawable(drawable); } else { //单独设置四个角 GradientDrawable drawable = new GradientDrawable(); drawable.setShape(GradientDrawable.RECTANGLE); drawable.setColor(0xFFEEA2A4); float[] radii = new float[]{ radius, radius, 0F, 0F, 0F, 0F, radius, radius }; drawable.setCornerRadii(radii); ((ImageView) getView(R.id.image_view_4)).setImageDrawable(drawable); }
1)对GradientDrawable设置圆角drawable.setCornerRadius(radius)
2)或者对GradientDrawable设置float[],对4角单独设置
float[] radii = new float[]{ radius, radius, 0F, 0F, 0F, 0F, radius, radius };drawable.setCornerRadii(radii);
1)对图片进行设置
2)可对4角同时设置,也可对4角单独设置
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), ResourceUtils.drawable2Bitmap(ResourceUtils.getDrawable(R.drawable.drawable_90_corner_2)));drawable.setCornerRadius(radius);((ImageView) getView(R.id.image_view_4_2)).setImageDrawable(drawable);
1)通过RoundedBitmapDrawableFactory创建RoundedBitmapDrawable,提供了三个实现方法:
public static RoundedBitmapDrawable create(@NonNull Resources res, @Nullable Bitmap bitmap)public static RoundedBitmapDrawable create(@NonNull Resources res, @NonNull String filepath)public static RoundedBitmapDrawable create(@NonNull Resources res, @NonNull InputStream is)
区别在于第2个参数:Bitmap、filepath、InputStream
2)对RoundedBitmapDrawable设置setCornerRadius(radius)
1)对图片进行设置
2)对4个角同时设置,不能单独设置单个角
//Glide if(true) { //同时设置四个角 Glide.with(this) .asDrawable() .load(R.drawable.drawable_90_corner) .transfORM(new RoundedCorners(radius)) .into((ImageView) getView(R.id.image_view_5)); } else { //单独设置四个角 Glide.with(this) .asDrawable() .load(R.drawable.drawable_90_corner) .transform(new GranularRoundedCorners(radius, radius, radius, radius)) .into((ImageView) getView(R.id.image_view_5)); }
1)引入Glide,使用Glide加载图片
2)设置transform(new RoundedCorners(radius))或者transform(new GranularRoundedCorners(radius, radius, radius, radius))
1)对图片进行设置
2)可对4角同时设置,也可对4角单独设置,只是方法不同
AndroidJavaKotlin: 深度学习Android、Java、Kotlin (gitee.com)
来源地址:https://blog.csdn.net/zhiyuan263287/article/details/127624728
--结束END--
本文标题: Android设置圆角看着一篇文章就够了
本文链接: https://lsjlt.com/news/384640.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0