android 如何画心

   先前写了一个Windows版的画心,现在想把windows版的画心变成安卓版的。

xml布局:

<?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:background="#ffe1e1e1"
    android:orientation="vertical"
    tools:context="com.ahcc08.ahcc08.loveheart.MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@drawable/main_header">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="LoveHeart"
            android:textColor="#ffffffff"
            android:textSize="26sp" />
    </RelativeLayout>
    <Button android:id="@+id/buttonStart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点我开始画心"/>
    <SurfaceView
        android:id="@+id/surfaceView_main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </SurfaceView>
</LinearLayout>
MainActivity.java
package com.ahcc08.ahcc08.loveheart;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    SurfaceView surfaceView_main;
    SurfaceHolder surfaceHolder;
    Button buttonStart;
    private int m_iCenterPt;
    private int currentX;
    private int m_iMaxStep = 80;
    private Timer mTimer = null;
    private MyTimerTask mTimerTask = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        surfaceView_main = (SurfaceView) this.findViewById(R.id.surfaceView_main);
        surfaceHolder = surfaceView_main.getHolder();

        buttonStart = (Button) this.findViewById(R.id.buttonStart);

        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClearDraw();
                InitTimer();
                mTimer.schedule(mTimerTask, 0, 5);
            }
        });

        //
        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        //窗口的宽度
        int screenWidth = dm.widthPixels;
        m_iCenterPt = screenWidth / 2;

        Log.i("onCreate:",
                String.valueOf(m_iCenterPt) );
    }

    class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            SimpleDraw(currentX);
            currentX++;//往前进
            if (currentX == m_iMaxStep) {
                StopTimer();
                currentX = 0;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        StopTimer();
    }

    private void InitTimer() {
        mTimer = new Timer();
        mTimerTask = new MyTimerTask();
    }

    private void StopTimer() {
       if ( mTimer != null) {
           mTimer.cancel();
           mTimer = null;
       }
        if ( mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }

    /*
    * 绘制指定区域
    */
    void SimpleDraw(int length) {
        Canvas canvas = surfaceHolder.lockCanvas(null);
        Paint mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(5);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);

        for (int i = 0; i < length; i++) {
            double angle = 2 * Math.PI / length * i;
            //桃形心
            double x = m_iCenterPt - 16 * (Math.sin(angle) * Math.sin(angle) * Math.sin(angle)) * 20;
            double y = m_iCenterPt - (13 * Math.cos(angle) - 5 * Math.cos(2 * angle) - 2 * Math.cos(3 * angle) - Math.cos(4 * angle)) * 20;
            Log.i("Canvas:",
                    String.valueOf(x) + "," + String.valueOf(y ));
            canvas.drawPoint((int)x, (int)y, mPaint);
        }
        surfaceHolder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
    }
    void ClearDraw() {
        Canvas canvas = surfaceHolder.lockCanvas(null);
        canvas.drawColor(Color.BLACK);// 清除画布
        surfaceHolder.unlockCanvasAndPost(canvas);
        try {
            Thread.sleep(1 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

  效果如下:

posted @ 2017-04-16 14:54  高山百川  阅读(420)  评论(0编辑  收藏  举报