Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> android 怎麼畫心

android 怎麼畫心

編輯:Android開發教程

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();
        }
    }
}

  效果如下:

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved