public class SuView extends View { private float x = 100; private float y = 100; public SuView(Context context) { super(context); } public SuView(Context context, AttributeSet attrs) { super(context, attrs); } public SuView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE);//設置畫布顏色 Paint paint = new Paint();//畫筆 paint.setColor(Color.RED);//設置畫筆顏色 paint.setStrokeWidth(10); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true);//抗鋸齒 canvas.drawCircle(x,y,100,paint);//畫圓 } @Override public boolean onTouchEvent(MotionEvent event) { x = event.getX(); y = event.getY(); postInvalidate(); return true; } }
初學自定義view(可拖動圓)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.