2013-02-20 32 views
0

我有一个画布图形,我将使用onTouch更改图形的颜色。这是一栋房子,一旦触摸屏幕就会变成黑暗的阴影。我只知道我需要将方法调用转换为dayTime和nightTime,可以通过布尔值进行切换,并且文本白天和夜晚时间必须相应地在图像上呈现。使用onTouch/onClick更改Android上显示的画布图形

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

/* 
* 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(Context,Context)下面的void dayTime(),然后分配两个方法值0和1,并在触摸或点击时(我认为onclick会更好)计数器增加,值在0和1之间交替。我不知道如何使用的代码反复尝试,任何帮助表示赞赏后...

回答

0

添加private boolean dark = 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(); 
} 
+0

你必须改变的onDraw方法,而不是构造函数(更新答案现在是正确的) – Toast 2013-02-20 21:05:11

+0

但是,所有这些颜色的细节是在构造和不是onDraw()方法? – mrahh 2013-02-23 22:18:34

+0

那些不需要改变的颜色可以在构造函数中设置。每次绘制图像时都必须更新背景颜色,因此您需要在绘制背景之前使用'onDraw'方法执行此操作。 – Toast 2013-02-28 14:56:10