2013-02-20 33 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