Android Color Selector and example tutorials for changing font colors _android

Source: Internet
Author: User
Tags getcolor xsl

1. Build a colorful Picture:

We often see this type of Color selector bar.

And then actually the right brightness selection is:

So that our code can be carried out ...

Create a colorful picture private void init () {int[] oc = {0xffff0000, 0xffffff00, 0xff00ff00, 0xff00ffff, 0XFF0000FF, 0XFFFF00FF,
 0xffff0000};
 Float[] op = {0, 0.16667f, 0.33333f, 0.5f, 0.66667f, 0.83333f, 1};
 LinearGradient lg = new LinearGradient (0, 0, oriwidth, 0, OC, OP, Tilemode.mirror);
 LinearGradient lg2 = new LinearGradient (0, 0, 0, oriheight, 0x00808080, 0xff808080, Tilemode.mirror);
 Oricolor = Bitmap.createbitmap (Oriwidth, Oriheight, config.argb_8888);
 Canvas C = new Canvas (Oricolor);
 Paint.setshader (LG);
 C.drawrect (0, 0, oriwidth, oriheight, paint);
 Paint.setshader (LG2);
 C.drawrect (0, 0, oriwidth, oriheight, paint);
 }///Right brightness bar private void Drawabar (Canvas c) {int x, y;
 x = (Roundcolor & 0X00FFFFFF);
 y = (x | 0xff000000);
 LinearGradient lg = new LinearGradient (0, 0, layoutwidth, 0, X, y, tilemode.mirror); 
 Initialize x + 6 * 2 y = oriheight + (Gap << 2)-gap + barheight; 
 Paint.setcolor (0xFFFFFFFF);
 C.drawbitmap (ABK, 0, y, paint); Paint.setsHader (LG);
 C.drawrect (0, y, layoutwidth, y + barheight, paint);

 }

Other screen events and so on No code ...

2.ColorPicker Color Selector Change font Color instance:

(1) Test interface

(2) Palette dialog box

(3) Change the font color

Well, look at the code:

Package com.xsl.colorpicker; 
 
Import Android.app.Dialog; 
 
Import Android.content.Context; 
 
Import Android.graphics.Canvas; 
 
Import Android.graphics.Color; 
 
Import android.graphics.LinearGradient; 
 
Import Android.graphics.Paint; 
 
Import Android.graphics.RectF; 
 
Import Android.graphics.Shader; 
 
Import android.graphics.SweepGradient; 
 
Import Android.os.Bundle; 
 
Import Android.util.Log; 
 
Import android.view.MotionEvent; 
 
Import Android.view.View; 
 
  
 
Import Android.view.WindowManager; 
 
  public class Colorpickerdialog extends Dialog {private final Boolean debug = true; 
 
    
 
  Private final String TAG = "ColorPicker"; 
 
  Context context;    Private String title; Title private int Minitialcolor; 
 
  
 
  Original color private Oncolorchangedlistener mlistener; /** * Initial Color Black * @param context * @param title dialog box Title * @param listener callback/Public color Pickerdialog (context context, String title, OncolorchAngedlistener listener) {This (context, Color.Black, title, listener); 


 }
/** * * @param context * @param initialcolor initial color * @param tit Le title * @param Listener Callback */Public Colorpickerdialog (context, int initialcolor, Strin 
 
    G title, Oncolorchangedlistener listener) {super (context); 
 
    This.context = context; 
 
    Mlistener = listener; 
 
    Minitialcolor = Initialcolor; 
 
  This.title = title;  
 
    } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
    WindowManager manager = GetWindow (). Getwindowmanager ();    int height = (int) (Manager.getdefaultdisplay (). GetHeight () * 0.38f);     0.5 int width = (int) (Manager.getdefaultdisplay (). GetWidth () * 0.5f); 
 
    0.7 Colorpickerview MyView = new Colorpickerview (context, height, width); 
 
    Setcontentview (MyView); 
 
  Settitle (title); } 
  Private class Colorpickerview extends View {private Paint mpaint;   Gradient Ring Brush private Paint mcenterpaint;    Middle Circle Brush Private Paint mlinepaint;    Separator line Brush private Paint mrectpaint;   Gradient Square Brush private Shader rectshader;     Gradient square gradient Image private float rectleft;     Gradient Square left x coordinate private float recttop;    Gradient square right x coordinate private float rectright;    Gradient square on the y-coordinate private float rectbottom;   Gradient square under y-coordinate private final int[] mcirclecolors;    Gradient Color ring Colors private final int[] mrectcolors;          Gradient square color private int mheight;           View high private int mwidth;            View wide private float R;       Color ring radius (central paint) private float Centerradius;  Center Circle Radius Private Boolean downincircle = true;       Press the private Boolean downinrect on the gradient ring;    Press on the Gradient box private Boolean highlightcenter; Highlight Private Boolean highlightcenterlittle; 
 
      Micro Light
 
    Public Colorpickerview (context, int height, int width) {super (context); 
 
      This.mheight = height-36; 
 
      This.mwidth = width; 
 
      Setminimumheight (height-36); 
 
        
 
      Setminimumwidth (width); Gradient ring parameter mcirclecolors = new int[] {0xffff0000, 0xffff00ff, 0xff0000ff, 0XFF00FFFF, 0xff00ff00,0xff 
 
      FFFF00, 0xffff0000}; 
 
      Shader s = new Sweepgradient (0, 0, mcirclecolors, NULL); 
 
      Mpaint = new Paint (Paint.anti_alias_flag); 
 
      Mpaint.setshader (s); 
 
      Mpaint.setstyle (Paint.Style.STROKE); 
 
      Mpaint.setstrokewidth (50); 
 
        
 
      r = WIDTH/2 * 0.7f-mpaint.getstrokewidth () * 0.5f; 
 
      Central Circular parameter mcenterpaint = new Paint (Paint.anti_alias_flag); 
 
      Mcenterpaint.setcolor (Minitialcolor); 
 
      Mcenterpaint.setstrokewidth (5); 
 
        
 
      Centerradius = (R-mpaint.getstrokewidth ()/2) * 0.7f; Border parameter Mlinepaint = new Paint (Paint.Anti_alias_flag); 
 
      Mlinepaint.setcolor (Color.parsecolor ("#72A1D1")); 
 
        
 
      Mlinepaint.setstrokewidth (4); 
 
      Black and white gradient parameter mrectcolors = new int[]{0xff000000, Mcenterpaint.getcolor (), 0xFFFFFFFF}; 
 
      Mrectpaint = new Paint (Paint.anti_alias_flag); 
 
      Mrectpaint.setstrokewidth (5); 
 
      Rectleft =-r-mpaint.getstrokewidth () * 0.5f; 
 
      Recttop = r + mpaint.getstrokewidth () * 0.5f + mlinepaint.getstrokemiter () * 0.5f + 15; 
 
      Rectright = r + mpaint.getstrokewidth () * 0.5f; 
 
    Rectbottom = Recttop + 50; 
 }



 
    @Override 
 
    protected void OnDraw (Canvas Canvas) { 
 
      //Mobile Center 
 
      canvas.translate (MWIDTH/2, MHEIGHT/2-m); 
 
      Draw Center Round 
 
      canvas.drawcircle (0, 0, Centerradius, mcenterpaint); 
 
        
 
      Whether to display a small ring outside the center circle 
 
      if (Highlightcenter | | highlightcenterlittle) { 
 
        int c = Mcenterpaint.getcolor (); 
 
        Mcenterpaint.setstyle (Paint.Style.STROKE); 
 
        if (highlightcenter) { 
 
          mcenterpaint.setalpha (0xFF); 
 
        } else if (highlightcenterlittle) { 
 
          mcenterpaint.setalpha (0x90); 
 
        } 
 
        Canvas.drawcircle (0, 0,  
 
            Centerradius + mcenterpaint.getstrokewidth (), mcenterpaint); 
 
          
 
        Mcenterpaint.setstyle (Paint.Style.FILL); 
 
        Mcenterpaint.setcolor (c); 
 
      } 


      Draw the color ring Canvas.drawoval (new RECTF (-R, R, R., R), Mpaint); 
 
      Draw black and white gradient block if (downincircle) {mrectcolors[1] = Mcenterpaint.getcolor (); 
 
      } Rectshader = new LinearGradient (rectleft, 0, rectright, 0, mrectcolors, NULL, Shader.TileMode.MIRROR); 
 
      Mrectpaint.setshader (Rectshader); 
 
      Canvas.drawrect (Rectleft, Recttop, Rectright, Rectbottom, Mrectpaint); 
 
      float offset = mlinepaint.getstrokewidth ()/2; Canvas.drawline (Rectleft-offset, Recttop-offset * 2, Rectleft-offset, Rectbottom + offset * 2, Mlinepa  int);//Left Canvas.drawline (Rectleft-offset * 2, Recttop-offset, Rectright + offset * 2, Recttop- Offset, mlinepaint);//Upper Canvas.drawline (rectright + offset, Recttop-offset * 2, Rectright + offs  
 
          ET, rectbottom + offset * 2, mlinepaint);//Right Canvas.drawline (Rectleft-offset * 2, Rectbottom + offset, Rectright + offset * 2, Rectbottom + offset, mlinepaint);//Lower Super.ondraw (canvas); @Override public boolean ontouchevent (Motionevent event) {float x = event.getx ()-Mwid 
 
      TH/2; 
 
      Float y = event.gety ()-MHEIGHT/2 + 50; 
 
      Boolean incircle = Incolorcircle (x, y, R + mpaint.getstrokewidth ()/2, R-mpaint.getstrokewidth ()/2); 
 
      Boolean incenter = Incenter (x, y, Centerradius); 
 
        
 
      Boolean inrect = Inrect (x, y); 
 
          Switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:downInCircle = incircle; 
 
          Downinrect = Inrect; 
 
        Highlightcenter = Incenter; 
 
            Case MotionEvent.ACTION_MOVE:if (downincircle && incircle) {//down is in the gradient ring and move is in the gradient ring 
 
            float angle = (float) math.atan2 (y, x); 
 
            FLOAT unit = (float) (Angle/(2 * math.pi)); 
 
           if (unit < 0) {unit = 1; } mcenterpaint.setcolor (Interpcirclecolor (mcirclecolors, Unit)); 
 
          if (Debug) log.v (TAG, "color ring, Coordinates:" + x + "," + y); }else if (downinrect && inrect) {//down in the gradient box, and move is also in the gradient square mcenterpaint.setcolor (MR 
 
          Ectcolors, x)); } if (Debug) log.v (TAG, [move] Highlight: + Highlightcenter + "micro-bright:" + highlightcenterlittle + "Center:" + incenter) 
 
          ; if ((Highlightcenter && incenter) | | (Highlightcenterlittle && incenter)) 
 
            {//Click Center Circle, current move in center circle Highlightcenter = true; 
 
          Highlightcenterlittle = false; 
 
            else if (Highlightcenter | | highlightcenterlittle) {//Click in center Circle, current move out center circle Highlightcenter = false; 
 
          Highlightcenterlittle = true; 
 
            else {highlightcenter = false; 
 
          Highlightcenterlittle = false; 
 
          } invalidate (); 
 
        Break Case MotionEvent.ACTION_UP:if (Highlightcenter && incenter) {//click in center Circle and currently start in center circle if (Mlistener!= 
 
              NULL) {mlistener.colorchanged (Mcenterpaint.getcolor ()); 
 
            ColorPickerDialog.this.dismiss (); 
 
          } if (downincircle) {downincircle = false; 
 
          } if (downinrect) {downinrect = false; 
 
          } if (highlightcenter) {highlightcenter = false; 
 
          } if (highlightcenterlittle) {highlightcenterlittle = false; 
 
          } invalidate (); 
 
      Break 
 
    return true; @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {Super.onme 
 
    Asure (Mwidth, mheight); 

 }
/** * coordinates on the color ring * @param x coordinate * @param y coordinate * @param  Outradius Outer radius * @param inradius inner radius of the ring * @return/private Boolean incolorcircle (float x, 
 
      Float y, float outradius, float Inradius) {Double outcircle = Math.PI * Outradius * Outradius; 
 
      Double incircle = Math.PI * Inradius * Inradius; 
 
      Double fingercircle = Math.PI * (x * x + y * y); 
 
      if (Fingercircle < outcircle && fingercircle > InCircle) {return true; 
 
      }else {return false; } 
 
    } 

    /** 
 
     * Coordinates are in the center circle 
 
     * @param x Coordinate * 
 
     @param y coordinate * 
 
     @param centerradius Circle Radius 
 
     * @return * * 
 
    Private Boo Lean Incenter (float x, float y, float centerradius) { 
 
      double centercircle = Math.PI * Centerradius * centerradius;
   
    double fingercircle = Math.PI * (x * x + y * y); 
 
      if (Fingercircle < centercircle) {return 
 
        true; 
 
      } else {return 
 
        false; 
 
      } 
 
    } 

   
    /** 
 
     * Coordinates whether in gradient 
 
     * @param x 
 
     * @param y 
 
     * @return 
 
    /Private Boolean Inrect (float x, float y) { 
 
      if (x <= rectright && x >=rectleft && y <= rectbottom && y >=recttop) { 
 
        R Eturn true; 
 
      } else {return 
 
        false; 
 
      } 
 
    } 

/** * Gets the color of the ring * @param colors * @param unit * @retur n */private int interpcirclecolor (int colors[], float unit) {if (unit <= 0) {Retu 
 
      RN 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 is 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); } 

/** * Gets the color of the gradient block * @param colors * @param x * @return 
 
      * * Private int interprectcolor (int colors[], float x) {int A, r, G, B, C0, C1; 
 
      float p;  
 
        if (x < 0) {C0 = colors[0]; 
 
        C1 = colors[1]; 
 
      p = (x + rectright)/rectright; 
 
        else {C0 = colors[1]; 
 
        C1 = colors[2]; 
 
      p = x/rectright; 
 
      A = Ave (Color.alpha (C0), Color.alpha (C1), p); 
 
      R = Ave (Color.Red (C0), Color.Red (C1), p); 
 
      G = Ave (Color.green (C0), Color.green (C1), p); 
 
      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 + math.round (p * (d)); } 
 
  } 
  /** 
 
   * Callback interface/public 
 
  interface Oncolorchangedlistener { 
 
    /** 
 
     * callback function 
 
     * @param color selected Colors 
 
     * 
 
    void colorchanged (int color); 
 
  } 
 
    
 
  Public String GetTitle () {return 
 
    title; 
 
  } 
 
  
 
  public void Settitle (String title) { 
 
    this.title = title; 
 
  } 
 
  
 
  public int Getminitialcolor () {return 
 
    minitialcolor; 
 
  } 
 
  
 
  public void Setminitialcolor (int minitialcolor) { 
 
    this.minitialcolor = Minitialcolor; 
 
  } 
 
  
 
  Public Oncolorchangedlistener Getmlistener () {return 
 
    mlistener; 
 
  } 
 
  
 
  public void Setmlistener (Oncolorchangedlistener mlistener) { 
 
    this.mlistener = Mlistener; 
 
  } 
 
} 


Test interface
Paintdemoactivity.java

Package com.xsl.colorpicker; 
 
Import android.app.Activity; 
 
Import Android.content.Context; 
 
Import Android.os.Bundle; 
 
Import Android.view.View; 
 
Import Android.widget.Button; 
 
Import Android.widget.TextView;   
 
  public class Paintdemoactivity The extends activity {context context;   
 
  Private Button Btncolorpicker;   
 
     
 
  Private TextView Tvtext;   
 
  Private Colorpickerdialog Dialog; 
 
    @Override public void OnCreate (Bundle savedinstancestate) {context = this; 
 
    Super.oncreate (savedinstancestate); 
 
    Setcontentview (R.layout.main); 
 
  Initviews (); /** * Initialize UI */private void Initviews () {btncolorpicker = (Button) findviewbyi   
 
    D (R.id.button1);   
 
        Btncolorpicker.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) { dialog = new Colorpickerdialog (context, Tvtext.gettextcolors (). Getdefaultcolor (), GetreSources (). getString (R.string.app_name), new Colorpickerdialog.oncolorchangedlistener () {p   
 
          ublic void colorchanged (int color) {tvtext.settextcolor (color);   
 
        }   
 
        }); 
 
      Dialog.show ();   


    }   
 
    });   
 
  Tvtext = (TextView) Findviewbyid (r.id.tv); 

 }   
 
}

Related Article

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.