Android Color Configurator

1. Android Color settings

1. In the xml file

If you want to set the color, set the background property or other color properties directly. Set a color randomly, such as #000, and then click the color square on the left, and the color selector will pop up to select the color.

 

2. In java code

①Color.parseColor("#000");

tvShow.setBackgroundColor(Color.parseColor("#000"));

[Tip] You can configure the color value in the layout file, and then bring the color represented by "#" to the java code to use

 

②Color.BLACK uses the colors that come with the Color class, but they are all basic colors

tvShow.setBackgroundColor(Color.BLACK);

③ Define the Color resource file, referenced by R.color.myColor

tvShow.setBackgroundResource(R.color.myColor);

 

④Color.argb(a,r,g,b) method:

tvShow.setBackgroundColor(Color.argb(255, 255, 0, 0));

They are alpha, red (red), green (green), and blue (blue) four color values ​​(ARGB). Each number takes a value from 0-255, so a color can be represented by an integer. In order to run efficiently, Android uses an integer Color class instance to represent the color when coding.

【Tip】Pass in the corresponding transparency value, red value, green value, and blue value through this method for color configuration. So we can make a simple color configurator by this method.

 

Second, the color configurator case

1. [Effect]

The interface design is relatively rough, I hope everyone can learn the effect and optimize the interface.

2. [Project Structure]

3. [Code]

 ①activity_main.xml layout file

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context=".MainActivity"
 7     android:orientation="vertical">
 8 
 9     <TextView
10         android:text="请输入argb值:"
11         android:textSize="20sp"
12         android:textColor="#000"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:layout_margin="10dp"/>
16 
17     <LinearLayout
18         android:layout_width="match_parent"
19         android:layout_height="50dp"
20         android:orientation="horizontal">
21         <EditText
22             android:id="@+id/etA"
23             android:layout_width="0dp"
24             android:layout_weight="1"
25             android:layout_height="match_parent"
26             android:layout_margin="1dp"
27             android:hint="透明度(0-255)"
28             android:inputType="number"/>
29 
30         <EditText
31             android:id="@+id/etR"
32             android:hint="红(0-255)"
33             android:layout_width="0dp"
34             android:layout_weight="1"
35             android:layout_height="match_parent"
36             android:background="#f00"
37             android:layout_margin="1dp"
38             android:gravity="center"
39             android:inputType="number"/>
40     </LinearLayout>
41     <LinearLayout
42         android:layout_width="match_parent"
43         android:layout_height="50dp"
44         android:orientation="horizontal">
45         <EditText
46             android:id="@+id/etG"
47             android:hint="绿(0-255)"
48             android:layout_width="0dp"
49             android:layout_weight="1"
50             android:layout_height="match_parent"
51             android:background="#0f0"
52             android:layout_margin="1dp"
53             android:gravity="center"
54             android:inputType="number"/>
55 
56         <EditText
57             android:id="@+id/etB"
58             android:hint="蓝(0-255)"
59             android:layout_width="0dp"
60             android:layout_weight="1"
61             android:layout_height="match_parent"
62             android:background="#00f"
63             android:layout_margin="1dp"
64             android:gravity="center"
65             android:inputType="number"/>
66     </LinearLayout>
67 
68     <TextView
69         android:id="@+id/tv_show"
70         android:text="TextView"
71         android:layout_width="200dp"
72         android:layout_height="200dp"
73         android:background="#000"
74         android:layout_gravity="center"
75         android:layout_marginTop="20dp"
76         />
77 
78     <Button
79         android:id="@+id/btn"
80         android:text="确定配置"
81         android:layout_margin="20dp"
82         android:layout_width="match_parent"
83         android:layout_height="wrap_content" />
84 </LinearLayout>

[Tip] Hint attribute in EditText: This is the prompt text in the input box. inputType property: Set the text type input in the input box, here it is set to integer type

②MainActivity.java file

 1 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 2 
 3     private EditText etA;
 4     private EditText etR;
 5     private EditText etG;
 6     private EditText etB;
 7     private TextView tvShow;
 8     private Button btn;
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14 
15         initView();
16     }
17 
18     private void initView() {
19         etA = (EditText) findViewById(R.id.etA);
20         etR = (EditText) findViewById(R.id.etR);
21         etG = (EditText) findViewById(R.id.etG);
22         etB = (EditText) findViewById(R.id.etB);
23         tvShow = (TextView) findViewById(R.id.tv_show);
24         btn = (Button) findViewById(R.id.btn);
25 
26         btn.setOnClickListener(this);
27     }
28 
29     @Override
30     public void onClick(View v) {
31         switch (v.getId()) {
32             case R.id.btn:
33                 submit();
34                 break;
35         }
36     }
37 
38     private void submit() {
39         // validate
40         if (!etA.getText().equals("")&&!etB.getText().equals("")
41                 &&!etR.getText().equals("")&&!etG.getText().equals("" )) {
 42              // Check whether the value entered by the user is empty. Avoid null character cannot be converted to int exception
 43              int et_a = Integer.parseInt(etA.getText().toString());
 44              int et_r = Integer.parseInt(etR.getText().toString());
 45              int et_g = Integer.parseInt(etG.getText().toString());
 46              int et_b = Integer.parseInt(etB.getText().toString());
 47              tvShow.setBackgroundColor(Color.argb(et_a, et_r, et_g, et_b ));
 48          } else {
 49              Toast.makeText( this, "The entered value cannot be empty" , Toast.LENGTH_SHORT).show();
 50          }
 51      }
 52 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324647843&siteId=291194637