当前位置:操作系统 > 安卓/Android >>

Android 颜色选择器(ColorPicker)

1. package com.dwood.paintdemo; 
2.  
3. import android.app.Dialog; 
4. import android.content.Context; 
5. import android.graphics.Canvas; 
6. import android.graphics.Color; 
7. import android.graphics.LinearGradient; 
8. import android.graphics.Paint; 
9. import android.graphics.RectF; 
10. import android.graphics.Shader; 
11. import android.graphics.SweepGradient; 
12. import android.os.Bundle; 
13. import android.util.Log; 
14. import android.view.MotionEvent; 
15. import android.view.View; 
16. import android.view.WindowManager; 
17.  
18. public class ColorPickerDialog extends Dialog { 
19.     private final boolean debug = true; 
20.     private final String TAG = "ColorPicker"; 
21.      
22.     Context context; 
23.     private String title;//标题 
24.     private int mInitialColor;//初始颜色 
25.     private OnColorChangedListener mListener; 
26.  
27.     /**
28.      * 初始颜色黑色
29.      * @param context
30.      * @param title 对话框标题
31.      * @param listener 回调
32.      */ 
33.     public ColorPickerDialog(Context context, String title,  
34.             OnColorChangedListener listener) { 
35.         this(context, Color.BLACK, title, listener); 
36.     } 
37.      
38.     /**
39.      * 
40.      * @param context
41.      * @param initialColor 初始颜色
42.      * @param title 标题
43.      * @param listener 回调
44.      */ 
45.     public ColorPickerDialog(Context context, int initialColor,  
46.             String title, OnColorChangedListener listener) { 
47.         super(context); 
48.         this.context = context; 
49.         mListener = listener; 
50.         mInitialColor = initialColor; 
51.         this.title = title; 
52.     } 
53.  
54.     @Override 
55.     protected void onCreate(Bundle savedInstanceState) { 
56.         super.onCreate(savedInstanceState); 
57.         WindowManager manager = getWindow().getWindowManager(); 
58.         int height = (int) (manager.getDefaultDisplay().getHeight() * 0.5f); 
59.         int width = (int) (manager.getDefaultDisplay().getWidth() * 0.7f); 
60.         ColorPickerView myView = new ColorPickerView(context, height, width); 
61.         setContentView(myView); 
62.         setTitle(title); 
63.     } 
64.      
65.     private class ColorPickerView extends View { 
66.         private Paint mPaint;//渐变色环画笔 
67.         private Paint mCenterPaint;//中间圆画笔 
68.         private Paint mLinePaint;//分隔线画笔 
69.         private Paint mRectPaint;//渐变方块画笔 
70.          
71.         private Shader rectShader;//渐变方块渐变图像 
72.         private float rectLeft;//渐变方块左x坐标 
73.         private float rectTop;//渐变方块右x坐标 
74.         private float rectRight;//渐变方块上y坐标 
75.         private float rectBottom;//渐变方块下y坐标 
76.          
77.         private final int[] mCircleColors;//渐变色环颜色 
78.         private final int[] mRectColors;//渐变方块颜色 
79.          
80.         private int mHeight;//View高 
81.         private int mWidth;//View宽 
82.         private float r;//色环半径(paint中部) 
83.         private float centerRadius;//中心圆半径 
84.          
85.         private boolean downInCircle = true;//按在渐变环上 
86.         private boolean downInRect;//按在渐变方块上 
87.         private boolean highlightCenter;//高亮 
88.         private boolean highlightCenterLittle;//微亮 
89.          
90.         public ColorPickerView(Context context, int height, int width) { 
91.             super(context); 
92.             this.mHeight = height - 36; 
93.             this.mWidth = width; 
94.             setMinimumHeight(height - 36); 
95.             setMinimumWidth(width); 
96.              
97.             //渐变色环参数 
98.             mCircleColors = new int[] {0xFFFF0000, 0xFFFF00FF, 0xFF0000FF,  
99.                     0xFF00FFFF, 0xFF00FF00,0xFFFFFF00, 0xFFFF0000}; 
100.      

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,