按下按钮后,我需要进入方法“createCircles”另一个项目。因为我通过canvas从这个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; }
}
答案 0 :(得分: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));
}
只要它只为null,就应该实例化ArrayList。
另外,如果您通过查看此GUI来测试此内容,则圆圈可能正在堆叠。这意味着它可能使圆圈很好但同时不显示它们,因为它们都具有(120, 30, 7)
的属性。考虑将它们随机放置,或设置在具有不同属性的不同位置,以便您知道它们不会堆叠。