網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Android通過SeekBar調(diào)節(jié)布局背景顏色的具體代碼,供大家參考,具體內(nèi)容如下
用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));
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
效果
原文鏈接:https://blog.csdn.net/kh971024/article/details/78418817
相關(guān)推薦
- 2022-05-15 Python?OpenCV使用dlib進(jìn)行多目標(biāo)跟蹤詳解_python
- 2023-02-12 Jupyter?Notebook運(yùn)行代碼無反應(yīng)問題及解決方法_python
- 2022-11-30 C語言中順序棧和鏈棧的定義和使用詳解_C 語言
- 2022-06-07 ASP.NET?Core依賴關(guān)系注入_基礎(chǔ)應(yīng)用
- 2022-03-01 箭頭函數(shù)的this 構(gòu)造函數(shù)的this 全局環(huán)境下的this各是什么
- 2022-10-08 ASP.NET泛型三之使用協(xié)變和逆變實(shí)現(xiàn)類型轉(zhuǎn)換_實(shí)用技巧
- 2022-09-21 Mac安裝軟件時(shí)提示已損壞的完美解決方法_相關(guān)技巧
- 2022-06-18 Android?Recyclerview實(shí)現(xiàn)左滑刪除功能_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支