[英]Canvas (onDraw) Android
多次單擊按鈕后,我一直試圖在畫布上繪制不同的矩形。 它應顯示不同顏色的矩形,並且每次單擊按鈕后該矩形應保留在畫布上。 矩形應該能夠在畫布上移動。 我已經編寫了View類,但是我不知道如何在單擊按鈕后在活動上實現onDraw()方法,也不清楚如何創建不同顏色的矩形。
我的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以便矩形可以移動?
請指教。 謝謝。
是的,您必須在視圖子類中使用onTouchEvent
。 您可以在此處閱讀文檔。 事件參數包含有關您獲得的觸摸類型的信息( ACTION_DOWN
, ACTION_MOVE
, ACTION_UP
),還包含觸摸事件的坐標。 當您收到ACTION_MOVE事件時,可以更改矩形的位置並調用invalidate()
進行重繪。 普萊舍擺脫對內部繪圖調用onClickListener
您的活動
您應該能夠在每個按鈕的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方法無效時在其中循環遍歷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.