Android的Canvas绘制正弦函数图像

package com.baozilichao.test009;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainActivity extends AppCompatActivity {
    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.activity_main);
        surfaceView = (SurfaceView) findViewById(R.id.main_suf);
        surfaceHolder = surfaceView.getHolder();
        paint = new Paint();
        surfaceHolder.addCallback(new SurfaceHolder.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
            }
        });
    }
}
 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.baozilichao.test009.MainActivity">
    <SurfaceView
        android:id="@+id/main_suf"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

        />

</LinearLayout>


Android中,可以使用Canvas的drawArc()方法来绘制圆弧。首先,需要创建一个Paint对象来设置画笔的属性,比如颜色、宽度等。在绘制圆弧之前,可以调用setAntiAlias()方法取消锯齿,可以使用setStyle()方法来设置画笔的样式,例如填充、描边等。然后,定义一个RectF对象来确定圆弧所在的矩形区域。最后,调用canvas的drawArc()方法来绘制圆弧。 下面是一个示例代码,展示了如何在自定义的View中使用Canvas绘制圆弧: ```java public class CustomProgress extends View { private Paint mPaint; private int mCircleWidth = 3; public CustomProgress(Context context) { this(context, null); } public CustomProgress(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomProgress(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); mPaint.setStrokeWidth(mCircleWidth); mPaint.setColor(Color.CYAN); float x = (getWidth() - getHeight() / 2) / 2; float y = getHeight() / 4; RectF oval = new RectF(x, y, getWidth() - x, getHeight() - y); canvas.drawArc(oval, 360, 140, true, mPaint); } } ``` 以上代码展示了一个自定义的View类CustomProgress,在onDraw()方法中使用Canvas绘制了一个圆弧。在绘制圆弧之前,先设置了画笔的属性,然后定义了一个矩形区域,最后调用drawArc()方法绘制了圆弧。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Android 画圆弧canvas.drawArc() 详解](https://blog.csdn.net/csdn_aiyang/article/details/71616979)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Android 绘图基础:Canvas画布——自定义View基础(绘制表盘、矩形、圆形、弧、渐变)](https://blog.csdn.net/YllP_1230/article/details/129321090)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值