手机版
你好,游客 登录 注册
背景:
阅读新闻

Android 颜色选择的实现

[日期:2011-05-20] 来源:Linux社区  作者:snailandjava [字体:大 中 小]

Tip:自己封装控件不论是自定义控件也好,preference 也好在XML里面都是无法智能提示的,只要自己把握好输入的属性准确无误就行。

完整代码如下:

  1. package com.terry.util;     
  2.       
  3. import Android.app.AlertDialog.Builder;     
  4. import android.content.Context;     
  5. import android.content.SharedPreferences;     
  6. import android.graphics.Canvas;     
  7. import android.graphics.Color;     
  8. import android.graphics.LinearGradient;     
  9. import android.graphics.Paint;     
  10. import android.graphics.RectF;     
  11. import android.graphics.Shader;     
  12. import android.graphics.SweepGradient;     
  13. import android.preference.DialogPreference;     
  14. import android.util.AttributeSet;     
  15. import android.view.Gravity;     
  16. import android.view.LayoutInflater;     
  17. import android.view.MotionEvent;     
  18. import android.view.View;     
  19. import android.view.ViewGroup;     
  20. import android.widget.LinearLayout;     
  21. import android.widget.TextView;     
  22.       
  23. import com.terry.eBook.R;     
  24.       
  25. public class ColorPickerPreference extends DialogPreference {     
  26.     private int mInitialColor;     
  27.     private int mCurrentColor;     
  28.     private ColorPickerView mCPView;     
  29.     private TextView summary;     
  30.       
  31.     private static class ColorPickerView extends View {     
  32.         private Paint mPaint;     
  33.         private Paint mCenterPaint;     
  34.         private Paint mHSVPaint;     
  35.         private final int[] mColors;     
  36.         private int[] mHSVColors;     
  37.         private boolean mRedrawHSV;     
  38.         private OnColorChangedListener mListener;     
  39.       
  40.         ColorPickerView(Context c, OnColorChangedListener l, int color) {     
  41.             super(c);     
  42.             mListener = l;     
  43.             mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF,     
  44.                     0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };     
  45.             Shader s = new SweepGradient(0, 0, mColors, null);     
  46.       
  47.             mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);     
  48.             mPaint.setShader(s);     
  49.             mPaint.setStyle(Paint.Style.STROKE);     
  50.             mPaint.setStrokeWidth(55);     
  51.       
  52.             mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);     
  53.             mCenterPaint.setColor(color);     
  54.             mCenterPaint.setStrokeWidth(5);     
  55.       
  56.             mHSVColors = new int[] { 0xFF000000, color, 0xFFFFFFFF };     
  57.       
  58.             mHSVPaint = new Paint(Paint.ANTI_ALIAS_FLAG);     
  59.             mHSVPaint.setStrokeWidth(10);     
  60.       
  61.             mRedrawHSV = true;     
  62.       
  63.         }     
  64.       
  65.         private boolean mTrackingCenter;     
  66.         private boolean mHighlightCenter;     
  67.       
  68.         public int getColor() {     
  69.             return mCenterPaint.getColor();     
  70.         }     
  71.       
  72.         @Override     
  73.         protected void onDraw(Canvas canvas) {     
  74.             float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f;     
  75.       
  76.             canvas.translate(CENTER_X, CENTER_X);     
  77.             int c = mCenterPaint.getColor();     
  78.       
  79.             if (mRedrawHSV) {     
  80.                 mHSVColors[1] = c;     
  81.                 mHSVPaint.setShader(new LinearGradient(-100, 0, 100, 0,     
  82.                         mHSVColors, null, Shader.TileMode.CLAMP));     
  83.             }     
  84.       
  85.             canvas.drawOval(new RectF(-r, -r, r, r), mPaint);     
  86.             canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);     
  87.             canvas.drawRect(new RectF(-100, 130, 100, 110), mHSVPaint);     
  88.       
  89.             if (mTrackingCenter) {     
  90.                 mCenterPaint.setStyle(Paint.Style.STROKE);     
  91.       
  92.                 if (mHighlightCenter) {     
  93.                     mCenterPaint.setAlpha(0xFF);     
  94.                 } else {     
  95.                     mCenterPaint.setAlpha(0x80);     
  96.                 }     
  97.                 canvas.drawCircle(0, 0, CENTER_RADIUS     
  98.                         + mCenterPaint.getStrokeWidth(), mCenterPaint);     
  99.       
  100.                 mCenterPaint.setStyle(Paint.Style.FILL);     
  101.                 mCenterPaint.setColor(c);     
  102.             }     
  103.       
  104.             mRedrawHSV = true;     
  105.         }     
  106.       
  107.         @Override     
  108.         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     
  109.             setMeasuredDimension(CENTER_X * 2, (CENTER_Y + 25) * 2);     
  110.         }     
  111.       
  112.         private static final int CENTER_X = 100;     
  113.         private static final int CENTER_Y = 100;     
  114.         private static final int CENTER_RADIUS = 30;     
  115.       
  116.         private int ave(int s, int d, float p) {     
  117.             return s + java.lang.Math.round(p * (d - s));     
  118.         }     
  119.       
  120.         private int interpColor(int colors[], float unit) {     
  121.             if (unit <= 0) {     
  122.                 return colors[0];     
  123.             }     
  124.             if (unit >= 1) {     
  125.                 return colors[colors.length - 1];     
  126.             }     
  127.       
  128.             float p = unit * (colors.length - 1);     
  129.             int i = (int) p;     
  130.             p -= i;     
  131.       
  132.             // now p is just the fractional part [0...1) and i is the index     
  133.             int c0 = colors[i];     
  134.             int c1 = colors[i + 1];     
  135.             int a = ave(Color.alpha(c0), Color.alpha(c1), p);     
  136.             int r = ave(Color.red(c0), Color.red(c1), p);     
  137.             int g = ave(Color.green(c0), Color.green(c1), p);     
  138.             int b = ave(Color.blue(c0), Color.blue(c1), p);     
  139.       
  140.             return Color.argb(a, r, g, b);     
  141.         }     
  142.       
  143.         private static final float PI = 3.1415926f;     
  144.       
  145.         @Override     
  146.         public boolean onTouchEvent(MotionEvent event) {     
  147.             float x = event.getX() - CENTER_X;     
  148.             float y = event.getY() - CENTER_Y;     
  149.             boolean inCenter = java.lang.Math.sqrt(x * x + y * y) <= CENTER_RADIUS;     
  150.       
  151.             switch (event.getAction()) {     
  152.             case MotionEvent.ACTION_DOWN:     
  153.                 mTrackingCenter = inCenter;     
  154.                 if (inCenter) {     
  155.                     mHighlightCenter = true;     
  156.                     invalidate();     
  157.                     break;     
  158.                 }     
  159.             case MotionEvent.ACTION_MOVE:     
  160.                 if (mTrackingCenter) {     
  161.                     if (mHighlightCenter != inCenter) {     
  162.                         mHighlightCenter = inCenter;     
  163.                         invalidate();     
  164.                     }     
  165.                 } else if ((x >= -100 & x <= 100) && (y <= 130 && y >= 110)) // see     
  166.                 // if     
  167.                 // we're     
  168.                 // in     
  169.                 // the     
  170.                 // hsv     
  171.                 // slider     
  172.                 {     
  173.                     int a, r, g, b, c0, c1;     
  174.                     float p;     
  175.       
  176.                     // set the center paint to this color     
  177.                     if (x < 0) {     
  178.                         c0 = mHSVColors[0];     
  179.                         c1 = mHSVColors[1];     
  180.                         p = (x + 100) / 100;     
  181.                     } else {     
  182.                         c0 = mHSVColors[1];     
  183.                         c1 = mHSVColors[2];     
  184.                         p = x / 100;     
  185.                     }     
  186.       
  187.                     a = ave(Color.alpha(c0), Color.alpha(c1), p);     
  188.                     r = ave(Color.red(c0), Color.red(c1), p);     
  189.                     g = ave(Color.green(c0), Color.green(c1), p);     
  190.                     b = ave(Color.blue(c0), Color.blue(c1), p);     
  191.       
  192.                     mCenterPaint.setColor(Color.argb(a, r, g, b));     
  193.       
  194.                     mRedrawHSV = false;     
  195.                     invalidate();     
  196.                 } else {     
  197.                     float angle = (float) java.lang.Math.atan2(y, x);     
  198.                     // need to turn angle [-PI ... PI] into unit [0....1]     
  199.                     float unit = angle / (2 * PI);     
  200.                     if (unit < 0) {     
  201.                         unit += 1;     
  202.                     }     
  203.                     mCenterPaint.setColor(interpColor(mColors, unit));     
  204.                     invalidate();     
  205.                 }     
  206.                 break;     
  207.             case MotionEvent.ACTION_UP:     
  208.                 if (mTrackingCenter) {     
  209.                     if (inCenter) {     
  210.                         mListener.colorChanged(mCenterPaint.getColor());     
  211.                     }     
  212.                     mTrackingCenter = false; // so we draw w/o halo     
  213.                     invalidate();     
  214.                 }     
  215.                 break;     
  216.             }     
  217.             return true;     
  218.         }     
  219.     }     
  220.       
  221.     public interface OnColorChangedListener {     
  222.         void colorChanged(int color);     
  223.     }     
  224.       
  225.     public ColorPickerPreference(Context contex) {     
  226.         this(contex, null);     
  227.     }     
  228.       
  229.     public ColorPickerPreference(Context context, AttributeSet attrs) {     
  230.         super(context, attrs);     
  231.       
  232.     }     
  233.       
  234.     public ColorPickerPreference(Context context, AttributeSet attrs,     
  235.             int defStyle) {     
  236.         super(context, attrs, defStyle);     
  237.       
  238.     }     
  239.       
  240.     @Override     
  241.     protected void onDialogClosed(boolean positiveResult) {     
  242.         if (positiveResult) {     
  243.             mCurrentColor = mCPView.getColor();     
  244.             summary.setTextColor(mCurrentColor);     
  245.             SharedPreferences.Editor editor = getEditor();     
  246.             editor.putInt(getKey(), mCurrentColor);     
  247.             editor.commit();     
  248.             callChangeListener(new Integer(mCurrentColor));     
  249.         }     
  250.     }     
  251.       
  252.     @Override     
  253.     protected View onCreateView(ViewGroup parent) {     
  254.         // TODO Auto-generated method stub     
  255.       
  256.         View view = LayoutInflater.from(getContext()).inflate(     
  257.                 R.layout.preference, null);     
  258.       
  259.         TextView title = (TextView) view.findViewById(R.id.title);     
  260.         title.setText(getTitle());     
  261.       
  262.         summary = (TextView) view.findViewById(R.id.summary);     
  263.         summary.setText(getSummary());     
  264.         SharedPreferences prefs = getPreferenceManager().getSharedPreferences();     
  265.         mInitialColor = prefs.getInt(getKey(), Color.LTGRAY);     
  266.         summary.setTextColor(mInitialColor);      
  267.         return view;     
  268.     }     
  269.       
  270.     @Override     
  271.     protected void onPrepareDialogBuilder(Builder builder) {     
  272.         super.onPrepareDialogBuilder(builder);     
  273.       
  274.         OnColorChangedListener l = new OnColorChangedListener() {     
  275.             public void colorChanged(int color) {     
  276.                 mCurrentColor = color;     
  277.                 onDialogClosed(true);     
  278.                 getDialog().dismiss();     
  279.             }     
  280.         };      
  281.       
  282.         LinearLayout layout = new LinearLayout(getContext());     
  283.         layout.setPadding(20, 20, 20, 20);     
  284.         layout.setOrientation(LinearLayout.VERTICAL);     
  285.         mCPView = new ColorPickerView(getContext(), l, mInitialColor);     
  286.       
  287.         LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(     
  288.                 LinearLayout.LayoutParams.WRAP_CONTENT,     
  289.                 LinearLayout.LayoutParams.WRAP_CONTENT);     
  290.         params1.gravity = Gravity.CENTER;     
  291.         mCPView.setLayoutParams(params1);     
  292.         layout.addView(this.mCPView);     
  293.         layout.setId(android.R.id.widget_frame);     
  294.         builder.setView(layout);     
  295.     }     
  296. }    
linux
相关资讯       Android 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* xx 发表于 2012/4/24 0:05:30
求下载。。