当我触摸屏幕及其工作时,我想绘制黑点,但是当我第一次开始活动时,屏幕是黑色的,触摸后会变成白色。我希望从开始时变白。我该如何解决?这是我的代码:
package com.inveitix.android.clue.ui.views;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
/**
* Created by Tito on 3.2.2016 г..
*/
public class DrawingView extends SurfaceView {
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas;
int width;
int height;
private SurfaceHolder surfaceHolder;
private WindowManager wm;
public DrawingView(Context context) {
super(context);
init();
}
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
surfaceHolder = getHolder();
wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
getScreenSize();
}
private void getScreenSize() {
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (surfaceHolder.getSurface().isValid()) {
canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
canvas.drawCircle(event.getX(), event.getY(), 10, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
return false;
}
}
答案 0 :(得分:2)
在init方法中添加:
holder.addCallback(new SurfaceHolder.Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
}
public void surfaceCreated(SurfaceHolder holder) {
canvas = holder.lockCanvas();
canvas.drawColor(Color.WHITE);
holder.unlockCanvasAndPost(canvas);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
});
答案 1 :(得分:0)
更改此
paint.setColor(Color.BLACK);
到
paint.setColor(Color.WHITE);
答案 2 :(得分:0)
paint.setColor(Color.WHITE);