1

我正在尝试开发一个应用程序,该应用程序的唯一功能是在我触摸屏幕时更改画布的颜色,但我无法获得它。这是代码。谢谢,我正在学习这个,这对我来说有点困难。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.EventLog.Event;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class Juego extends View implements OnTouchListener {
 Bitmap  bitmap;
    Path    path = new Path();
    Canvas  canvas;
    Paint paint = new Paint();
    int colorchange=0;

public Juego(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnTouchListener(this);
}
public void onDraw(Canvas canvas){
    super.onDraw(canvas);
    canvas.drawColor(Color.GREEN);

}
public boolean onTouch (View view, MotionEvent event) {
    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:{
    if(colorchange==0){
    canvas.drawColor(Color.BLUE);
    colorchange=1;
    }else{
    canvas.drawColor(Color.GREEN);
    colorchange=0;
    }
    }
    invalidate();
    break;
    default:
        return false;
    }
    return true;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
}
}
4

0 回答 0