package com.android.xiong.gridlayoutTest;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MathActivity extends Activity implements OnClickListener {
/**
* @author zhengmin
*/
private SurfaceView sv;
// 初始化画笔
private Paint paint;
final int WIDTH = 768;
final int HEIGHT = 320;
final int X_OFFSET = 5;
private int cx = X_OFFSET;
// 实际Y轴的位置
int centerY = HEIGHT / 2;
Timer timer = new Timer();
TimerTask task = null;
private Button btn_sin, btn_cos;
private SurfaceHolder holder;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math);
sv = (SurfaceView) findViewById(R.id.sv);
holder = sv.getHolder();
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(3);
initView();
initEvent();
}
private void initEvent() {
// 监听事件
btn_cos.setOnClickListener(this);
btn_sin.setOnClickListener(this);
}
private void initView() {
// 初始化控件
btn_cos = (Button) findViewById(R.id.btn_cos);
btn_sin = (Button) findViewById(R.id.btn_sin);
}
@Override
public void onClick(final View v) {
// TODO Auto-generated method stub
drawBack(holder);
cx = X_OFFSET;
if (task != null) {
task.cancel();
// 取消任务
}
task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
int cy = (v.getId() == R.id.btn_sin ? centerY
- (int) (100 * Math.sin((cx - 5) * 2) * Math.PI / 150)
: centerY
- (int) (100 * Math.cos((cx - 5) * 2 * Math.PI
/ 150)));
Canvas canvas = holder.lockCanvas(new Rect(cx, cy - 2, cx + 2,
cy + 2));
canvas.drawPoint(cx, cy, paint);
// 画点
cx++;
if (cx > WIDTH) {
task.cancel();
task = null;
}
holder.unlockCanvasAndPost(canvas);
}
};
timer.schedule(task, 0, 30);
holder.addCallback(new Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
timer.cancel();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
drawBack(holder);
}
});
}
private void drawBack(SurfaceHolder holder) {
// TODO Auto-generated method stub
Canvas canvas = holder.lockCanvas();
// 绘制白色背景
canvas.drawColor(Color.WHITE);
Paint p = new Paint();
p.setColor(Color.BLACK);
p.setStrokeWidth(2);
// 绘制坐标轴
canvas.drawLine(X_OFFSET, centerY, WIDTH, centerY, p);
canvas.drawLine(X_OFFSET, 40, X_OFFSET, HEIGHT, p);
holder.unlockCanvasAndPost(canvas);
holder.lockCanvas(new Rect(0, 0, 0, 0));
holder.unlockCanvasAndPost(canvas);
}
}
activity_math.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="@+id/btn_sin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="正弦函数" />
<Button
android:id="@+id/btn_cos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:padding="10dp"
android:text="余弦函数" />
</LinearLayout>
<SurfaceView
android:id="@+id/sv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这里需要注意:
1> 所有SurfaceView和SurfaceHolder.Callback的方法都应该在UI(Main)线程里调用,也就是程序的主线程。
渲染线程所要访问的各种变量应该作同步处理。
2> 由于surface可能被销毁,它只在SurfaceHolder.Callback.surfaceCreated()和 SurfaceHolder.Callback.surfaceDestroyed()之间有效,所以要确保渲染线程访问的是合法有效的surface。
效果如图所示