SurfaceView基本使用--动态画正弦函数

SurfaceView基本使用--动态画正弦函数_第1张图片SurfaceView基本使用--动态画正弦函数_第2张图片SurfaceView基本使用--动态画正弦函数_第3张图片SurfaceView基本使用--动态画正弦函数_第4张图片

 

package com.zzw.TestSurfaceView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


/**
 * Created by zzw on 2016/6/28.
 * 描述:
 */
public class SinSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private int mWidth;
    private int mHeight;

    private double angle = 0;

    private SurfaceHolder mHolder;
    /**
     * 与SurfaceHolder绑定的Canvas
     */
    private Canvas mCanvas;
    /**
     * 用于绘制的线程
     */
    private Thread t;
    /**
     * 线程的控制开关
     */
    private boolean isRunning;


    public SinSurfaceView(Context context) {
        this(context, null);
    }

    public SinSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SinSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        mWidth = dm.widthPixels;
        mHeight = dm.heightPixels;

        mHolder = getHolder();
        mHolder.addCallback(this);

        // setZOrderOnTop(true);// 设置画布 背景透明
        // mHolder.setFormat(PixelFormat.TRANSLUCENT);

        //设置可获得焦点
        setFocusable(true);
        setFocusableInTouchMode(true);
        //设置常亮
        this.setKeepScreenOn(true);

    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isRunning = true;
        t = new Thread(this);
        t.start();
    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // 通知关闭线程
        isRunning = false;
    }

    @Override
    public void run() {

        // 不断的进行draw
        while (isRunning) {
            draw();
        }
    }


    private void drawCoordinates() {
        Path mPath = new Path();
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);//设置画笔类型
        paint.setAntiAlias(true);//抗锯齿
        paint.setStyle(Paint.Style.STROKE);//设置为空心

        mCanvas.translate(20, mHeight / 2);// 将画布坐标原点移动到中心位置
        //绘制坐标原点
        paint.setColor(Color.RED);//设置画笔颜色
        paint.setStrokeWidth(10);//为了看得清楚,设置了较大的画笔宽度
        //绘制坐标轴4个断点
        mCanvas.drawPoints(new float[]{0, 0,
                mWidth * 0.9f, 0
                , mWidth * 0.9f * 0.75f, 0
                , mWidth * 0.9f * 0.5f, 0
                , mWidth * 0.9f * 0.25f, 0}, paint);

        paint.setStrokeWidth(1);//恢复画笔默认宽度

        mCanvas.drawLine(0, 0, mWidth * 0.9f, 0, paint);
        mCanvas.drawLine(0, -mHeight / 2 * 0.9f, 0, mHeight / 2 * 0.9f, paint);

        mPath.moveTo(mWidth * 0.9f * 0.95f, -mWidth * 0.9f * 0.05f);
        mPath.lineTo(mWidth * 0.9f, 0);
        mPath.lineTo(mWidth * 0.9f * 0.95f, mWidth * 0.9f * 0.05f);
        //绘制Path
        mCanvas.drawPath(mPath, paint);
    }


    private void drawSin() {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);//设置画笔类型
        paint.setAntiAlias(true);//抗锯齿
        paint.setStyle(Paint.Style.STROKE);//设置为空心
        paint.setStrokeWidth(5);
        //绘制坐标原点
        paint.setColor(Color.RED);//设置画笔颜色

        double x = mWidth * 0.9f * (angle / 360);
//        double y = Math.sin((Math.PI / 180) * angle) * mWidth * 0.9f * 0.2;// π/180乘于角度为弧度
//        mCanvas.drawPoint((float) x, -(float) y, paint);
        for (int i = 1; i < 6; i++) {
            double y = Math.sin((Math.PI / 180) * angle) * mWidth * 0.9f * 0.15 * i;// π/180乘于角度为弧度
            mCanvas.drawPoint((float) x, -(float) y, paint);
        }

        angle += 1;
        if (angle > 360) {
            isRunning = false;
        }
    }

    private void draw() {
        try {
            Thread.sleep(1);
            // 获得canvas
            mCanvas = mHolder.lockCanvas();
            if (mCanvas != null) {
                drawCoordinates();
                drawSin();
            }
        } catch (Exception e) {
        } finally {
            if (mCanvas != null)
                mHolder.unlockCanvasAndPost(mCanvas);
        }
    }
}

 

转载于:https://www.cnblogs.com/zzw1994/p/5623835.html

你可能感兴趣的:(SurfaceView基本使用--动态画正弦函数)