[Android 學習筆記] 繪圖使用教學
在 Android 系統中支援 2D 繪圖,可以在繼承 View 類別覆寫的 onDraw() 方法進行繪圖,例如:public class Draw2D extends View{
public Draw2D(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
//在此產生繪圖程式
}
}
@Override
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
Draw2D d = new Draw2D(this);
setContentView(d);
}
建立畫布
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
繪出圓形
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawCircle(80, 30, 25, paint);
繪出長方形
paint.setColor(Color.BLUE);
canvas.drawRect(20, 15, 50, 100, paint);