当前位置: 代码迷 >> Android >> Android LCD背景色彩和边框颜色的可控制
  详细解决方案

Android LCD背景色彩和边框颜色的可控制

热度:46   发布时间:2016-05-01 16:49:28.0
Android LCD背景颜色和边框颜色的可控制
我们大家都知道android LCD背景颜色和边框颜色的是可控制,实现在mountionEvent中对onClick和onLongClick的区分,利用线程返回不同的值;实现边框颜色和背景颜色的改变,那么我们来看看下面的代码,是怎么实现LCD的背景颜色和边框颜色。


package eoe.android;

import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.graphics.Paint.Style;
import android.os.Bundle;
import android.os.Handler;

import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class ScreenChange extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// MyView.this.setOnClickListener(mClickListener);

requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置成全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(new MyView(this));

//dm.heightPixels=100;
//dm.widthPixels=200;

//this.setOnLongClickListener(new OnLongClickListener(){

//getWindowManager().addView(arg0, arg1)
}

public class MyView extends SurfaceView implements
SurfaceHolder.Callback,android.view.View.OnKeyListener,android.view.View.OnTouchListener,
android.view.View.OnClickListener,android.view.View.OnLongClickListener {
boolean mLong;
boolean handled;
private static final int MAX_TOUCHPOINTS = 10;
// private static final String START_TEXT = "请触摸屏幕进行测试";
private Paint textPaint = new Paint();
private Paint touchPaints[] = new Paint[MAX_TOUCHPOINTS];
private int colors[] = new int[MAX_TOUCHPOINTS];

private int width, height;
private float scale = 1.0f;

private float timesecond = (float)5;

private int screencolor = 1;
private int backcolor = 1;

long timedown;
private long timeperiod;

protected OnClickListener mOnClickListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
if(backcolor==10){backcolor=1;}
backcolor++;
}
};


public void setOnClickListener(OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
mOnClickListener = l;
}

public OnLongClickListener mOnLongClickListener =new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//if (onLongPress != null) {
// onLongPress.onLongPress(v);
//}
if(screencolor==10){screencolor=1;}
changebackcolor(screencolor);
screencolor++;
return true;
}


};

public void setOnLongClickListener(OnLongClickListener l) {
if (!isLongClickable()) {
setLongClickable(true);
}
mOnLongClickListener = l;

}

SurfaceHolder holder;
public MyView(Context context) {
super(context);
SurfaceHolder holder = getHolder();
holder.addCallback(this);
setFocusable(true); // 确保View能获得输入焦点
setFocusableInTouchMode(true); // 确保能接收到触屏事件
setOnClickListener(mOnClickListener);
setOnLongClickListener(mOnLongClickListener);
init();

}

private void init() {
// 初始化10个不同颜色的画笔
textPaint.setColor(Color.WHITE);
textPaint.setAlpha(0x40);
colors[0] = 1;
colors[1] = 2;
colors[2] = 3;
colors[3] = 4;
colors[4] = 5;
colors[5] = 6;
colors[6] = 7;
colors[7] = 8;
colors[8] = 9;
colors[9] = 10;
for (int i = 0; i < MAX_TOUCHPOINTS; i++) {
touchPaints[i] = new Paint();
touchPaints[i].setColor(colors[i]);
}
}


/*
* 处理触屏事件
*/

@Override
public boolean onTouchEvent(MotionEvent event) {
// 获得屏幕触点数量
int pointerCount = event.getPointerCount();
if (pointerCount > MAX_TOUCHPOINTS) {
pointerCount = MAX_TOUCHPOINTS;
}
// 锁定Canvas,开始进行相应的界面处理
Canvas c = getHolder().lockCanvas();
if (c != null) {
// c.drawColor(Color.BLACK);
if (event.getAction() == MotionEvent.ACTION_UP) {
handler1.removeCallbacks(runnable);
if (!handled) {
performClick();


}
switch(backcolor){
case 1:
c.drawColor(Color.GREEN);
break;
case 2:
c.drawColor(Color.DKGRAY);
break;
case 3:
c.drawColor(Color.GRAY);
break;
case 4:
c.drawColor(Color.LTGRAY);
break;
case 5:
c.drawColor(Color.MAGENTA);
break;
case 6:
c.drawColor(Color.RED);
break;
case 7:
c.drawColor(Color.WHITE);
break;
case 8:
c.drawColor(Color.YELLOW);
break;
case 9:
c.drawColor(Color.TRANSPARENT);
break;
case 10:
c.drawColor(Color.CYAN);
break;
default:
break;

}

textPaint.setStyle(Style.STROKE);
textPaint.setStrokeWidth(10);
c.drawRect(0, 0, this.width, this.height, textPaint);

} else if(event.getAction() == MotionEvent.ACTION_DOWN) {


handled=false;
timedown = event.getEventTime();
postCheckForLongClick(c);
switch(backcolor){
case 1:
//this.setBackgroundColor(Color.GREEN);
c.drawColor(Color.GREEN);
//getHolder().lockCanvas().drawColor(Color.GREEN);
break;
case 2:
c.drawColor(Color.DKGRAY);
break;
case 3:
c.drawColor(Color.GRAY);
break;
case 4:
c.drawColor(Color.LTGRAY);
break;
case 5:
c.drawColor(Color.MAGENTA);
break;
case 6:
c.drawColor(Color.RED);
break;
case 7:
c.drawColor(Color.WHITE);
break;
case 8:
c.drawColor(Color.YELLOW);
break;
case 9:
c.drawColor(Color.TRANSPARENT);
break;
case 10:
c.drawColor(Color.CYAN);
break;
default:
break;

}
textPaint.setStyle(Style.STROKE);
textPaint.setStrokeWidth(10);
c.drawRect(0, 0, this.width, this.height, textPaint);
}
else{
textPaint.setStyle(Style.STROKE);
textPaint.setStrokeWidth(10);
c.drawRect(0, 0, this.width, this.height, textPaint);
}


getHolder().unlockCanvasAndPost(c);
}
return true;
}

public boolean performClick() {
if (mOnClickListener != null) {
mOnClickListener.onClick(this);
return true;
}
return false;
}

Handler handler1=new Handler();
Runnable runnable=new Runnable(){

public void run() {
// TODO Auto-generated method stub
//要做的事情
// mOnLongClickListener.onLongClick(this);
Date timeNow = new Date(System.currentTimeMillis());
timeperiod = (long)timeNow.getTime()-(long)timedown;
if(timeperiod > timesecond){
handler1.postDelayed(runnable, 500);
performLongClick();

mLong = true;
handler1.removeCallbacks(runnable);
}
else{
handler1.removeCallbacks(runnable);
}
}
};


private void postCheckForLongClick(Canvas c) {
mLong = false;
handler1.postDelayed(runnable, 500);
if(mLong == true){
performLongClick();
}
}

public boolean performLongClick() {
if (mOnLongClickListener != null) {
handled = mOnLongClickListener.onLongClick(this);
mLong = true;
}
if(mLong == true)handler1.removeCallbacks(runnable);
return handled;

}

public void changebackcolor(int pc){
switch(pc){
case 1:
//this.setBackgroundColor(Color.GREEN);
textPaint.setColor(Color.GREEN);
//getHolder().lockCanvas().drawColor(Color.GREEN);
break;
case 2:
textPaint.setColor(Color.DKGRAY);
break;
case 3:
textPaint.setColor(Color.GRAY);
break;
case 4:
textPaint.setColor(Color.LTGRAY);
break;
case 5:
textPaint.setColor(Color.MAGENTA);
break;
case 6:
textPaint.setColor(Color.RED);
break;
case 7:
textPaint.setColor(Color.WHITE);
break;
case 8:
textPaint.setColor(Color.YELLOW);
break;
case 9:
textPaint.setColor(Color.TRANSPARENT);
break;
case 10:
textPaint.setColor(Color.CYAN);
break;
default:
break;
}
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {

this.width = width;
this.height = height;
if (width > height) {
this.scale = width / 480f;
} else {
this.scale = height / 480f;
}
textPaint.setTextSize(14 * scale);

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
//new Thread(new MyThread()).start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onClick(View v ) {
// TODO Auto-generated method stub
}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
return false;
}

/*
//内部类的内部类
class MyThread implements Runnable{

@Override
public void run() {
Canvas canvas = holder.lockCanvas(null);//获取画布
Paint mPaint = new Paint();
mPaint.setColor(Color.BLUE);

canvas.drawRect(new RectF(40,60,80,80), mPaint);
holder.unlockCanvasAndPost(canvas);//解锁画布,提交画好的图像

}

}
*/

}
}
  相关解决方案