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

實(shí)現(xiàn)
將布局改為LinearLayout,并通過android:orientation="vertical">設(shè)置為垂直布局,然后添加一個ImageView和SeekBar,并分別添加id屬性。
其中SeekBar,添加最大值為255.因?yàn)橥该鞫鹊淖畲笾稻褪?55
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拖動條實(shí)現(xiàn)改變圖片透明度(代碼實(shí)現(xiàn)),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Android中利用xml文件布局修改Helloworld程序
這篇文章主要介紹了Android中利用xml文件布局修改Helloworld程序 的相關(guān)資料,需要的朋友可以參考下2016-07-07
詳解Android數(shù)據(jù)存儲—使用SQLite數(shù)據(jù)庫
本篇文章主要介紹了詳解Android數(shù)據(jù)存儲—使用SQLite數(shù)據(jù)庫,具有一定的參考價值,有興趣的可以了解一下。2017-03-03
Android中RecyclerView的item寬高問題詳解
RecyclerView出現(xiàn)已經(jīng)有一段時間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了關(guān)于Android中RecyclerView的item寬高問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼
本篇文章主要介紹了Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
android POST數(shù)據(jù)遇到的UTF-8編碼(亂碼)問題解決辦法
這篇文章主要介紹了android POST數(shù)據(jù)遇到的UTF-8編碼(亂碼)問題解決辦法,需要的朋友可以參考下2014-04-04
Android入門之SubMenu的實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)SubMenu子菜單的效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Android有一定的幫助,感興趣的可以了解一下2022-11-11
Android程序開發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼
流行的應(yīng)用的導(dǎo)航一般分為兩種,一種是底部導(dǎo)航,一種是側(cè)邊欄。本文給大家介紹Fragment實(shí)現(xiàn)底部導(dǎo)航欄,對Fragment實(shí)現(xiàn)底部導(dǎo)航欄相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-03-03

