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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章