SurfaceView涂鸦板(带截屏)
2018-09-05 本文已影响0人
Amy_LuLu__
文章来自于
SurfaceView的基本使用
Android截屏SurfaceView黑屏问题解决办法
目录
- 创建SurfaceView
- 使用SurfaceView
1.创建SurfaceView
public class FirstSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
//SurfaceHolder
private SurfaceHolder mHolder;
//用于绘图的Canvas
private Canvas mCanvas;
//子线程标志位
private boolean mIsDrawing;
//画笔
private Paint mPaint;
//路径
private Path mPath;
public FirstSurfaceView(Context context) {
super(context);
initView();
}
public FirstSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public FirstSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
mHolder = getHolder();
//添加回调
mHolder.addCallback(this);
mPath = new Path();
//初始化画笔
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(6);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
setFocusable(true);
setFocusableInTouchMode(true);
this.setKeepScreenOn(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mIsDrawing = true;
new Thread(this).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mIsDrawing = false;
}
@Override
public void run() {
long start = System.currentTimeMillis();
while (mIsDrawing) {
try {
//锁定画布并返回画布对象
mCanvas = mHolder.lockCanvas();
//接下去就是在画布上进行一下draw
drawIt(mCanvas);
} catch (Exception e) {
} finally {
//当画布内容不为空时,才post,避免出现黑屏的情况。
if (mCanvas != null)
mHolder.unlockCanvasAndPost(mCanvas);
}
long end = System.currentTimeMillis();
if (end - start < 100) {
try {
Thread.sleep(100 - end + start);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void drawIt(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.drawPath(mPath, mPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPath.moveTo(x, y);
break;
case MotionEvent.ACTION_MOVE:
mPath.lineTo(x, y);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
/**
* 清屏
*
* @return true
*/
public boolean reDraw() {
mPath.reset();
return true;
}
public Bitmap getBitmap() {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawIt(canvas);
return bitmap;
}
}
2. 使用SurfaceView
新建一个activity
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<xxx.lulu.view.FirstSurfaceView
android:id="@+id/view_surfaceview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/btn_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple"
android:text="clear"
android:textColor="@color/white" />
</LinearLayout>
java文件:
public class SurfaceViewActivity extends BaseActivity {
private static final int PERMISSION_EXTERNAL = 1;
@BindView(R.id.view_surfaceview)
FirstSurfaceView viewSurfaceview;
@BindView(R.id.btn_clear)
Button btnClear;
@BindView(R.id.btn_save)
Button btnSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surfaceview, "surfaceview");
ButterKnife.bind(this);
}
@OnClick({R.id.btn_clear, R.id.btn_save})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_clear:
viewSurfaceview.reDraw();
break;
case R.id.btn_save:
if (ContextCompat.checkSelfPermission(SurfaceViewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//没有授权,则申请授权
//ActivityCompat.requestPermissions() 参数一:context 参数二:申请的权限名数组 参数三:请求码,要求唯一值
ActivityCompat.requestPermissions(SurfaceViewActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_EXTERNAL);
} else {
save();
}
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PERMISSION_EXTERNAL:
if (resultCode != Activity.RESULT_OK) return;
save();
break;
}
}
private void save() {
/* View dView = getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();*/
Bitmap bitmap = viewSurfaceview.getBitmap();
if (bitmap != null) {
try {
// 获取内置SD卡路径
File dir = Environment.getExternalStorageDirectory();
// 图片文件路径
File file = new File(dir, "123.png");
FileOutputStream os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
Toast.makeText(this, "存储完成", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
}
}
}