|
看了一些APP後,自己也想做一個有調色盤的APP
這模組名稱為ColorPickerView
原文說明網站,模組原始碼下載
二話不說馬上來實驗看看module先引入colorPickerView
colorPickerView
首先先在
build.gradle裡, dependency下加入
- compile 'com.github.skydoves:colorpickerpreference:1.0.2'
複製代碼
ColorPickerView
colorPickerView
layout 加入ColorPickerView 元件
- <com.skydoves.colorpickerpreference.ColorPickerView
- android:id="@+id/colorPickerView"
- android:layout_width="300dp"
- android:layout_height="300dp"
- app:palette="@drawable/palette"
- app:selector="@drawable/wheel" />
複製代碼
在colorPickerView 加入 ColorListener
- colorPickerView.setColorListener(new ColorListener() {
- @Override
- public void onColorSelected(ColorEnvelope colorEnvelope) {
- LinearLayout linearLayout = findViewById(R.id.linearLayout);
- linearLayout.setBackgroundColor(colorEnvelope.getColor());
- }
- });
複製代碼
得到顏色數值
- colorEnvelope.getColor(); // int
- colorEnvelope.getColorHtml(); //color code String
- colorEnvelope.getColorRGB(); // int[3]
複製代碼
如果你要儲存選擇器的顏色色碼
- colorPickerView.setPreferenceName("MyColorPickerView");
複製代碼
當你要使用上次的記錄位置顏色色碼
- @Override
- protected void onDestroy() {
- super.onDestroy();
- colorPickerView.saveData();
- }
複製代碼
預設顏色初始化
- int color = colorPickerView.getSavedColor(Color.WHITE);
- String htmlColor = colorPickerView.getSavedColorHtml(Color.WHITE);
- int[] colorRGB = colorPickerView.getSavedColorRGB(Color.WHITE);
複製代碼
ColorPickerDialog
如果要執行ColorPicker對話視窗
- ColorPickerDialog.Builder builder = new ColorPickerDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
- builder.setTitle("ColorPicker Dialog");
- builder.setPreferenceName("MyColorPickerDialog");
- builder.setFlagView(new CustomFlag(this, R.layout.layout_flag));
- builder.setPositiveButton(getString(R.string.confirm), new ColorListener() {
- @Override
- public void onColorSelected(ColorEnvelope colorEnvelope) {
- TextView textView = findViewById(R.id.textView);
- textView.setText("#" + colorEnvelope.getHtmlCode());
- LinearLayout linearLayout = findViewById(R.id.linearLayout);
- linearLayout.setBackgroundColor(colorEnvelope.getColor());
- }
- });
- builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- builder.show();
複製代碼
如果你要儲存位置
- ColorPickerView colorPickerView = builder.getColorPickerView();
- colorPickerView.setPreferenceName("MyColorPickerDialog");
複製代碼
ColorPickerPreference
colorPickerView
如果要執行PreferenceScreen
在layout 加入ColorPickerView 元件
- <com.skydoves.colorpickerpreference.ColorPickerView
- android:id="@+id/colorPickerView"
- android:layout_width="300dp"
- android:layout_height="300dp"
- app:palette="@drawable/palette"
- app:selector="@drawable/wheel" />
複製代碼 在colorPickerView 加入 ColorListener
- colorPickerView.setColorListener(new ColorListener() {
- @Override
- public void onColorSelected(ColorEnvelope colorEnvelope) {
- LinearLayout linearLayout = findViewById(R.id.linearLayout);
- linearLayout.setBackgroundColor(colorEnvelope.getColor());
- }
- });
複製代碼
儲存色碼及重置方式跟ColorPickerView上述一樣
|
-
colorPickerView
|