android ImageView中setBackground相关属性的区别

最近在写android程序时,遇到以下情形:

底部导航栏要用到4个ImageView来实现,在点击不同的ImageView是,要对其背景色进行设置,在使用ImageView关于setBackground相关属性设置时,会出现异常,从而导致android程序崩溃试了好多办法都不行,于是乎,就到android 官方网站查API,才把问题解决。写一篇博客记下来,防止以后再忘记。


首先看一下android官方给的文档:

public void setBackground (Drawable background)
Added in  API level 16

Set the background to a given Drawable, or remove the background. If the background has padding, this View's padding is set to the background's padding. However, when a background is removed, this View's padding isn't touched. If setting the padding is desired, please use setPadding(int, int, int, int).

Parameters
background The Drawable to use as the background, or null to remove the background

public void setBackgroundColor (int color)
Added in  API level 1

Sets the background color for this view.

Parameters
color the color of the background

public void setBackgroundDrawable (Drawable background)
Added in  API level 1

This method was deprecated in API level 16.
use setBackground(Drawable) instead


public void setBackgroundResource (int resid)
Added in  API level 1

Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

Related XML Attributes
Parameters
resid The identifier of the resource.

从官方给出的API可以得出以下结论:

1.setBackgroundColor(),setBackgroundDrawable(),setBackgroundResource()三个方法从API 1开始就已经存在,而setBackgrouond()是从API 16才引进来的(Android 4.1开始),目的是为了取代setBackgroundDrawable().

所以如果开发的android API 在16+,则推荐使用setBackground().

2.三者区别:

setBackgroundColor(int color)

看其参数和说明可知,要传入一个颜色的ID值,可以使在colors.xml中定义的资源,也可以使用ARGB模式的十六进制数值来表示,如0xFFFF0000(红色);

setBackgroundResource(int resid)

这个方法在XML中有对应的属性android:background,可以理解为给ImageView设置背景,参数为资源的ID。

setBackground(Drawable background)

这个表示给ImageView设置背景,参数为资源,一般为图片,与其他不同的是,当背景有内边距的时候,那么View的内边距被设置和背景的内边距一样,当背景不存在的时候,View的背景也不存在。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android ImageView 圆角可以通过以下两种方式实现: 1. 使用 XML 属性设置圆角 在 ImageView 的 XML 布局文件,可以使用以下属性设置圆角: ``` android:background="@drawable/your_image" android:scaleType="centerCrop" android:clipToOutline="true" android:outlineProvider="background" ``` 其,`your_image` 是你要显示的图片资源。`scaleType` 属性设置图片的缩放方式,`clipToOutline` 属性设置是否裁剪视图的轮廓,`outlineProvider` 属性设置视图的轮廓提供者,这里使用 `background` 表示使用视图的背景作为轮廓。 然后,在 `res/drawable` 目录下创建一个 XML 文件,命名为 `your_image.xml`,内容如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp" /> </shape> ``` 其,`radius` 属性设置圆角的半径大小。 2. 使用代码设置圆角 在 Java 代码,可以使用以下方法设置圆角: ``` ImageView imageView = findViewById(R.id.image_view); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_image); RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); roundedBitmapDrawable.setCornerRadius(10); imageView.setImageDrawable(roundedBitmapDrawable); ``` 其,`your_image` 是你要显示的图片资源。`RoundedBitmapDrawableFactory.create()` 方法创建一个圆角位图,`setCornerRadius()` 方法设置圆角的半径大小,`setImageDrawable()` 方法设置 ImageView 的显示内容为圆角位图。 以上两种方法都可以实现 ImageView 圆角的效果,具体使用哪种方式取决于你的需求和习惯。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值