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

场景 效果 实现 将布局改为linearlayout,并通过android:orientation=”vertical”>设置为垂直布局,然后添加一个imageview

场景

效果

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

实现

将布局改为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="https://schemas.android.com/apk/res/android"    xmlns:app="https://schemas.android.com/apk/res-auto"    xmlns:tools="https://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拖动条实现改变图片透明度(代码实现),希望对大家有所帮助

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/addevelopment/899881.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐