在计时器上删除Imageview

时间:2012-06-05 15:48:25

标签: android timer imageview

我有这样的代码,我想在点击后删除imageview。我使用相对布局。我必须做什么? 我必须删除它而不是隐藏它,因为我有很多imageview,我想要销毁它,这样程序可以更快地工作。

banyakmusuh= new CountDownTimer(50000,1500) {

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

        Random a = new Random();
        int posisix = a.nextInt(500);
        btn = new ImageView(level2.this);
        btn.setImageResource(R.drawable.salju);
        i= i+1;
        btn.setId(i);                               
        RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        params.setMargins(posisix, 0, 0, 0);
        btn.setLayoutParams(params);
        RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.tingkat2);                 
        linearLayout.addView(btn);
        btn.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                    data.nilai+=10;
                    TextView score1 = (TextView) findViewById(R.id.skore);
                    score1.setText(String.valueOf(data.nilai));
                //what the code for delete imageview? 
                //some people say to write this code but it cant, there are a green line when i debug it 
               //((RelativeLayout)v.getParent()).removeView(v);
                                }
                            });

                        }

                        @Override
                        public void onFinish() {
                            // TODO Auto-generated method stub
                            //banyakmusuh.start();
                        }
                    }.start();

2 个答案:

答案 0 :(得分:1)

如果您对特定视图有参考,则可以使用:

yourRelativeLayout.removeView(imageViewToRemove);

我愿意:

// I would consider renaming your layout, btw
final RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.tingkat2);
btn.setOnClickListener(new OnClickListener() 
            {
               @Override
               public void onClick(View v)
               {
                   linearLayout.removeView(btn);
               }
             });

答案 1 :(得分:1)

Android适用于Java。您无法在java中销毁或删除对象。你只能对java机器说,如果需要,机器可以自由销毁对象。

是的,我想,你应该从removeView(btn)开始。但是不要忘记设置btn=null。因此,您告诉Java机器可以释放内存。