Android中使用SeekBar拖動條實現(xiàn)改變圖片透明度(代碼實現(xiàn))
場景
效果

實現(xiàn)
將布局改為LinearLayout,并通過android:orientation="vertical">設(shè)置為垂直布局,然后添加一個ImageView和SeekBar,并分別添加id屬性。
其中SeekBar,添加最大值為255.因為透明度的最大值就是255
android:max="255"
并設(shè)置當(dāng)前值就是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的進(jìn)度條改變事件中給imageView設(shè)置透明度。
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) {
}
});
}
}
總結(jié)
以上所述是小編給大家介紹的Android中使用SeekBar拖動條實現(xiàn)改變圖片透明度(代碼實現(xiàn)),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Android AbsoluteLayout和RelativeLayout布局詳解
本文主要講解Android AbsoluteLayout和RelativeLayout布局,這里整理了相關(guān)資料,并附示例代碼和效果圖,有興趣的小伙伴可以參考下2016-08-08
Android使用ViewPager實現(xiàn)圖片滑動預(yù)覽效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實現(xiàn)圖片滑動預(yù)覽效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Android切換至SurfaceView時閃屏(黑屏閃一下)以及黑屏移動問題的解決方法
本文主要介紹了Android切換至SurfaceView時閃屏(黑屏閃一下)以及黑屏移動問題的解決方法。具有一定的參考作用,下面跟著小編一起來看下吧2017-01-01

