画一个用户触摸画布的圆圈

时间:2012-07-04 12:10:50

标签: android canvas

开始学习Canvas并且到目前为止有两个类(主要用于调用视图和视图) View类onDraw创建一个目标(即cicles数量,每个颜色不同)

我设置了一个ontouch侦听器来记录用户点击的x和y

我的麻烦就是画一个用户触摸的新圆圈/点

用类更新

主类

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

画班

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

    }

调用该方法但未绘制圆。我究竟做错了什么。感谢

2 个答案:

答案 0 :(得分:3)

您在问题中提供的信息非常少。一点点的阐述不会受到伤害。目标(圆圈)是否按照您希望的方式创建?如果是,那么抓住FrameLayout并使用&#39; addView&#39;将球重叠到主视图上的方法。

稍微陈旧但是可能对您有用的教程: http://www.kellbot.com/2009/06/android-hello-circle/

祝你好运

答案 1 :(得分:0)