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);
}
}
}
上一篇: windows下安装mysql5.7
下一篇: tensorflow环境搭建
推荐阅读
-
android使用surfaceview+MediaPlayer播放视频
-
Cocos2d游戏开发学习记录——1.Surface、SurfaceView、SurfaceHolder实现简单的游戏demo
-
Android SurfaceView拍照录像实现方法
-
Android利用SurfaceView实现简单计时器
-
利用SurfaceView实现下雨与下雪动画效果详解(Kotlin语法)
-
Android SurfaceView拍照录像实现方法
-
利用SurfaceView实现下雨与下雪动画效果详解(Kotlin语法)
-
android图像绘制(四)自定义一个SurfaceView控件
-
Android截屏SurfaceView黑屏问题的解决办法
-
Android利用SurfaceView实现下雨的天气动画效果