溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android怎么通過(guò)SeekBar調(diào)節(jié)布局背景顏色

發(fā)布時(shí)間:2022-04-25 13:49:45 來(lái)源:億速云 閱讀:255 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Android怎么通過(guò)SeekBar調(diào)節(jié)布局背景顏色”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Android怎么通過(guò)SeekBar調(diào)節(jié)布局背景顏色”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

用RGB設(shè)置布局背景顏色的方法

relativeLayout.setBackgroundColor(Color.rgb(r,g,b));

布局文件

<RelativeLayout 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"
    android:id="@+id/RelativeLayout"
    tools:context="com.example.konghao.adjustcolor.MainActivity">
 
 
    <LinearLayout
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
 
        <TextView
            android:id="@+id/int_R"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
 
        <SeekBar
            android:id="@+id/R"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
 
        <TextView
            android:id="@+id/int_G"
            android:layout_marginTop="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
 
        <SeekBar
            android:id="@+id/G"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
 
        <TextView
            android:id="@+id/int_B"
            android:layout_marginTop="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
 
        <SeekBar
            android:id="@+id/B"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
 
    </LinearLayout>
 
</RelativeLayout>

Main活動(dòng)

public class MainActivity extends Activity {
 
    private RelativeLayout relativeLayout;
    private SeekBar color_R,color_G,color_B;
    private static int r = 0,g = 0,b = 0;
    private TextView int_r,int_g,int_b;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
 
        color_R = (SeekBar) findViewById(R.id.R);
        color_G = (SeekBar) findViewById(R.id.G);
        color_B = (SeekBar) findViewById(R.id.B);
        int_r = (TextView) findViewById(R.id.int_R);
        int_g = (TextView) findViewById(R.id.int_G);
        int_b = (TextView) findViewById(R.id.int_B);
 
        color_R.setMax(255);
        color_G.setMax(255);
        color_B.setMax(255);
 
        color_B.setProgress(0);
        color_G.setProgress(0);
        color_B.setProgress(0);
 
        color_R.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar,int progress, boolean fromUser) {
            }
 
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
 
            }
 
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                r = seekBar.getProgress();
                String int_color_r = "R:" + String.valueOf(r);
                int_r.setText(int_color_r);
                relativeLayout.setBackgroundColor(Color.rgb(r,g,b));
            }
        });
 
        color_G.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean fromUser) {
            }
 
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
 
            }
 
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                g = seekBar.getProgress();
                String int_color_g = "G:" + String.valueOf(g);
                int_g.setText(int_color_g);
                relativeLayout.setBackgroundColor(Color.rgb(r,g,b));
            }
        });
 
        color_B.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean fromUser) {
            }
 
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
 
            }
 
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                b = seekBar.getProgress();
                String int_color_b = "B:" + String.valueOf(b);
                int_b.setText(int_color_b);
                relativeLayout.setBackgroundColor(Color.rgb(r,g,b));
            }
        });
    }
}

效果

Android怎么通過(guò)SeekBar調(diào)節(jié)布局背景顏色

讀到這里,這篇“Android怎么通過(guò)SeekBar調(diào)節(jié)布局背景顏色”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI