2014-12-19 232 views
0

我必須在屏幕上繪製圓圈並通過OnTouch方法獲得交互。請幫助我。這是我嘗試過的代碼。這裏的問題是,它不與用戶交互intract但是這個代碼成功繪製圓形在畫布上繪製交互圓圈

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(); 
    } 
} 

但問題是,它並沒有得到用戶的交互

+0

它,它制定出來的? – 2014-12-19 07:56:35

回答

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); 
} 

互動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; 
     } 
    }); 
} 
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); 
0

刪除invalidate()onDraw()方法,並在onTouch()獲得隨機顏色。

您需要在每次觸摸動作,觸摸位置(或觸摸位置)中使用不同的顏色,或者想要更改顏色,然後檢查event.getAction()==MotionEvent.ACTION_DOWN中的動作。

0

刪除onDraw()方法invalidate(),因爲當你在invalidatingonDraw()它會調用onDraw Recursivly,並在onTouch()獲得隨機顏色。

你想不同的顏色在每一個觸摸動作,將在onTouch()隨機顏色的方法,或基於觸摸下要更改的顏色,然後檢查從event.getAction()==MotionEvent.ACTION_DOWN.