0

按下按钮后,我需要进入另一个项目的“createCircles”方法。因为我从这个 ArrayList 中通过画布绘制圆圈。所以我做了以下。但是当它运行时,所以当你按下按钮时没有任何反应。我的问题是,如果它正确注册了我的附加物品到盒子里。

public class krouzky extends View {
    final Paint mMalovani;
    ArrayList<Circle> mCircles;

    private boolean KontrolerVykreslovani = true;

    public krouzky(Context context, AttributeSet atrs) {
        super(context, atrs);

        mMalovani = new Paint();
        mMalovani.setColor(Color.RED);
        mMalovani.setAntiAlias(true);

        createCircles();

    }

    private void createCircles() {
        mCircles = new ArrayList<Circle>();
        mCircles.add(new Circle(120, 30, 7));

    }

    public void Pridani_kruhu() {
        Button btn2 = (Button) findViewById(R.id.button3);
        btn2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                createCircles(v);
            }
        });
    }      

    private void createCircles(View v) {
        mCircles.add(new Circle(120, 30, 15));
    }

如果在程序开始之前有一个项目要添加,一个方法是这样的,所以他按照计划画了两个圆圈。

private void createCircles() {
    mCircles = new ArrayList<Circle>();
    mCircles.add(new Circle(120, 30, 7));
    mCircles.add(new Circle(130, 30, 12));
}

感谢您的意见

完整的活动在这里。

public class krouzky extends View {

final Paint mMalovani;  


ArrayList<Circle> mCircles;

private boolean KontrolerVykreslovani = true;

public krouzky(Context context, AttributeSet atrs) {
    super(context, atrs);

    mMalovani = new Paint();
    mMalovani.setColor(Color.RED);
    mMalovani.setAntiAlias(true);

    createCircles();
}

private void createCircles() {
    mCircles = new ArrayList<Circle>();

    mCircles.add(new Circle(120, 30, 7));
}



public void Pridani_kruhu() {

    Button btn2 = (Button) findViewById(R.id.button3);

    btn2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            createCircles(v);
        }
    });
    }          

private void createCircles(View v) {    
    mCircles.add(new Circle(120, 30, 15));
}


public void draw(Canvas canvas) {
    // pokud je mDrawinEnabled "true" tak udělá .....
    if ( KontrolerVykreslovani ) {
        //přiřazení hodnot do proměnných width a height
        final int width = canvas.getWidth();
        final int height = canvas.getHeight();

        kresleníKruhu(canvas);



        invalidate();
    }
}

public boolean onTouchEvent(MotionEvent event) {

    float x = event.getX();
    float y = event.getY();

    Circle cr = findCircleClosestToTouchEvent(x, y);



    float dtcr = euclidDist(cr.getCurrentX(), cr.getCurrentY(), x, y);

        handleTouchedCircle(event, cr);

    return true;
}

private void kresleníKruhu(Canvas canvas) {
    for (Circle c : mCircles) {

        canvas.drawCircle(c.getCurrentX(), c.getCurrentY(), c.getRadius(),
                mMalovani);
    }
}



private static float euclidDist(float x1, float y1, float x2, float y2) {
    return android.util.FloatMath.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1)
            * (y2 - y1));
}


private Circle findCircleClosestToTouchEvent(float x, float y) {

    Circle c = mCircles.get(0);

    float dist = euclidDist(c.getCurrentX(), c.getCurrentY(), x, y);        
    float tempdist = 0;
    for (Circle cr : mCircles) {
        tempdist = euclidDist(cr.getCurrentX(), cr.getCurrentY(), x, y);
        if (tempdist < dist) {
            c = cr;
            dist = tempdist;
        }
    }
    return c;
}

private void handleTouchedCircle(MotionEvent me, Circle c) {

    final float me_x = me.getX();
    final float me_y = me.getY();
    final int action = me.getAction();

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        c.setActionDownX(c.getCurrentX());
        c.setActionDownY(c.getCurrentY());
        c.setActionMoveOffsetX(me_x);
        c.setActionMoveOffsetY(me_y);
        break;
    case MotionEvent.ACTION_MOVE:
    case MotionEvent.ACTION_UP:
        c.setCurrentX(c.getActionDownX() + me_x - c.getActionMoveOffsetX());
        c.setCurrentY(c.getActionDownY() + me_y - c.getActionMoveOffsetY());
        break;
    case MotionEvent.ACTION_CANCEL:
        c.restoreStartPosition();
        break;
    }
}


final void MuzemeVykreslovat() { KontrolerVykreslovani = true; }

final void NemuzemeVykreslovat() { KontrolerVykreslovani = false; }

}

4

1 回答 1

0
private void createCircles() {
    mCircles = new ArrayList<Circle>();

    mCircles.add(new Circle(120, 30, 7));
}

ArrayList<Circle>每次调用它都会创建一个新的,写在它拥有的任何圈子上。如果您打算实例化 ArrayList 而不是擦除它,请尝试以下操作:

private void createCircles() { if (mCircles == null) { mCircles = new ArrayList(); }

mCircles.add(new Circle(120, 30, 7));

}

只要 ArrayList 为空,它就应该实例化它。

此外,如果您通过查看这个的 GUI 来测试这个,圆圈可能会堆叠。这意味着它可能使圆圈很好,但同时不显示它们,因为它们都具有(120, 30, 7). 考虑将它们随机放置,或设置在具有不同属性的不同位置,以便您知道它们没有堆叠。

于 2013-08-06T20:40:51.737 回答