ImageView 使用 setAlpha() 设置控件透明

长话短说,为了是ImageView 整个控件变得透明或者半透明, 很多小伙伴会设置mImageView.setAlpha()这个函数但是ImageView 有三个方法,一是, 参数为int类型titleHeadIv.setAlpha(0~255);二是,参数为float类型titleHeadIv.setAlpha(0f~1f);三是,setImage
摘要由CSDN通过智能技术生成

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">长话短说,为了是ImageView 整个控件变得透明或者半透明, 很多小伙伴会设置mImageView.setAlpha()这个函数</span>

但是ImageView 有三个方法,


一是, 参数为int类型

titleHeadIv.setAlpha(0~255);


二是,参数为float类型

titleHeadIv.setAlpha(0f~1f);


三是,setImageAlpha(int),这个方法是较高api的, 不做使用


当我们使用第一个方法时, 对ImageView 是无效的, 不管设置什么值都显示不出效果。当时被这个方法坑死,怎么设置都无法使用。


当使用第二个方法setAlpha(0.5f),参数必须为0~1 的小数, 大于1就表示100%的透明度,

当设置参数为0.5 的时候,透明度是一半。成功解决ImageView的透明效果。

titleHeadIv.setAlpha(0.5f);


若想改变其他带有background 的控件也可以使用setAlpha方法 : 

如: 半透明效果

Drawable frontd = titleView.getBackground();

float  f = 0.5;


frontd.setAlpha

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道了。下面是您所需要的代码: XML文件: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher_background" /> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" android:layout_alignParentBottom="true"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_weight="1" android:max="100" android:progress="0" /> <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:numStars="5" android:stepSize="1.0" /> </LinearLayout> </LinearLayout> ``` Java文件: ``` public class MainActivity extends AppCompatActivity { private ImageView imageView; private SeekBar seekBar; private ProgressBar progressBar; private RatingBar ratingBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = findViewById(R.id.imageView); seekBar = findViewById(R.id.seekBar); progressBar = findViewById(R.id.progressBar); ratingBar = findViewById(R.id.ratingBar); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { imageView.setAlpha((float) progress / 255); } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Toast.makeText(MainActivity.this, "Rating: " + rating, Toast.LENGTH_SHORT).show(); } }); } } ``` 运行效果: ![image](https://user-images.githubusercontent.com/38335321/122733356-37ccff00-d2a8-11eb-9a0f-3c9843dc5d5a.png) 希望这可以帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值