• Android中使用SeekBar拖动条实现改变图片透明度


    场景

    效果

    注:

    博客:
    https://blog.csdn.net/badao_liumang_qizhi
    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    实现

    将布局改为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) {
    
                }
            });
        }
    }
  • 相关阅读:
    RecSys Challenge 2015
    Python 多行注释
    编译型语言与解释型语言
    vs2012 提示 未能正确加载 "Visual C++ Language Manager Package" 包
    人工智能 VS 机器学习 VS 深度学习
    CV-视频分析:静态背景下的运动检测
    消费者做出购买决策的流程
    Fat jar用途
    Eclipse中打包插件Fat Jar的安装与使用
    Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12158629.html
Copyright © 2020-2023  润新知