1

我有两种描述两张图片的方法,在触摸图片/屏幕时,我想在屏幕上的两者之间交替。如何使用布尔值来做到这一点?

这是我的代码:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * DrawView.java 
 * A first drawing view example
 */
public class DrawView extends View implements OnTouchListener
{

    private Paint backgroundPaint = new Paint();
    private Paint backgroundPaintn = new Paint();
    private Paint drawPaint_grass = new Paint();
    private Paint drawPaint_grassn = new Paint();
    private Paint drawPaint_door = new Paint();
    private Paint drawPaint_doorn = new Paint();
    private Paint drawPaint_house = new Paint();
    private Paint drawPaint_housen = new Paint();
    private Paint drawPaint_roof = new Paint();
    private Paint drawPaint_roofn = new Paint();
    private Paint circlePaint = new Paint();
    private Paint circlePaintn = new Paint();
    private Paint circlePaint_sun = new Paint();
    private Paint circlePaint_sunn = new Paint();
    private Paint textPaint = new Paint();
    private Paint textPaintn = new Paint();
    private Paint path = new Paint();
    private Paint pathn = new Paint();
    private Path trianglePath;
    private float sx, sy;

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

        setFocusable(true);
        setFocusableInTouchMode(true);

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

        backgroundPaintn.setColor(Color.rgb(107,92,240));
        backgroundPaintn.setAntiAlias(true);
        backgroundPaint.setStyle(Style.FILL);

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

        drawPaint_grassn.setColor(Color.rgb(63,104,73));
        drawPaint_grassn.setStyle(Style.FILL);

        drawPaint_door.setColor(Color.rgb(233,82,65));
        drawPaint_door.setStyle(Style.FILL);

        drawPaint_doorn.setColor(Color.rgb(105, 15, 5));
        drawPaint_doorn.setStyle(Style.FILL);

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

        drawPaint_housen.setColor(Color.rgb(175, 142, 138));
        drawPaint_housen.setStyle(Style.FILL);

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

        drawPaint_roofn.setColor(Color.rgb(67,52,51));
        drawPaint_roofn.setStyle(Style.FILL);

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

        circlePaint_sunn.setColor(Color.rgb(249, 245, 245));
        circlePaint_sunn.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

        textPaint.setColor(Color.BLACK);
        textPaint.setStyle(Style.FILL);

        textPaintn.setColor(Color.WHITE);
        textPaintn.setStyle(Style.FILL);

        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); // or whatever paint you want


        //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);

        //draw a circle with red paint with the touch coordinates
        canvas.drawCircle(80, 80, 30, circlePaint_sun);
    }

    public void onDrawn(Canvas canvas)
    {

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

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

        //draw a rectangle with blue paint
        canvas.drawRect(0,400, 540,600, drawPaint_grassn); 
        canvas.drawRect(100,400, 240,300, drawPaint_housen);
        canvas.drawRect(150,400, 190,335, drawPaint_doorn); 
        canvas.drawPath(trianglePath, drawPaint_roofn); // or whatever paint you want


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

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

        //draw a circle with red paint with the touch coordinates
        canvas.drawCircle(80, 80, 30, circlePaint_sunn);
    }


    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;
    }

}

我的问题是代码的最后一部分,public boolean onTouch ...

4

3 回答 3

1
public boolean bool = false;
public int count = 0; // say before the constructor.

//then in your onDraw method take everything and encapsulate it in an if statement:

if(!bool){

//the code in your onDraw() method

}else{//here you would put the call to the other draw method like
onDrawn(canvas);
}

然后在你重写的 onTouch() 方法中像这样

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();
    if(event.getAction() == MotionEvent.ACTION_DOWN){
     if(count == 0){
     bool = true;
     count++;}
     if(count == 1){
     bool = false;
     count--;}
     }
    invalidate();
    return true;
}

这个怎么样?

public void onDraw(Canvas canvas)
{
    if(!bool){
        // 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); // or whatever paint you want

        //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);

        //draw a circle with red paint with the touch coordinates
        canvas.drawCircle(80, 80, 30, circlePaint_sun);
    } else {
        onDrawn(canvas);
    }
}
于 2013-02-23T22:36:24.983 回答
1
if (flag) {
  // call first method
  flag = false;
} else {
  // call second method
  flag = true;
}
于 2013-02-23T22:39:52.397 回答
0
Random randomizer = new Random();
if(randomizer.nextBoolean()==True){
}
else{
}
于 2013-02-23T22:39:13.817 回答