利用进度条改变图片透明度【最终版】

1.AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.administrator.android">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:supportsRtl="true"
10         android:theme="@style/AppTheme">
11         <activity android:name=".MainActivity"></activity>
12         <activity android:name=".TestActivity4">
13             <intent-filter>
14                 <action android:name="android.intent.action.MAIN" />
15 
16                 <category android:name="android.intent.category.LAUNCHER" />
17             </intent-filter>
18         </activity>
19         <activity android:name=".Main2Activity"></activity>
20         <activity android:name=".Main3Activity">
21         </activity>
22     </application>
23 
24 </manifest>

2.activity_test4.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6   tools:context="com.example.administrator.android.TestActivity4"
 7     android:orientation="vertical">
 8 
 9 <ImageView
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:src="@drawable/f8"
13         android:id="@+id/iv_1"/>
14     <SeekBar
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:id="@+id/se_2"
18         android:max="255"
19         />
20 </LinearLayout>

3.TestActivity4.java

 1 package com.example.administrator.android;
 2 
 3 import android.app.AlertDialog;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.util.Log;
 7 import android.view.View;
 8 import android.widget.ImageView;
 9 import android.widget.ProgressBar;
10 import android.widget.SeekBar;
11 import android.widget.Toast;
12 
13 public class TestActivity4 extends AppCompatActivity {
14 
15 ImageView iv_1;
16     SeekBar se_2;
17 iv_1 = (ImageView)findViewById(R.id.iv_1);
18         se_2 = (SeekBar)findViewById(R.id.se_2);
19 
20         iv_1.setAlpha(0f);
21 
22             //给拖动条添加监听器
23         se_2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
24             @Override
25             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
26                 //设置图片的透明度
27                 //带中划线的代码是已经废弃的代码方法。【仍可以使用但不建议使用的代码】
28 //                iv_1.setAlpha(0.1F);
29 
30 //红色波浪线为版本问题 不支持API为16以下运行
31 //              值的范围是0-255
32                 iv_1.setAlpha(progress/255.0f); //float型数值转换
33             }
34 
35             @Override
36             public void onStartTrackingTouch(SeekBar seekBar) {
37 
38             }
39 
40             @Override
41             public void onStopTrackingTouch(SeekBar seekBar) {
42 
43             }
44         });
45 }
46 }
原文地址:https://www.cnblogs.com/TENOKAWA/p/5482406.html