package com.example.mysurfaceview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
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.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button btnSimpleDraw, btnTimerDraw;
SurfaceView svSurfaceView;
SurfaceHolder shSurfaceHolder;
private Timer mTimer;
private MyTimerTask mTimerTask;
int Y_axis[];// 保存正弦波的Y轴上的点
int centerY;// 中心线
int oldX, oldY;// 上一个XY点
int currentX;// 当前绘制到的X轴上的点
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSimpleDraw = (Button) this.findViewById(R.id.Button01);
btnTimerDraw = (Button) this.findViewById(R.id.Button02);
btnSimpleDraw.setOnClickListener(new ClickEvent());
btnTimerDraw.setOnClickListener(new ClickEvent());
svSurfaceView = (SurfaceView) this.findViewById(R.id.SurfaceView01);
shSurfaceHolder = svSurfaceView.getHolder();
// 动态绘制正弦波的定时器
mTimer = new Timer();
mTimerTask = new MyTimerTask();
// 初始化y轴数据
centerY = (getWindowManager().getDefaultDisplay().getHeight() - svSurfaceView.getTop()) / 2;
Y_axis = new int[getWindowManager().getDefaultDisplay().getWidth()];
for (int i = 1; i < Y_axis.length; i++) {// 计算正弦波
Y_axis[i - 1] = centerY - (int) (100 * Math.sin(i * 2 * Math.PI / 180));
}
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if (v == btnSimpleDraw) {
SimpleDraw(Y_axis.length - 1);// 直接绘制正弦波
} else if (v == btnTimerDraw) {
oldY = centerY;
//mTimer = new Timer();
mTimer.schedule(mTimerTask, 0, 5);// 动态绘制正弦波
}
}
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
SimpleDraw(currentX);
currentX++;// 往前进
if (currentX == Y_axis.length - 1) {// 如果到了终点,则清屏重来
ClearDraw();
currentX = 0;
oldY = centerY;
}
}
}
/*
* 绘制指定区域
*/
void SimpleDraw(int length) {
if (length == 0) {
oldX = 0;
}
Canvas canvas = shSurfaceHolder.lockCanvas(new Rect(oldX, 0, oldX + length, getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
Log.i("Canvas:", String.valueOf(oldX) + "," + String.valueOf(oldX + length));
Paint mPaint = new Paint();
mPaint.setColor(Color.GREEN);// 画笔为绿色
mPaint.setStrokeWidth(2);// 设置画笔粗细
/* // 获取当前时间
Date curDate = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss sss");
String sNow = sdf.format(curDate);
canvas.drawText("時間 "+sNow, 100, 100, mPaint);*/
/*
* InputStream is =
* getResources().openRawResource(R.drawable.ic_launcher);
*
* Bitmap mBitmap = BitmapFactory.decodeStream(is);
*
* canvas.drawBitmap(mBitmap, 0, 0, null);
*/
int y;
for (int i = oldX + 1; i < length; i++) {// 绘画正弦波
y = Y_axis[i - 1];
canvas.drawLine(oldX, oldY, i, y, mPaint);
oldX = i;
oldY = y;
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
Bitmap mBitmap = BitmapFactory.decodeStream(is);
canvas.drawBitmap(mBitmap, 0, 0, null);
// 获取当前时间
Date curDate = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss sss");
String sNow = sdf.format(curDate);
canvas.drawText("time "+sNow, 100, 100, mPaint);
}
shSurfaceHolder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
}
/**
* 清除畫圖內容
*/
void ClearDraw() {
Canvas canvas = shSurfaceHolder.lockCanvas(null);
canvas.drawColor(Color.BLACK);// 清除画布
shSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
surfaceview-自定義畫圖或顯示圖片
最新推荐文章于 2024-05-08 17:28:07 发布