2.一个随机生成多种颜色并获取颜色RGB值以及对应十六进制的安卓小应用


安卓学习中,每次选择颜色十六进制都让人头大,试着开发一个应用解决这个问题。


实现效果如下图:



主要功能:

1. 随机生成100种颜色(有重复,重复并不影响选择,没喜欢的颜色继续随机生成就行),点击相应颜色,下方红绿蓝的TextView会显示相应RGB值,左下TextView会现实该颜色值得十六进制表示。当前颜色变为选中颜色。

2. 可以拖动SeekBar调整RGB值,查看相应RGB值的颜色,同样当前颜色变为对应颜色。


实现:


1布局SurfaceView + 3 * SeekBar + 5 * TextView + Button。 没啥好说的--


2自定义dot类:该类用来存储自定义SurfaceView上的各个颜色的座标与颜色。(int x, y, R, G, B)


3自定义ColorSurface继承自SurfaceView。该类用来实现颜色的显示。

a  redraw()函数,用来绘制界面:

public void redraw(){
Canvas c = getHolder().lockCanvas();
if(c == null)
return;
Paint paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
for(int i=0; i<COL; ++i){
for(int j=0; j<COL; ++j){
randomRGB();
matrix[i][j].setR(R);
matrix[i][j].setG(G);
matrix[i][j].setB(B);
paint.setColor(changeColor(R, G, B));
c.drawRect(new RectF(i*WIDTH,j*HEIGHT,
(i+1)*WIDTH,(j+1)*HEIGHT), paint);
}
}
getHolder().unlockCanvasAndPost(c);
cc.setRunning(false);
}

其中matrix为dot数组,WIDTH,HEIGHT则根据View大小计算相应大小(View.width/COL),RandomRGB()随机产生0-255的整数,changeColor()函数将3个int型R G B值转化为RGB值对应整数值。

private void randomRGB(){
R = (int)(Math.random()*255);
G = (int)(Math.random()*255);
B = (int)(Math.random()*255);
}

private int changeColor(int R, int G, int B){
int color=0, t1, t2;
color = 15*(int)Math.pow(16, 7) + 15*(int)Math.pow(16, 6);
t1 = R/16;
t2 = R%16;
color = color + t1*(int)Math.pow(16, 5) + t2*(int)Math.pow(16, 4);
t1 = G/16;
t2 = G%16;
color = color + t1*(int)Math.pow(16, 3) + t2*(int)Math.pow(16, 2);
t1 = B/16;
t2 = B%16;
color = color + t1*(int)Math.pow(16, 1) + t2*(int)Math.pow(16, 0);
return color;
}


b 使用CallBack进行回调监听View的变化以便重新计算WIDTH,HEIGHT。


c 每次重绘时在新线程中进行重绘,避免在主线程中执行耗时操作。


d 设置OnTouchListener,此时matrix中存储的数据就起作用啦,根据获取的XY座标值,计算对应的matrix中的元素,获取该元素存储的RGB值。

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

if(event.getAction() == MotionEvent.ACTION_UP){
int x, y;
x = (int)((event.getX())/WIDTH);
y = (int)((event.getY())/HEIGHT);
if(x+1 > COL || y+1 > COL || y < 0 || x < 0 ){
return true;
}
R = matrix[x][y].getR();
G = matrix[x][y].getG();
B = matrix[x][y].getB();
MainActivity.getMainActivity().setR(R);
MainActivity.getMainActivity().setG(G);
MainActivity.getMainActivity().setB(B);
MainActivity.getMainActivity().setRGB();
MainActivity.getMainActivity().set0xRGB();
MainActivity.getMainActivity().setSeekBar();
}

return true;
}


4. 实现MainActivity中相应的方法。

private String rgb0x[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

public MainActivity() {
mainActivity = this;
}

public static MainActivity getMainActivity() {
return mainActivity;
}


public int getR(){
return Red;
}

public int getG(){
return Green;
}

public int getB(){
return Blue;
}


public void setRGB(){
tv1.setText("Red:" + Red + "");
tv5.setText("Green:" + Green + "");
tv3.setText("Blue:" + Blue + "");
}

public void set0xRGB(){
int t1,t2;
String str = "#";

t1 = Red/16;
t2 = Red%16;
str += rgb0x[t1] + rgb0x[t2];


t1 = Green/16;
t2 = Green%16;
str += rgb0x[t1] + rgb0x[t2];

t1 = Blue/16;
t2 = Blue%16;
str += rgb0x[t1] + rgb0x[t2];
tv4.setText(str);
tv2.setBackgroundColor(changeColor(Red, Green, Blue));
}


OK完成,以后再也不担心找不到好看的颜色了。



资料地址:http://download.csdn.net/detail/u012951394/9077241

发布了25 篇原创文章 · 获赞 1 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章