Color Picker Learning

Source: Internet
Author: User
Tags bitmask getcolor

Say is a color selector, actually I did not use this, just want to engage in the gradient, but in the online collection of information when it feels like it is together so I said all together to see it

In fact, the color selector on the internet, a lot of information is basically based on Android Apidemo do research, I am no exception:

A lot of code online I just posted: there are comments:

The main introduction of this:

Sweepgradient the color gradient direction is not annular, but at a point as the center of a fan swept.

Sweepgradient constructor:

Public sweepgradient (float CX, float CY, int[] colors, float[] positions)
Public sweepgradient (float CX, float CY, int color0, int color1)

Where cx,cy specifies the center, color1,color0, or colors specifies the color of the gradient, and for more than two colors, the relative position of each color can also be specified by positions, and the color is evenly distributed when positions is set to NULL.

The base class for Lineergradient, Radialgradient, sweepgradient, etc. is Shader (similar to the Brush class in GDI), and Shader defines a corresponding local Matrix, which can be the most Shader do coordinate transformations.


public class Colorview extends view{

Private Paint Mpaint;
Private Paint Mcenterpaint;
Private int[] mcolors;
private static final int center_x = 300;
private static final int center_y = 300;
private static final int center_radius = 100;
Private Boolean mtrackingcenter;
Private Boolean mhighlightcenter;
Private Oncolorchangelistener ChangeListener;
Private static final float PI = 3.1415926f;
public interface oncolorchangelistener{
void colorchanged (int color);

};
Public Colorview (Context Context,oncolorchangelistener changelistener) {
Super (context);
This.changelistener =changelistener;
Mcolors = new int[] {//gradient array
0xff000000,0xffffffff,0xffff0000, 0XFFFF00FF, 0xff0000ff, 0XFF00FFFF, 0XFF00FF00,
0XFFFFFF00, 0xffff0000
};

Shader Shader =new sweepgradient (0, 0,mcolors, NULL);
Mpaint =new Paint (paint.anti_alias_flag);//paint.filter_bitmap_flag is the bitmask flag that filters the bitmap
Paint.anti_alias_flag is the flag that makes the bitmap anti-aliasing
Paint.dither_flag is a bitmask flag that makes the bitmap a favorable jitter
Mpaint.setshader (shader);
Mpaint.setstyle (Style.stroke);
Mpaint.setstrokewidth (100);

Mcenterpaint =new Paint (Paint.anti_alias_flag);
Mcenterpaint.setcolor (color.red);
Mcenterpaint.setstrokewidth (5);
}
Public Colorview (context context, AttributeSet attrs, int defstyleattr) {
Super (context, attrs, defstyleattr);


}
@Override
protected void OnDraw (canvas canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
Float r =center_x-mpaint.getstrokewidth () *0.5f;
Canvas.translate (center_x, center_x);
Canvas.drawoval (New RECTF (-R,-R, R, R), Mpaint); DrawOval draws an ellipse,
Canvas.drawcircle (0, 0,center_radius, mcenterpaint);
Mcenterpaint.settextsize (15);
Canvas.drawtext ("Click on the outer ring to change the center circle color",-CENTER_X/3, CENTER_Y/2, Mcenterpaint);
if (mtrackingcenter) {
int c = Mcenterpaint.getcolor ();
Mcenterpaint.setstyle (Paint.Style.STROKE);

if (mhighlightcenter) {
Mcenterpaint.setalpha (0xFF);
} else {
Mcenterpaint.setalpha (0x80);
}
Canvas.drawcircle (0, 0,
Center_radius + mcenterpaint.getstrokewidth (),
Mcenterpaint);

Mcenterpaint.setstyle (Paint.Style.FILL);
Mcenterpaint.setcolor (c);
}
}

@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Set view's width height

Setmeasureddimension (center_x*2, center_y*2);
}


@Override
public boolean ontouchevent (Motionevent event) {
float x = Event.getx ()-center_x;
Float y = event.gety ()-center_y;
Boolean inCenter = java.lang.Math.sqrt (x*x + y*y) <= Center_radius;

Switch (event.getaction ()) {
Case Motionevent.action_down:
Mtrackingcenter = InCenter;
if (inCenter) {//is otherwise Central Park
Mhighlightcenter = true;
Invalidate ();
Break
}
Case Motionevent.action_move:
if (mtrackingcenter) {
if (mhighlightcenter! = inCenter) {
Mhighlightcenter = InCenter;
Invalidate ();
}
} else {
float angle = (float) java.lang.Math.atan2 (y, x);
Need to turn angle [-pi ... PI] into unit [0....1]
FLOAT UNIT = angle/(2*PI);
if (unit < 0) {
unit + = 1;
}
Mcenterpaint.setcolor (Interpcolor (mcolors, Unit));
Invalidate ();
}
Break
Case MOTIONEVENT.ACTION_UP:
if (mtrackingcenter) {
if (inCenter) {
Changelistener.colorchanged (Mcenterpaint.getcolor ());
}
Mtrackingcenter = false; So we draw w/o Halo
Invalidate ();
}
Break
}
return true;

}

private int Interpcolor (int colors[], float unit) {
if (unit <= 0) {
return colors[0];
}
if (unit >= 1) {
return colors[colors.length-1];
}

FLOAT p = unit * (colors.length-1);
int i = (int) p;
P-= i;

Now p are just the fractional part [0...1) and I are the index
int c0 = Colors[i];
int c1 = colors[i+1];
int a = Ave (Color.alpha (C0), Color.alpha (C1), p);
int r = Ave (Color.Red (C0), Color.Red (C1), p);
int g = Ave (Color.green (C0), Color.green (C1), p);
int b = Ave (Color.Blue (C0), Color.Blue (C1), p);

Return Color.argb (A, R, G, b);
}
private int Ave (int s, int d, float p) {
return S + java.lang.Math.round (p * (D-s));
}
}


Code can be used directly, if there is a problem can download demo:http://download.csdn.net/detail/u012808234/8596367


Color Picker Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.