Android - 在SurfaceView上绘制简单的正弦波

时间:2013-06-03 21:02:10

标签: android xml drawing surfaceview

我有一个带有视图的活动,其中包含一些UI元素,如滑块,一些textView和一个surfaceView。我想在surfaceView上绘制一个简单的正弦波,除了我不知道如何做到这一点。我能找到的教程并没有解决我的问题。

基本上,如何在我的xml布局文件中定义的surfaceView上绘制正弦波?

2 个答案:

答案 0 :(得分:1)

要在SurfaceView上绘图,您需要编写自己的类来扩展SurfaceView。在XML布局文件中使用此类。然后,您必须覆盖onDraw(Canvas canvas)才能进行绘图。使用Math.sin(double a)来获得窦。

答案 1 :(得分:0)

好的,这是在Surface View上绘制正弦的一种简单有趣的方法。当然,这一切都是以错误的方式完成的,但很好地说明了原则。

我知道Google对退出按钮和disagree的看法。所以那里。

MainActivity.java:

package com.gladeworks.gwscope ;

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;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{
    TextView textView = null;
    SurfaceView surfaceView;
    SurfaceHolder holder;
    Canvas canvas;
    Paint paint;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        textView.setText("Press the Draw button!");
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        holder = surfaceView.getHolder();
        holder.getSurface();
        paint = new Paint();

        final Button buttonExit = (Button) findViewById(R.id.buttonExit);
        buttonExit.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                exit(view);
            }
        });
        final Button buttonDraw = (Button) findViewById(R.id.buttonDraw);
        buttonDraw.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                draw(view);
            }
        });
    }


    protected void draw(View view)  //This gets called when the Draw button is clicked.
    {
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);

        canvas = holder.lockCanvas();
        if (canvas == null) {
            textView.setText("Canvas is still null");
        } else {
            float a, w,h,x,y;
            w=canvas.getWidth();
            h=canvas.getHeight();
            canvas.drawRGB(32, 32, 255);    //Clear the canvas to light blue
            canvas.drawLine(0,h/2,(int)w,h/2,paint);//Draw a line from left to right as the y=0 line.
            x=0;
            while(x<w)
            {
                a=(x/w)*((float)2.0*(float)3.131592654);
                y=(h/2)-((float)Math.sin(a)*(h/(float)2.1));

                canvas.drawPoint(x,y,paint);

                x++;
            }


            holder.unlockCanvasAndPost(canvas);
            textView.setText("Done drawing!, Canvas size="+w+"x"+h);
        }


    }

    protected void exit(View view)
    {
        System.exit(0);
    }

}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gladeworks.gwscope.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Uninitialized"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignBottom="@+id/buttonExit"
        android:layout_toLeftOf="@+id/buttonExit"
        android:layout_toStartOf="@+id/buttonExit"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quit"
        android:id="@+id/buttonExit"
        android:layout_alignTop="@+id/buttonDraw"
        android:layout_toLeftOf="@+id/buttonDraw"
        android:layout_toStartOf="@+id/buttonDraw"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draw"
        android:id="@+id/buttonDraw"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonExit"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</RelativeLayout>