drawing a circle where the user has touched on canvas

孤人 提交于 2019-12-29 08:16:05

问题


Starting to learn Canvas and have two classes so far (main one to call the view and the view) The View class onDraw creates a target (ie number of cicles and each one coloured differently)

I have a ontouch listenerer set up to record the x and y where the user clicks

my trouble then is drawing a new circle / point where the user touches

UPDATED with classes

Main Class

public class StartScreen extends Activity {

    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
}

Draw Class

public class DrawView extends View implements View.OnTouchListener {

    private Paint paint[];

    private Context context;
    private Canvas canvas;

    //definging some variables

    public DrawView(Context pContext) {
        super(pContext);  
        this.context = pContext;

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        paint = new Paint[5];
        setupColours();

    // setting varibale like raduis etc



    }

    private void setupColours() {

        // Creating Arrray of Paint Colours

    }

    @Override
    public void onDraw(Canvas pCanvas) {

        canvas = pCanvas;

        newRadius = radius;

        for (int i = 0; i < rings; i++) {


            if (i == 3) {
                canvas.drawCircle(centreWidth, centreHeight, newRadius, paint[0]);
            } else {
                canvas.drawCircle(centreWidth, centreHeight, newRadius, paint[1]);
            }


            canvas.drawCircle(centreWidth, centreHeight, newRadius - targetBoundary, paint[i / 2]);

            newRadius = newRadius - ringOffset;

        }

        this.setOnTouchListener(this);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d("TAG2", "x: " + event.getX() + " y: " + event.getY());
        drawHit(event.getX(), event.getY());
        return true;
    }

    public void drawHit(float hitX, float hitY) {
        Log.d("HIT", "Hit being drawn");

        Paint paint2 = new Paint();
        paint2.setColor(Color.BLACK);

        canvas.drawCircle(hitX, hitY, 100, paint2);

    }

The method is called but the circle is not being drawn. what am I doing wrong. Thanks


回答1:


You have provided very little information in your question. A little elaboration wouldn't have hurt. Are the targets(the circles) being created as you would want them to be? If yes, then get a hold of the FrameLayout and use the 'addView' method to overlay your ball onto the main view.

A slightly old but nevertheless a tutorial which may be useful to you: http://www.kellbot.com/2009/06/android-hello-circle/

Good Luck




回答2:


  1. https://github.com/swapgo20/Android-Hand-Drawing
  2. https://github.com/codepath/android_guides/wiki/Basic-Painting-with-Views
  3. https://github.com/Korilakkuma/CanvasView

I hope above link are very useful to draw shapes on canvas.



来源:https://stackoverflow.com/questions/11328848/drawing-a-circle-where-the-user-has-touched-on-canvas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!