場景
效果
實現
將布局改為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:andro 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: android:layout_height="250dp" android:src="@drawable/dog" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android: 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拖動條實現改變圖片透明度(代碼實現),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
新聞熱點
疑難解答
圖片精選