欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SurfaceView

程序员文章站 2022-03-29 19:29:38
...

画板清空

package com.example.day1113_demo;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private Button clear;
    private SurfaceView sv;
   
    private SurfaceHolder holder;

    private Path path;

    private Paint paint;//畫筆
    private Canvas canvas;//画布

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        clear = (Button) findViewById(R.id.clear);
        sv = (SurfaceView) findViewById(R.id.sv);
       


        path = new Path();
        holder  = sv.getHolder();
        holder.addCallback(this);
        sv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                float x = event.getX();
                float y = event.getY();
                if (action == MotionEvent.ACTION_DOWN){
                    path.moveTo(x,y);
                }
                if (action == MotionEvent.ACTION_MOVE){
                    path.lineTo(x,y);
                }
                return true;
            }
        });

        //清空
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                path.reset();
            }
        });

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new MyThread().start();
    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
             paint = new Paint();

            paint.setColor(Color.BLUE);
            paint.setStyle(Paint.Style.STROKE);//空心
            paint.setStrokeWidth(20);
            paint.setTextSize(20);
            while (true){
                 canvas = holder.lockCanvas();
                if (canvas == null){
                    break;
                }
                canvas.drawColor(Color.WHITE);
                canvas.drawPath(path,paint);
                holder.unlockCanvasAndPost(canvas);
            }

        }
    }
}

画一张图片(飞机大战部分)

package com.example.app2;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import com.example.app2.Bean.ZhujueBean;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    private static final String TAG = "zzzzzzz";
    private SurfaceView sv;
    private SurfaceHolder holder;
    private ZhujueBean zhujueBean;


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

        sv = (SurfaceView) findViewById(R.id.sv);
        holder = sv.getHolder();
        holder.addCallback(this);
        sv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

        int kuan = getWindowManager().getDefaultDisplay().getWidth();
        int gao = getWindowManager().getDefaultDisplay().getHeight();
        Toast.makeText(this, kuan+"+++"+gao, Toast.LENGTH_SHORT).show();
        zhujueBean = new ZhujueBean(R.drawable.zhujue,500,1200);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        new MyThread().start();
    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            Paint paint = new Paint();
                Canvas canvas = holder.lockCanvas();
                canvas.drawColor(Color.WHITE);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), zhujueBean.img);
                Log.i(TAG, zhujueBean.x+"++++"+zhujueBean.y);
                canvas.drawBitmap(bitmap,zhujueBean.x,zhujueBean.y,paint);
                holder.unlockCanvasAndPost(canvas);
        }
    }
}