Android中颜色选择器和改变字体颜色的实例教程

1.构建一张七彩图:

我们经常看到这种样子的颜色选择器吧..

然后其实右边的亮度选择是:

这样我们的代码就可以进行啦...

// 创建七彩图片
 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);
 }

// 右边的亮度栏
 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);
 // 初始化 x 240 + 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);
 }

其他屏幕事件什么的就不贴代码啦...

2.ColorPicker颜色选择器改变字体颜色实例:

(1)测试界面

(2)调色板对话框

(3)改变字体颜色

嗯好,来看代码:

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;    //标题 

  private int mInitialColor; //初始颜色 

  private OnColorChangedListener mListener; 

  /** 

   * 初始颜色黑色 

   * @param context 

   * @param title 对话框标题 

   * @param listener 回调 

   */ 

  public ColorPickerDialog(Context context, String title,  

      OnColorChangedListener listener) { 

    this(context, Color.BLACK, title, listener); 

  }
  /** 

   * 

   * @param context 

   * @param initialColor 初始颜色 

   * @param title 标题 

   * @param listener 回调 

   */ 

  public ColorPickerDialog(Context context, int initialColor,  

      String 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;      //渐变色环画笔 

    private Paint mCenterPaint;   //中间圆画笔 

    private Paint mLinePaint;    //分隔线画笔 

    private Paint mRectPaint;    //渐变方块画笔 

    private Shader rectShader;   //渐变方块渐变图像 

    private float rectLeft;     //渐变方块左x坐标 

    private float rectTop;     //渐变方块右x坐标 

    private float rectRight;    //渐变方块上y坐标 

    private float rectBottom;    //渐变方块下y坐标 

    private final int[] mCircleColors;   //渐变色环颜色 

    private final int[] mRectColors;    //渐变方块颜色 

    private int mHeight;          //View高 

    private int mWidth;           //View宽 

    private float r;            //色环半径(paint中部) 

    private float centerRadius;       //中心圆半径 

    private boolean downInCircle = true;  //按在渐变环上 

    private boolean downInRect;       //按在渐变方块上 

    private boolean highlightCenter;    //高亮 

    private boolean highlightCenterLittle; //微亮 

    public ColorPickerView(Context context, int height, int width) { 

      super(context); 

      this.mHeight = height - 36; 

      this.mWidth = width; 

      setMinimumHeight(height - 36); 

      setMinimumWidth(width); 

      //渐变色环参数 

      mCircleColors = new int[] {0xFFFF0000, 0xFFFF00FF, 0xFF0000FF,  

          0xFF00FFFF, 0xFF00FF00,0xFFFFFF00, 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; 

      //中心圆参数 

      mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 

      mCenterPaint.setColor(mInitialColor); 

      mCenterPaint.setStrokeWidth(5); 

      centerRadius = (r - mPaint.getStrokeWidth() / 2 ) * 0.7f; 

      //边框参数 

      mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 

      mLinePaint.setColor(Color.parseColor("#72A1D1")); 

      mLinePaint.setStrokeWidth(4); 

      //黑白渐变参数 

      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) { 

      //移动中心 

      canvas.translate(mWidth / 2, mHeight / 2 - 50); 

      //画中心圆 

      canvas.drawCircle(0, 0, centerRadius, mCenterPaint); 

      //是否显示中心圆外的小圆环 

      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); 

      }
      //画色环 

      canvas.drawOval(new RectF(-r, -r, r, r), mPaint); 

      //画黑白渐变块 

      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, mLinePaint);//左 

      canvas.drawLine(rectLeft - offset * 2, rectTop - offset,  

          rectRight + offset * 2, rectTop - offset, mLinePaint);//上 

      canvas.drawLine(rectRight + offset, rectTop - offset * 2,  

          rectRight + offset, rectBottom + offset * 2, mLinePaint);//右 

      canvas.drawLine(rectLeft - offset * 2, rectBottom + offset,  

          rectRight + offset * 2, rectBottom + offset, mLinePaint);//下 

      super.onDraw(canvas); 

    } 

    @Override 

    public boolean onTouchEvent(MotionEvent event) { 

      float x = event.getX() - mWidth / 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按在渐变色环内, 且move也在渐变色环内 

            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, "色环内, 坐标: " + x + "," + y); 

          }else if(downInRect && inRect) {//down在渐变方块内, 且move也在渐变方块内 

            mCenterPaint.setColor(interpRectColor(mRectColors, x)); 

          } 

          if(debug) Log.v(TAG, "[MOVE] 高亮: " + highlightCenter + "微亮: " + highlightCenterLittle + " 中心: " + inCenter); 

          if((highlightCenter && inCenter) || (highlightCenterLittle && inCenter)) {//点击中心圆, 当前移动在中心圆 

            highlightCenter = true; 

            highlightCenterLittle = false; 

          } else if(highlightCenter || highlightCenterLittle) {//点击在中心圆, 当前移出中心圆 

            highlightCenter = false; 

            highlightCenterLittle = true; 

          } else { 

            highlightCenter = false; 

            highlightCenterLittle = false; 

          } 

          invalidate(); 

          break; 

        case MotionEvent.ACTION_UP: 

          if(highlightCenter && inCenter) {//点击在中心圆, 且当前启动在中心圆 

            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.onMeasure(mWidth, mHeight); 

    }
    /** 

     * 坐标是否在色环上 

     * @param x 坐标 

     * @param y 坐标 

     * @param outRadius 色环外半径 

     * @param inRadius 色环内半径 

     * @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; 

      } 

    }
    /** 

     * 坐标是否在中心圆上 

     * @param x 坐标 

     * @param y 坐标 

     * @param centerRadius 圆半径 

     * @return 

     */ 

    private boolean 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; 

      } 

    }
    /** 

     * 坐标是否在渐变色中 

     * @param x 

     * @param y 

     * @return 

     */ 

    private boolean inRect(float x, float y) { 

      if( x <= rectRight && x >=rectLeft && y <= rectBottom && y >=rectTop) { 

        return true; 

      } else { 

        return false; 

      } 

    }
    /** 

     * 获取圆环上颜色 

     * @param colors 

     * @param unit 

     * @return 

     */ 

    private int interpCircleColor(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 is 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); 

    }
    /** 

     * 获取渐变块上颜色 

     * @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 - s)); 

    } 

  }
  /** 

   * 回调接口
   */ 

  public interface OnColorChangedListener { 

    /** 

     * 回调函数 

     * @param color 选中的颜色 

     */ 

    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; 

  } 

}

测试界面
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 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(); 

  } 

  /**  

   * 初始化UI  

   */  

  private void initViews() {   

    btnColorPicker = (Button) findViewById(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() {   

          public void colorChanged(int color) {   

            tvText.setTextColor(color);   

          }   

        });   

        dialog.show(); 

      }   

    });   

    tvText = (TextView) findViewById(R.id.tv);   

  }   

}
(0)

相关推荐

  • android 字体颜色选择器(ColorPicker)介绍

    primary_text_yellow.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this f

  • android TextView设置中文字体加粗实现方法

    英文设置加粗可以在xml里面设置: 复制代码 代码如下: <SPAN style="FONT-SIZE: 18px">android:textStyle="bold"</SPAN> 英文还可以直接在String文件里面直接这样填写: 复制代码 代码如下: <string name="styled_text">Plain, <b>bold</b>, <i>italic</

  • Android编程实现TextView字体颜色设置的方法小结

    本文实例讲述了Android编程实现TextView字体颜色设置的方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. 复制代码 代码如下: tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android t

  • Android开发改变字体颜色方法

    在TextView中添加文本时有时会改变一些文本字体的颜色,今天主要分享三种实现方法及相关优缺点. 1.通过html标签改变文本颜色 复制代码 代码如下: tv.setText(Html.fromHtml("我是<font color=blue>danyijiangnan</font>")); 点评:通过Html.fromHtml()方法就可以在字符串中使用html的标签,通过font标签可以改变字体的格式.麦子学院-国内最专业的IT在线教育平台. 2.在act

  • Android开发笔记 改变字体颜色的三种方法

    1.在layout文件下的配置xml文件中直接设置字体颜色,通过添加android:textcolor="#FFFFFF"来变化颜色 但这样的效果只能让字体千篇一律的显示一种颜色 2.在activity中通过TextView tv=new TextView(this):实例化一个textview,通过setContentView(tv);将其加载到当前activity,设置要显示的内容String str="想要显示的内容":通过以下代码可以实现部分文本字体的改变,

  • Android TextView设置不同的颜色字体

    这里记录一个比较方便的方式来解决Textview设置不同颜色的字体的方法.可能第一反应是布局的嵌套,这个方法肯定可以啊,但是肯定不推荐啊,布局要尽量减少布局的嵌套,其次,使用自定义控件,U got it,不过确实有种小题大做的感觉,然后就是使用textview解析html,这个是个思路,可以实现.最后想到用SpannableStringBuilder,比较方便啊.确实... 1.代码很简单(具体的样式自己拓展吧): //textview TextView tvLatestdis= (TextVi

  • Android使用selector修改TextView中字体颜色和背景色的方法

    本文实例讲述了Android使用selector修改TextView中字体颜色和背景色的方法.分享给大家供大家参考,具体如下: android中的selector大家都很熟悉了,用它可以很方便的实现,控件在不同的动作中,颜色等值的变化.这里我说一下TextView中的一些应用. 我想大家都知道,Button按钮在源码上看是一种特殊的TextView,所以我们很多时候,按钮全是使用的TextView来完成,只要加一个android:clickable="true"就可以了. TextVi

  • Android TextView字体颜色设置方法小结

    本文实例总结了Android TextView字体颜色设置方法.分享给大家供大家参考,具体如下: 对于setTextView(int a)这里的a是传进去颜色的值.例如,红色0xff0000是指0xff0000如何直接传入R.color.red是没有办法设置颜色的,只有通过文章中的第三种方法先拿到资源的颜色值再传进去. tv.setTextColor(this.getResources().getColor(R.color.red)); 关键字: android textview color T

  • android 设置控件的颜色字体的方法

    1.用代码设置控件的颜色: 复制代码 代码如下: int b =  getResources().getColor(R.drawable.blue);//得到配置文件里的颜色    mButton.setTextColor(b); 2.设置空间的字体:方式一:mText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"));//设置字体   注意:1.保证文件一定是ttf格式:

  • Android系统更改状态栏字体颜色

    随着时代的发展,Android的状态栏都不是乌黑一片了,在Android4.4之后我们可以修改状态栏的颜色或者让我们自己的View延伸到状态栏下面.我们可以进行更多的定制化了,然而有的时候我们使用的是淡色的颜色比如白色,由于状态栏上面的文字为白色,这样的话状态栏上面的文字就无法看清了.因此本文提供一些解决方案,可以是MIUI6+,Flyme4+,Android6.0+支持切换状态栏的文字颜色为暗色. 修改MIUI public static boolean setMiuiStatusBarDar

随机推荐