利用Android的Canvas绘制正弦函数图像

一时兴起用android原生的基础绘制工具,绘制了一个正弦(余弦)函数图象,不多说,先上图:

上图

MainView.java类

package com.zhaofeng.main;

import javax.crypto.SealedObject;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MainView extends Activity {
    /** Called when the activity is first created. */
	
	private SurfaceView surfaceView;
	private Paint paint;
	private SurfaceHolder surfaceHolder;
	private int scale_x = 20;
	private int scale_y = 50;
	private double ox=10;
	private double oy=240;
	private double x=0;
	private double y=0;
	private double t=0;
	private boolean flag=true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        surfaceView = (SurfaceView) findViewById(R.id.main_suf);
        surfaceHolder = surfaceView.getHolder();
        
        paint = new Paint();
        
      surfaceHolder.addCallback(new Callback() {
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					while (flag) {
						try {
							Thread.sleep(10);
							Canvas canvas=surfaceHolder.lockCanvas();
							if(canvas!=null)
							{
								//绘制坐标轴
								paint.setColor(Color.WHITE);
								canvas.drawText("O",20,220, paint);
								paint.setColor(Color.BLUE);
								canvas.drawLine(10,10,10,480, paint);
								canvas.drawText("Y",20,30, paint);
								paint.setColor(Color.GREEN);
								canvas.drawLine(0,240,320,240, paint);
								canvas.drawText("X",300,260, paint);
								//绘制正弦曲线
								t+=0.1;
									x=t*scale_x;
									y=Math.sin(t)*scale_y+240;
									System.out.println("---------"+Math.round(Math.sin(t)));
									if (t>0) {
										paint.setColor(Color.RED);
										canvas.drawLine(10+(int)ox,(int)oy,10+(int)x,(int)y, paint);
										System.out.println(x+"s"+y);
									}
									ox=x;
									oy=y;
									if (t>15) {
										flag=false;
									}
							}
							surfaceHolder.unlockCanvasAndPost(canvas);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}).start();
		}
		
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub
		}
	});
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<SurfaceView  
	android:id="@+id/main_suf"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="@string/hello"
    />
</LinearLayout>


发布了30 篇原创文章 · 获赞 46 · 访问量 13万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览