网站首页 网站建设 IT知识 IT知识教程 Android中使用SeekBar拖动条实现改变图片透明度(代码实现)

Android中使用SeekBar拖动条实现改变图片透明度(代码实现)

2021-05-20 21:25:38

场景

效果

实现

将布局改为LinearLayout,并通过android:orientation="vertical">设置为垂直布局,然后添加一个ImageView和SeekBar,并分别添加id属性。

其中SeekBar,添加最大值为255.因为透明度的最大值就是255

android:max="255"

并设置当前值就是255

android:progress="255"

完整xml代码

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SeekBarActivity" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:id="@+id/image" android:layout_height="250dp" android:src="@drawable/dog" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekBar" android:max="255" android:progress="255" /></LinearLayout>

然后来到Activity

分别通过id获取到ImageView和SeekBar

然后在seekBar的进度条改变事件中给imageView设置透明度。

package com.badao.relativelayouttest;import androidx.annotation.RequiresApi;import androidx.appcompat.app.AppCompatActivity;import android.os.Build;import android.os.Bundle;import android.widget.ImageView;import android.widget.SeekBar;public class SeekBarActivity extends AppCompatActivity { private SeekBar seekBar; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seek_bar); imageView = (ImageView) findViewById(R.id.image); seekBar = (SeekBar) findViewById(R.id.seekBar); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { imageView.setImageAlpha(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); }}

总结

以上所述是小编给大家介绍的Android中使用SeekBar拖动条实现改变图片透明度(代码实现),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。 如涉及版权问题,请提交至online#300.cn邮箱联系删除。

以上产品还未完全满足我的所有需求,在下方提交我的专属需求
我的专属需求:
*手机号:
*验证码:
img
咨询报价
现在咨询
img

在线咨询

建站在线咨询

img

微信咨询

扫一扫添加
动力姐姐微信

img
img

TOP