在画布上绘制交互圈

时间:2014-12-19 07:27:33

标签: android xml

我必须在屏幕上绘制圆圈并通过OnTouch方法与其进行交互。请帮助我。这是我尝试过的代码。这里的问题是它不会影响用户交互,但是这段代码成功地绘制了圆圈

    public class DrawingView extends View implements OnTouchListener {

    static int x, y, r = 255, g = 255, b = 255;
    final static int radius = 30;
    Paint paint; // using this ,we can draw on canvas

    public DrawingView(Context context) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true); // for smooth rendering
        paint.setARGB(255, r, g, b); // setting the paint color

        // to make it focusable so that it will receive touch events properly
        setFocusable(true);

        // adding touch listener to this view
        this.setOnTouchListener(this);
    }

    // overriding the View's onDraw(..) method
    public void onDraw(Canvas canvas) {
        paint.setARGB(255, r, g, b);

        super.onDraw(canvas);
        // drawing the circle

        canvas.drawCircle(x, y, radius, paint);
        randColor(); // calls this method to generate a color before drawing
        invalidate(); // calls onDraw method

    }

    // this is the interface method of "OnTouchListener"
    public boolean onTouch(View view, MotionEvent event) {
        x = (int) event.getX() - (radius / 2); // some math logic to plot the
                                                // circle in exact touch place
        y = (int) event.getY() - (radius / 2);
        // System.out.println("X,Y:"+"x"+","+y); //see this output in "LogCat"
        randColor(); // calls this method to generate a color before drawing
        invalidate(); // calls onDraw method
        return true;
    }

    // this method sets a random color using Math.random()
    // Note: RGB color values ranges from 0 to 255..
    public void randColor() {
        r = (int) (Math.random() * 255);
        g = (int) (Math.random() * 255);
        b = (int) (Math.random() * 255);
        // Toast.makeText(c, "r,g,b="+r+","+g+","+b,Toast.LENGTH_SHORT).show();
    }
}

但问题在于,它没有得到用户互动

4 个答案:

答案 0 :(得分:0)

至少有几个问题:

您实际上没有测试过触摸x / y是否落在圆的半径内。您需要if子句。 invalidate()现在每次触摸都会被调用。

事件的顺序错误,有些操作被调用太多次。内部ondDraw内部应该有效:

super.onDraw(canvas);
paint.setARGB(255, r, g, b); // (and you don't need this in the method above)
canvas.drawCircle(x, y, radius, paint);

答案 1 :(得分:0)

删除invalidate()方法中的onDraw(),并在onTouch()中获得随机颜色。

您希望在每个触摸操作中都有不同的颜色,触摸()或基于触摸下来想要更改颜色然后从event.getAction()==MotionEvent.ACTION_DOWN检查操作。

答案 2 :(得分:0)

删除onDraw()方法中的invalidate(),因为当你invalidating onDraw()时,它会调用onDraw Recursivly,并在onTouch()中获得随机颜色。

你想在每个触摸动作中使用不同的颜色,将随机颜色方法放在onTouch()中,或者基于触摸下来想要改变颜色然后从event.getAction()==MotionEvent.ACTION_DOWN.

检查动作

答案 3 :(得分:0)

只需用它来画圆圈

public class Circle extends View {
 private final float x;
    private final float y;
    private final int r;
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

public Circle(Context context, float x, float y, int r) { 
    super(context);
    mPaint.setColor(0xFFFF0000);                 
    this.x = x;
    this.y = y;
    this.r = r;
}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawCircle(x, y, r, mPaint);
}

For Interaction MainActivity类在这里

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.circle);
    FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
    main.addView(new Circle(this, 50, 50, 25));

    main.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            float x = e.getX();
            float y = e.getY();
            FrameLayout flView = (FrameLayout) v;
            flView.addView(new Circle(flView.getContext(), x, y, 5));
            return true;
        }
    });
}