当前位置: 移动技术网 > IT编程>移动开发>Android > Android中使用SeekBar拖动条实现改变图片透明度(代码实现)

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

2020年03月09日  | 移动技术网IT编程  | 我要评论

云惜吧,爱华音响,wwe摔跤

场景

效果

实现

将布局改为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拖动条实现改变图片透明度(代码实现),希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网