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

實(shí)現(xiàn)
將布局改為L(zhǎng)inearLayout,并通過(guò)android:orientation="vertical">設(shè)置為垂直布局,然后添加一個(gè)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>
然后來(lái)到Activity
分別通過(guò)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拖動(dòng)條實(shí)現(xiàn)改變圖片透明度(代碼實(shí)現(xiàn)),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Android實(shí)現(xiàn)靜音檢測(cè)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)靜音檢測(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android中利用xml文件布局修改Helloworld程序
這篇文章主要介紹了Android中利用xml文件布局修改Helloworld程序 的相關(guān)資料,需要的朋友可以參考下2016-07-07
解決Android studio模擬器啟動(dòng)失敗的問題
這篇文章主要介紹了Android studio模擬器啟動(dòng)失敗的問題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
詳解Android數(shù)據(jù)存儲(chǔ)—使用SQLite數(shù)據(jù)庫(kù)
本篇文章主要介紹了詳解Android數(shù)據(jù)存儲(chǔ)—使用SQLite數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03
Android中RecyclerView的item寬高問題詳解
RecyclerView出現(xiàn)已經(jīng)有一段時(shí)間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了關(guān)于Android中RecyclerView的item寬高問題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08
Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼
本篇文章主要介紹了Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧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ì),對(duì)我們學(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)航欄,對(duì)Fragment實(shí)現(xiàn)底部導(dǎo)航欄相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03

