2013-12-17 84 views
0

我一直試圖在幾次單擊按鈕後在畫布上繪製不同的矩形。它應該顯示不同的彩色矩形,每按一下按鈕,矩形就會保留在畫布上。矩形應該能夠在畫布上移動。我已經寫了視圖類,但我不知道如何在點擊按鈕後對活動實現onDraw()方法,也不知道如何創建不同顏色的矩形。畫布(onDraw)Android

我的main.xml文件上有4個按鈕。

public class DrawRectangle extends View { 

public DrawRectangle(Context context){ 

    super(context); 

} 

@Override 
protected void onDraw(Canvas canvas) { 
    // TODO Auto-generated method stub 
    super.onDraw(canvas); 

    Rect ourRect = new Rect(); 

    ourRect.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2); 

    Paint blue = new Paint(); 

    blue.setColor(Color.BLUE); 

    blue.setStyle(Paint.Style.FILL); 

    //Draw to actual canvas 
    canvas.drawRect(ourRect, blue); 

} 

}

這是我的活動類。

public class MainActivity extends Activity { 

Button bluebutton, redbutton, yellowbutton, greenbutton; 
DrawRectangle dr; 
Canvas canvas; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    dr = new DrawRectangle(this); 

    bluebutton = (Button)findViewById(R.id.bluebutton); 
    redbutton = (Button)findViewById(R.id.redbutton); 
    yellowbutton = (Button)findViewById(R.id.yellowbutton); 
    greenbutton = (Button)findViewById(R.id.greenbutton); 



bluebutton.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 

       dr.onDraw(); 
    } 
}); 
} 

}

我一定要實現onTouchListener以及使矩形可以移動?

請指教。謝謝。

回答

2

您應該可以使onClick方法中的每個按鈕的畫布無效。添加一些布爾變量來告訴onDraw方法繪製什麼顏色。

public static boolean isBlue, isRed, isYellow, isGreen; 
public class MainActivity extends Activity { 

    Button bluebutton, redbutton, yellowbutton, greenbutton; 
    DrawRectangle dr; 
    Canvas canvas; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    dr = new DrawRectangle(this); 

    bluebutton = (Button)findViewById(R.id.bluebutton); 
    redbutton = (Button)findViewById(R.id.redbutton); 
    yellowbutton = (Button)findViewById(R.id.yellowbutton); 
    greenbutton = (Button)findViewById(R.id.greenbutton); 

    boolean blueColor = false; 
    bluebutton.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     isBlue = true; 
     isRed = false; 
     isGreen = false; 
     isYellow = false; 
     dr.invalidate(); 
    } 
    }); 
} 

的更新的onDraw方法進行檢查,看畫什麼顏色。

//Used for storing rectangles   
public static List<Rect> rectangles; 
public class DrawRectangle extends View { 

    public DrawRectangle(Context context){ 

    super(context); 

} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    //Draw previous rectangles 
    for(int i=0;i<rectangles.size();i++){ 
     canvas.drawRect(rectangles.get(i), paintColor); 
    } 
    Rect ourRect = new Rect(); 

    ourRect.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2); 

    Paint paintColor = new Paint(); 

    if(MainActivity.isBlue){ 
     paintColor.setColor(Color.BLUE); 
    } 

    paintColor.setStyle(Paint.Style.FILL); 

    //Draw to actual canvas 
    canvas.drawRect(ourRect, paintColor); 
    rectangles.add(ourRect) 

}}

爲了吸引更多的矩形,你應該每次以前繪製的矩形存儲在通過它Vector和循環您無效的onDraw方法。

+0

感謝您的回覆和建議。我已經對我的代碼進行了修改,但是當按鈕被點擊時,矩形沒有被繪製。問題,我是否必須在我的xml上指定一個surfaceView? – pancakeleh

+0

我已經設置了setContentView(); be = setContentView(dr);並繪製矩形。但4個按鈕失蹤。我如何保持按鈕和矩形? – pancakeleh

+0

你能提供你的main.xml文件嗎? – industrychanger

2

是的,你必須在你的視圖子類中使用onTouchEvent。您可以閱讀文檔here。事件參數包含您正在獲取的觸摸種類的信息(ACTION_DOWNACTION_MOVE,ACTION_UP),並且還包含觸摸事件的座標。 當您收到ACTION_MOVE事件時,您可以更改矩形的位置並呼叫invalidate()重繪。在你的活動中,在onClickListener內部擺脫平局呼叫