瘋狂 Android 講義, 4/e – 拉霸(拖動)顯示元件(SeekBar) 改變/修改圖片透明度 範例 P133~P135
瘋狂 Android 講義, 4/e – 拉霸(拖動)顯示元件(SeekBar) 改變/修改圖片透明度 範例 P133~P135
資料來源:
https://github.com/daichangya/book/tree/master/android
https://pan.baidu.com/s/1d_xYJI0UQ_1tQzSj_V_NIg 提取码:70ch
XML Code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="240dp" android:src="@drawable/lijiang"/> <!-- 定义一个拖动条,并改变它的滑块外观 --> <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" android:progress="255" android:thumb="@mipmap/ic_launcher"/> <!-- 定义一个拖动条,并改变它的刻度图标 --> <SeekBar android:id="@+id/seekBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="10" android:tickMark="@drawable/tickmark"/> </LinearLayout>
JAVA Code
package org.crazyit.ui;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
/**
* Description:<br>
* 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a><br>
* Copyright (C), 2001-2020, Yeeku.H.Lee<br>
* This program is protected by copyright laws.<br>
* Program Name:<br>
* Date:<br>
*
* @author Yeeku.H.Lee kongyeeku@163.com<br>
* @version 1.0
*/
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView image = findViewById(R.id.image);
SeekBar seekBar = findViewById(R.id.seekbar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
// 当拖动条的滑块位置发生改变时触发该方法
@Override public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
{
// 动态改变图片的透明度
image.setImageAlpha(progress);
}
@Override public void onStartTrackingTouch(SeekBar bar)
{
}
@Override public void onStopTrackingTouch(SeekBar bar)
{
}
});
}
}