Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用onTouch/onClick更改Android上显示的画布图形_Java_Android - Fatal编程技术网

Java 使用onTouch/onClick更改Android上显示的画布图形

Java 使用onTouch/onClick更改Android上显示的画布图形,java,android,Java,Android,我有一张帆布画,我要用onTouch来改变画的颜色。这是一座房子,一旦触摸屏幕,它就会变暗。我只被告知,我需要将方法调用移动到白天和夜间,可通过布尔值进行切换,并且文本白天和夜间必须相应地呈现在图像上 下面是我用来绘制图像的代码。如有任何关于如何完成此任务的帮助,将不胜感激: /* * DrawView.java */ public class DrawView extends View implements OnTouchListener { private Paint bac

我有一张帆布画,我要用onTouch来改变画的颜色。这是一座房子,一旦触摸屏幕,它就会变暗。我只被告知,我需要将方法调用移动到白天和夜间,可通过布尔值进行切换,并且文本白天和夜间必须相应地呈现在图像上

下面是我用来绘制图像的代码。如有任何关于如何完成此任务的帮助,将不胜感激:

/*
 * DrawView.java 
 */
public class DrawView extends View implements OnTouchListener
{

    private Paint backgroundPaint = new Paint();
    private Paint drawPaint_grass = new Paint();
    private Paint drawPaint_door = new Paint();
    private Paint drawPaint_house = new Paint();
    private Paint drawPaint_roof = new Paint();
    private Paint circlePaint = new Paint();
    private Paint circlePaint_sun = new Paint();
    private Paint textPaint = new Paint();
    private Paint path = new Paint();
    private Path trianglePath;
    private float sx, sy;

    public DrawView(Context context)
    {
        super(context);         

        setFocusable(true);
        setFocusableInTouchMode(true);

        backgroundPaint.setColor(Color.rgb(135,250,205));
        backgroundPaint.setAntiAlias(true);
        backgroundPaint.setStyle(Style.FILL);

        drawPaint_grass.setColor(Color.rgb(124, 252, 0));
        drawPaint_grass.setStyle(Style.FILL);

        drawPaint_door.setColor(Color.RED);
        drawPaint_door.setStyle(Style.FILL);

        //drawPaint_.setColor(Color.RED);
        //drawPaint_door.setStyle(Style.FILL);

        drawPaint_house.setColor(Color.rgb(205, 133, 63));
        drawPaint_house.setStyle(Style.FILL);

        drawPaint_roof.setColor(Color.rgb(160, 82, 45));
        drawPaint_roof.setStyle(Style.FILL);

        circlePaint_sun.setColor(Color.rgb(255, 255, 0));
        circlePaint_sun.setStyle(Style.FILL);

        trianglePath = new Path();
        trianglePath.moveTo(70, 300); // starting point
        trianglePath.lineTo(170,250); // 1st vertix
        trianglePath.lineTo(270, 300); // 2nd vertix
        trianglePath.lineTo(70, 300); // 3rd vertix and close
        //path.moveTo(getRight()/2, getLeft()/2, getTop()/2, getBottom()/2);
        textPaint.setColor(Color.BLACK);
        textPaint.setStyle(Style.FILL);
        //255, 255, 240
        circlePaint.setColor(Color.rgb(211, 211, 211));
        circlePaint.setStyle(Style.FILL);

        this.setOnTouchListener(this);
    }

    @Override
    public void onDraw(Canvas canvas)
    {

        //canvas.drawPath(path, paint);
        //canvas.drawPath(path, paint);

        // Draw white background
        canvas.drawRect(this.getLeft(), this.getTop(), this.getRight(), this.getBottom(), backgroundPaint);

        //draw a rectangle with blue paint
        canvas.drawRect(0,400, 540,600, drawPaint_grass); 
        canvas.drawRect(100,400, 240,300, drawPaint_house);
        canvas.drawRect(150,400, 190,335, drawPaint_door); 
        canvas.drawPath(trianglePath, drawPaint_roof); 

        //draw text with green paint
        canvas.drawText("Muhibur Rahim", 232, 565, textPaint);

        //draw a circle with red paint with the touch coordinates
        canvas.drawCircle(sx-30,sy-30, 3, circlePaint);

        canvas.drawCircle(80, 80, 30, circlePaint_sun);
    }

    public boolean onTouch(View v, MotionEvent event)
    {   
        //update the coordinates for the OnDraw method above, with wherever we touch
        sx = event.getX();
        sy = event.getY();

        invalidate();
        return true;
    }

}

我正在考虑将代码的着色部分(circlePaint_sun.setColor(Color.rgb(255,255,0))放入方法中(例如私有Drawview(上下文,上下文)下的static void day()),然后将这两个方法的值赋值为0和1,然后触摸或单击(我认为onclick会更好)计数器增加,值在0和1之间交替。但是,我不知道如何在代码中使用它。经过反复尝试,非常感谢您的帮助…

向变量添加
私有布尔暗=false;

onDraw
方法中,添加如下内容

if (dark) backgroundPaint.setColor(Color.rgb(0,0,0));
else backgroundPaint.setColor(Color.rgb(255,255,255));
在你画背景之前

onTouch
方法中更改布尔值:

if (event.getAction() == MotionEvent.ACTION_DOWN) {
   dark = !dark;
   DrawView();
}

您必须更改onDraw方法,而不是构造函数(更新的答案现在是正确的),但是所有颜色的细节都在构造函数中,而不是onDraw()中方法?那些不需要更改的颜色可以在构造器中设置。每次绘制图像时都必须更新背景颜色,因此在绘制背景之前,需要在
onDraw
方法中进行此操作。