当前位置:操作系统 > 安卓/Android >>

Su易做图ceView的一个小应用:开发示波器

Su易做图ceView与普通View还有一个重要区别:View的绘图必须在UI线程中进行,但Su易做图ceView不存在这个问题,因为它是由Su易做图ceHolder来完成的。所以对于View组件,如果绘图时间过长,会阻塞UI主线程,而Su易做图ceHolder则会启动新的线程去更新Su易做图ceView的绘制,不会阻塞UI线程。

下面的程序是通过Su易做图ceView绘制正玄曲线和余玄曲线的示例,代码如下:

Activity:

 

package com.home.showwave;

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.Su易做图ceHolder;
import android.view.Su易做图ceHolder.Callback;
import android.view.Su易做图ceView;
import android.view.View;

public class ShowWaveActivity extends Activity {
	private Su易做图ceHolder holder;
	private Su易做图ceView su易做图ce;
	private Paint paint;
	private final int HEIGHT = 320;
	// 要绘制的曲线的水平宽度
	private final int WIDTH = 500;
	// 离屏幕左边界的起始距离
	private final int X_OFFSET = 5;
	// 初始化X坐标
	private int cx = X_OFFSET;
	// 实际的Y轴的位置
	private int centerY = HEIGHT / 2;
	private Timer timer = new Timer();
	private TimerTask task = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_wave);
		// 获得Su易做图ceView对象
		su易做图ce = (Su易做图ceView) findViewById(R.id.activity_show_wave_sv);
		// 初始化Su易做图ceHolder对象
		holder = su易做图ce.getHolder();
		paint = new Paint();
		paint.setColor(Color.GREEN);
		paint.setStrokeWidth(3);

	}

	public void click(final View v) {
		drawBackGround(holder);
		cx = X_OFFSET;
		if (task != null) {
			task.cancel();
		}
		task = new TimerTask() {

			@Override
			public void run() {
				// 根据是正玄还是余玄和X坐标确定Y坐标
				int cy = v.getId() == R.id.activity_show_wave_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));
				// 根据X,Y坐标画点
				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 su易做图ceChanged(Su易做图ceHolder holder, int format,
					int width, int height) {
				drawBackGround(holder);
			}

			@Override
			public void su易做图ceCreated(Su易做图ceHolder holder) {

			}

			@Override
			public void su易做图ceDestroyed(Su易做图ceHolder holder) {
				timer.cancel();
			}
		});
	}

	private void drawBackGround(Su易做图ceHolder holder) {
		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);
	}
}
<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:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/activity_show_wave_btn_sin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="正玄曲线" />

        <Button
            android:id="@+id/activity_show_wave_btn_cos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="余玄曲线" />
    </LinearLayout>

    <Su易做图ceView
        android:id="@+id/activity_show_wave_sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,