package com.example.testsurfaceviewactivity;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class TestsurfaceviewActivity extends Activity {
private final static String TAG = "TestsurfaceviewActivity";
private int mScreenWidth,mScreenHeight;
private int stp;
private int mCnt=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
mScreenWidth = metric.widthPixels; // 屏幕宽度(像素)
mScreenHeight = metric.heightPixels; //
onHanlderTest();
}
private final static int MSG_UPDATE_START = 1;
private final static int MSG_UPDATE_STOP = 2;
private final static int MSG_UPDATE_REGION = 3;
private DrawViewer dv = null;
private myTestThread myDrawThread;
private Thread th;
private void onHanlderTest(){
dv = new DrawViewer(this);
// new Thread(new myTestThread()).start();
myDrawThread=new myTestThread();
th=new Thread(myDrawThread);
th.start();
this.setContentView(dv); //请务必注意这句,否则其内容将不是画在此DrawViewer上面
}
private class DrawViewer extends View {
private int center = 0;
public DrawViewer(Context ctx) {
super(ctx);
this.center = 30;
stp=10;
}
@Override
protected void onDraw(Canvas canvas){
Log.i(TAG,"onDraw curr position = "+ center);
center+= 10;
stp+=10;
if(stp>=mScreenWidth) stp = 0;
if(center >= 1280) center = 0;
Paint mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setAlpha(0x80);
canvas.drawCircle(center, 320, 40, mPaint);
mPaint.setColor(Color.BLUE);
canvas.drawLine(0, 360, 1280, 360, mPaint);
if(stp==0){
mCnt++;
if(mCnt>5){
mCnt=0;
}
}
switch(mCnt){
case 1://R
mPaint.setColor(Color.RED);
break;
case 2://G
mPaint.setColor(Color.GREEN);
break;
case 3://B
mPaint.setColor(Color.BLUE);
break;
case 4://W
mPaint.setColor(Color.WHITE);
this.setBackgroundColor(Color.BLACK);
break;
case 5://B
mPaint.setColor(Color.BLACK);
this.setBackgroundColor(Color.WHITE);
canvas.drawText("lcd end...",mScreenWidth/2 , mScreenHeight/2, mPaint);
break;
default:
}
if(mCnt==5){
if(th.isAlive()){
th.interrupt();
}
}
canvas.drawRect(0, 0, stp, mScreenHeight, mPaint);
}
}
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what){
case MSG_UPDATE_START:
Log.i(TAG,"revice MSG_UPDATE_START msg");
break;
case MSG_UPDATE_STOP:
Log.i(TAG,"revice MSG_UPDATE_STOP msg");
break;
case MSG_UPDATE_REGION:
Log.i(TAG,"revice MSG_UPDATE_REGION msg");
/**
* Invalidate the whole view. If the view is visible,
* onDraw(Canvas) will be called at some point in the future.
* This must be called from a UI thread. To call from a non-UI
* thread, call postInvalidate().
*/
dv.invalidate(); //实际调用的函数是DrawViewer中的onDraw()函数
break;
}
super.handleMessage(msg);
}
};
class myTestThread implements Runnable {
private void sendMsg(int what) {
Message message = new Message();
message.what = what;
myHandler.sendMessage(message);
}
public void run() {
this.sendMsg(MSG_UPDATE_START);
while (!Thread.currentThread().isInterrupted()) {
try {
this.sendMsg(MSG_UPDATE_REGION);
Thread.sleep(5);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
this.sendMsg(MSG_UPDATE_STOP);
}
}
}