Android绘图

时间:2016-03-14 20:46:47

标签: java android graphics

我正在学习一个教程,并且有以下代码,但是我想稍微修改一下,所以我想要在圈内点击它必须用另一个collor填充,我怎么能这样做,因为我我刚刚学习这个概念我不知道如何做到这一点:/

public class MainActivity extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(new MyView(this));
     }

     public class MyView extends View {
         public MyView(Context context) {
              super(context);
         }

         @Override
         protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int x = getWidth();
            int y = getHeight();
            int radius;
            radius = 100;
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            canvas.drawPaint(paint);
            paint.setColor(Color.parseColor("#CD5C5C"));
            canvas.drawCircle(x / 2, y / 2, radius, paint);
        }
     }
}

1 个答案:

答案 0 :(得分:1)

您必须在视图中添加触控侦听器

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View myView = new MyView(this);
        setContentView(myView);

        myView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float x = event.getX();
                float y = event.getY();

                //if(x==... your code here

                invalidate(); //this repaints the view
                return false;
            }
        });
    }
}