1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package safeluck.drive.evaluation.fragment;
 
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
 
import me.yokeyword.fragmentation.SupportFragment;
import safeluck.drive.evaluation.R;
 
/**
 * MyApplication2
 * Created by lzw on 2019/9/26. 15:20:58
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class MapFragment extends SupportFragment implements SurfaceHolder.Callback {
    private boolean isDrawing = false;
 
    public static SupportFragment newInstance(){
        return new MapFragment();
    }
    private SurfaceView mSurfaceView;
    private SurfaceHolder holder;
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Path path = new Path();
    private Canvas canvas;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 
 
        mSurfaceView = new SurfaceView(getActivity());
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        holder = mSurfaceView.getHolder();
        holder.addCallback(this);
        mSurfaceView.setLayoutParams(layoutParams);
        mSurfaceView.setKeepScreenOn(true);
        return mSurfaceView;
    }
 
    @Override
    public void surfaceCreated(final SurfaceHolder holder) {
        isDrawing = true;
        //开启线程 进行画图
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isDrawing){
                    drawPath();
                }
            }
        }).start();
    }
 
    private void drawPath() {
        try {
            canvas = holder.lockCanvas();
            paint.setColor(Color.RED);
            path.addCircle(mSurfaceView.getWidth()/2,mSurfaceView.getHeight()/2,70, Path.Direction.CW);
            path.addOval(new RectF(new Rect(100,100,200,200)), Path.Direction.CW);
            canvas.drawPath(path,paint);
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (canvas != null){
                holder.unlockCanvasAndPost(canvas);
            }
        }
    }
 
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
 
    }
 
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;
    }
}