SurfaceView 绘制分形图

之前一直做的是应用类,这次抽时间,参考网上资料实践了下SurfaceView。目标是在页面上画一个科赫曲线的分形图。

代码如下:

package com.example.fredric.demo02;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;

public class MainActivity extends AppCompatActivity {

    private int width;
    private int height;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();
        setContentView(new MyView(this));
    }

    //知识点备注:
    //1、Surface:原始图像缓冲区的句柄,通过该接口可以获取原始缓冲区中的数据;
    //Surface 中包含canvas对象
    //2、SurfaceView:继承至View,
    // 2.1 提供了一个运行在渲染线程的surface
    // 2.2 SurfaceView 和 SurfaceHolder.Callback的方法都应该在主线程(UI线程)里面调用
    //3、SurfaceHolder:控制surface的一个抽象接口
    //4、SurfaceHolder.Callback: 监听Surface改变的接口
    class MyView extends SurfaceView implements SurfaceHolder.Callback {

        private SurfaceHolder holder;
        private MyThread myThread;

        public MyView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            holder = this.getHolder();
            holder.addCallback(this);
            myThread = new MyThread(holder);
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            myThread.isRun = true;
            myThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            myThread.isRun = false;
        }

        class MyThread extends Thread {
            private SurfaceHolder holder;
            public boolean isRun;

            public MyThread(SurfaceHolder holder) {
                this.holder = holder;
                isRun = true;
            }

            @Override
            public void run() {
                int count = 0;
                while (isRun) {
                    Canvas c = null;
                    c = this.holder.lockCanvas();
                    try {
                        synchronized (holder) {
                            if (null != c){

                                c.drawColor(Color.WHITE);
                                Paint p = new Paint();
                                p.setColor(Color.BLUE);
                                p.setStrokeWidth((float) 5.0);

                                draw(c, p, 0, 0, width, height, 6);

                                Thread.sleep(1000);

                                isRun = false;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (c != null) {
                            // 此时才提交绘画结果
                            holder.unlockCanvasAndPost(c);
                        }
                    }
                }
            }
        }//class MyThread extends Thread {

        public void draw(Canvas c, Paint p, int x1, int y1, int x2, int y2,int depth) {

            c.drawLine(x1, y1, x2, y2, p);


            if (depth <= 1) {

                return;
            }
            else {//得到三等分点
                double x11 = (x1 * 2  + x2)  / 3;
                double y11 = (y1 * 2  + y2)  / 3;

                double x22 = (x1 + x2 * 2) / 3;
                double y22 = (y1 + y2 * 2) / 3;

                double x33 = (x11 + x22) / 2 - (y11 - y22) * Math.sqrt(3) / 2;
                double y33 = (y11 + y22) / 2 - (x22 - x11) * Math.sqrt(3) / 2;

                c.drawLine(x1, y1, x2, y2, p);

                draw(c, p, x1, y1, (int) x11, (int) y11, depth - 1);
                draw(c, p, (int) x11, (int) y11, (int) x33, (int) y33, depth - 1);
                draw(c, p, (int) x22, (int) y22, x2, y2, depth - 1);
                draw(c, p, (int) x33, (int) y33, (int) x22, (int) y22, depth - 1);
            }
        }
    }
}

结果:

原文地址:https://www.cnblogs.com/Fredric-2013/p/5417584.html