通过imageview破坏消耗内存

时间:2011-08-10 06:41:35

标签: android

我在特定时间(2秒)替换了imageview中的多个图像.image保留在drawable文件夹中。应用程序在1.5。左右。
现在我想在显示秒时破坏以前的图像。因此可以轻松释放内存。 任何人帮助我?

感谢
尼克

更新::

package com.TouchSigns;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class tranning extends Activity {
    /** Called when the activity is first created. */
    CountDownTimer tns_DCounter;
    Bitmap bm;
    ImageButton tran_btn_skip;
    TextView tran_banner;

    Intent tranningIntent, tranningIntent2;
    ImageView tran_imgwv_anim_image, tran_imgwv_playnow;
    private MediaPlayer xmPlayer2, xmPlayer3, xmPlayer4, xmPlayer5, xmPlayer6,
            xmPlayer7,xmPlayer8,xmPlayer9,xmPlayer10,xmPlayer11;
    CountDownTimer aCounter, aCounter2, aCounter3, aCounter4, aCounter5,
            aCounter6, aCounter7,aCounter8,aCounter9,aCounter10,aCounter11;
    Boolean flag = false;
    Typeface face;
    TouchSigns shaneshAv = new TouchSigns();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tranning);
        {
            try
            {
                tran_banner = (TextView) findViewById(R.id.tran_banner);

                tran_btn_skip = (ImageButton) findViewById(R.id.tran_btn_skip);

                tran_imgwv_playnow = (ImageView) findViewById(R.id.tran_imgwv_playnow);
                tran_imgwv_anim_image = (ImageView) findViewById(R.id.tran_imgwv_anim_image);
                xmPlayer2 = MediaPlayer.create(this, R.raw.bike_route);
                xmPlayer3 = MediaPlayer.create(this, R.raw.closed);
                xmPlayer4 = MediaPlayer.create(this, R.raw.exit);
                xmPlayer5 = MediaPlayer.create(this, R.raw.no_parking);
                xmPlayer6 = MediaPlayer.create(this, R.raw.one_way);
                xmPlayer7 = MediaPlayer.create(this, R.raw.phone);
                xmPlayer8 = MediaPlayer.create(this, R.raw.rail_road_crossing);
                xmPlayer9 = MediaPlayer.create(this, R.raw.sale);
                xmPlayer10 = MediaPlayer.create(this, R.raw.stop);
                xmPlayer11 = MediaPlayer.create(this, R.raw.turn_off_tap);




                tranningIntent = new Intent(this, touch_and_show.class);
                animmation();
                /*tran_btn_exit.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        System.exit(0);
                    }
                });*/
                tran_btn_skip.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        try
                        {
                        flag = true;
                        /*xmPlayer2.stop();
                        xmPlayer3.stop();
                        xmPlayer4.stop();
                        xmPlayer5.stop();
                        xmPlayer6.stop();
                        xmPlayer7.stop();
                        xmPlayer8.stop();
                        xmPlayer9.stop();
                        xmPlayer10.stop();
                        xmPlayer11.stop();*/


                        startActivity(tranningIntent);
                        tranningIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        finish();
                        }catch (Exception e) {

                        }
                    }
                });

                System.out.println(flag);


                    face = Typeface.createFromAsset(this.getAssets(),
                            "fonts/comic.TTF");








            }catch (Exception e) {

            }
        }

    }

    public void animmation() {
            try
            {

        if(flag==true)
        {   
            try
            {
            xmPlayer2.stop();
            xmPlayer3.stop();
            xmPlayer4.stop();
            xmPlayer5.stop();
            xmPlayer6.stop();
            xmPlayer7.stop();
            xmPlayer8.stop();
            xmPlayer9.stop();
            xmPlayer10.stop();
            xmPlayer11.stop();

            aCounter10.cancel();
            aCounter2.cancel();
            aCounter3.cancel();
            aCounter4.cancel();
            aCounter5.cancel();
            aCounter6.cancel();
            aCounter7.cancel();
            aCounter8.cancel();
            aCounter9.cancel();
            aCounter11.cancel();
            }catch (Exception e) {

            }
        }
        else
        {


        aCounter = new CountDownTimer(2000, 1000) {
            public void onTick(long millisUntilFinished) {
                xmPlayer2.start();
                tran_banner.setTypeface(face, Typeface.BOLD);
                tran_banner.setText("Route Sign");
                //tran_imgwv_anim_image.setImageResource(R.drawable.sm_bike_route);
                 bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_bike_route);
                tran_imgwv_anim_image.setImageBitmap(bm);
            }

            public void onFinish() {
                if(flag==true)
                {   try{
                    xmPlayer2.stop();
                    xmPlayer3.stop();
                    xmPlayer4.stop();
                    xmPlayer5.stop();
                    xmPlayer6.stop();
                    xmPlayer7.stop();
                    xmPlayer8.stop();
                    xmPlayer9.stop();
                    xmPlayer10.stop();
                    xmPlayer11.stop();



                    aCounter10.cancel();
                    aCounter2.cancel();
                    aCounter3.cancel();
                    aCounter4.cancel();
                    aCounter5.cancel();
                    aCounter6.cancel();
                    aCounter7.cancel();
                    aCounter8.cancel();
                    aCounter9.cancel();
                    aCounter11.cancel();
                }
                catch (Exception e) {

                }
                }
                else
                {
                aCounter2 = new CountDownTimer(2000, 1000) {
                    public void onTick(long millisUntilFinished) {
                        try
                        {
                        xmPlayer2.release();
                        xmPlayer3.start();
                        tran_banner.setTypeface(face, Typeface.BOLD);
                        tran_banner.setText("Closed Sign");
                        //tran_imgwv_anim_image.setImageResource(R.drawable.sm_closed);
                        bm.recycle();
                         bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_closed);
                        tran_imgwv_anim_image.setImageBitmap(bm);
                        }catch (Exception e) {
                            // TODO: handle exception
                        }
                        }
                    public void onFinish() {
                        if(flag==true)
                        {   
                            try
                            {

                            xmPlayer3.stop();
                            xmPlayer4.stop();
                            xmPlayer5.stop();
                            xmPlayer6.stop();
                            xmPlayer7.stop();
                            xmPlayer8.stop();
                            xmPlayer9.stop();
                            xmPlayer10.stop();
                            xmPlayer11.stop();


                            aCounter.cancel();
                            aCounter10.cancel();

                            aCounter3.cancel();
                            aCounter4.cancel();
                            aCounter5.cancel();
                            aCounter6.cancel();
                            aCounter7.cancel();
                            aCounter8.cancel();
                            aCounter9.cancel();
                            aCounter11.cancel();
                            }
                            catch (Exception e) {

                            }
                        }
                        else
                        {
                        aCounter3 = new CountDownTimer(2000, 1000) {
                            public void onTick(long millisUntilFinished) {
                                try
                                {
                                xmPlayer3.release();
                                xmPlayer4.start();
                                tran_banner.setTypeface(face, Typeface.BOLD);
                                tran_banner.setText("Exit Sign");

                                //tran_imgwv_anim_image.setImageResource(R.drawable.sm_exit);
                                bm.recycle();
                                bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_exit);
                                tran_imgwv_anim_image.setImageBitmap(bm);
                                }catch (Exception e) {

                                }
                                }
                            public void onFinish() {
                                if(flag==true)
                                {   
                                    try{


                                    xmPlayer4.stop();
                                    xmPlayer5.stop();
                                    xmPlayer6.stop();
                                    xmPlayer7.stop();
                                    xmPlayer8.stop();
                                    xmPlayer9.stop();
                                    xmPlayer10.stop();
                                    xmPlayer11.stop();


                                    aCounter.cancel();
                                    aCounter10.cancel();
                                    aCounter2.cancel();

                                    aCounter4.cancel();
                                    aCounter5.cancel();
                                    aCounter6.cancel();
                                    aCounter7.cancel();
                                    aCounter8.cancel();
                                    aCounter9.cancel();
                                    aCounter11.cancel();
                                    }
                                    catch (Exception e) {

                                    }
                                }
                                else
                                {
                                aCounter4 = new CountDownTimer(2000, 1000) {
                                    public void onTick(long millisUntilFinished) {
                                        try
                                        {
                                        xmPlayer4.release();
                                        xmPlayer5.start();
                                        tran_banner.setTypeface(face, Typeface.BOLD);
                                        tran_banner.setText("No Parking Sign");

                                        //tran_imgwv_anim_image.setImageResource(R.drawable.sm_no_parking);
                                        bm.recycle();
                                        bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_no_parking);
                                        tran_imgwv_anim_image.setImageBitmap(bm);
                                        }catch (Exception e) {

                                        }
                                        }
                                    public void onFinish() {
                                        if(flag==true)
                                        {   
                                            try
                                            {

                                            xmPlayer5.stop();
                                            xmPlayer6.stop();
                                            xmPlayer7.stop();
                                            xmPlayer8.stop();
                                            xmPlayer9.stop();
                                            xmPlayer10.stop();
                                            xmPlayer11.stop();


                                            aCounter.cancel();
                                            aCounter10.cancel();
                                            aCounter2.cancel();
                                            aCounter3.cancel();

                                            aCounter5.cancel();
                                            aCounter6.cancel();
                                            aCounter7.cancel();
                                            aCounter8.cancel();
                                            aCounter9.cancel();
                                            aCounter11.cancel();
                                            }
                                            catch (Exception e) {

                                            }
                                        }
                                        else
                                        {
                                        aCounter5 = new CountDownTimer(2000, 1000) {
                                            public void onTick(long millisUntilFinished) {
                                                try
                                                {
                                                xmPlayer5.release();
                                                xmPlayer6.start();
                                                tran_banner.setTypeface(face, Typeface.BOLD);
                                                tran_banner.setText("One Way Sign");

                                                //tran_imgwv_anim_image.setImageResource(R.drawable.sm_oneway);
                                                bm.recycle();
                                                bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_oneway);
                                                tran_imgwv_anim_image.setImageBitmap(bm);
                                                }catch (Exception e) {
                                                    // TODO: handle exception
                                                }

                                                }
                                            public void onFinish() {
                                                if(flag==true)
                                                {   
                                                    try
                                                    {

                                                    xmPlayer6.stop();
                                                    xmPlayer7.stop();
                                                    xmPlayer8.stop();
                                                    xmPlayer9.stop();
                                                    xmPlayer10.stop();
                                                    xmPlayer11.stop();


                                                    aCounter.cancel();
                                                    aCounter10.cancel();
                                                    aCounter2.cancel();
                                                    aCounter3.cancel();
                                                    aCounter4.cancel();

                                                    aCounter6.cancel();
                                                    aCounter7.cancel();
                                                    aCounter8.cancel();
                                                    aCounter9.cancel();
                                                    aCounter11.cancel();
                                                    }
                                                    catch (Exception e) {

                                                    }
                                                }
                                                else
                                                {
                                                aCounter6 = new CountDownTimer(2000, 1000) {
                                                    public void onTick(long millisUntilFinished) {
                                                        try
                                                        {
                                                        xmPlayer6.release();
                                                        xmPlayer7.start();
                                                        tran_banner.setTypeface(face, Typeface.BOLD);
                                                        tran_banner.setText("Phone Sign");

                                                        //tran_imgwv_anim_image.setImageResource(R.drawable.sm_phone);
                                                        bm.recycle();
                                                        bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_phone);
                                                        tran_imgwv_anim_image.setImageBitmap(bm);
                                                        }catch (Exception e) {
                                                            // TODO: handle exception
                                                        }
                                                        }
                                                    public void onFinish() {
                                                        if(flag==true)
                                                        {   
                                                            try
                                                            {

                                                            xmPlayer6.stop();
                                                            xmPlayer7.stop();
                                                            xmPlayer8.stop();
                                                            xmPlayer9.stop();
                                                            xmPlayer10.stop();
                                                            xmPlayer11.stop();


                                                            aCounter.cancel();
                                                            aCounter10.cancel();
                                                            aCounter2.cancel();
                                                            aCounter3.cancel();
                                                            aCounter4.cancel();
                                                            aCounter5.cancel();

                                                            aCounter7.cancel();
                                                            aCounter8.cancel();
                                                            aCounter9.cancel();
                                                            aCounter11.cancel();
                                                            }
                                                            catch (Exception e) {

                                                            }
                                                        }
                                                        else
                                                        {
                                                        aCounter7 = new CountDownTimer(2000, 1000) {
                                                            public void onTick(long millisUntilFinished) {
                                                                try
                                                                {
                                                                xmPlayer7.release();
                                                                xmPlayer8.start();
                                                                tran_banner.setTypeface(face, Typeface.BOLD);
                                                                tran_banner.setText("Rail Crossing Sign");
                                                                //tran_imgwv_anim_image.setImageResource(R.drawable.sm_rail_crossing);
                                                                bm.recycle();
                                                                bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_rail_crossing);
                                                                tran_imgwv_anim_image.setImageBitmap(bm);
                                                                }catch (Exception e) {
                                                                    // TODO: handle exception
                                                                }
                                                                }
                                                            public void onFinish() {
                                                                if(flag==true)
                                                                {   
                                                                    try
                                                                    {
                                                                    xmPlayer2.stop();
                                                                    xmPlayer3.stop();
                                                                    xmPlayer4.stop();
                                                                    xmPlayer5.stop();
                                                                    xmPlayer6.stop();
                                                                    xmPlayer7.stop();
                                                                    xmPlayer8.stop();
                                                                    xmPlayer9.stop();
                                                                    xmPlayer10.stop();
                                                                    xmPlayer11.stop();


                                                                    aCounter.cancel();
                                                                    aCounter10.cancel();
                                                                    aCounter2.cancel();
                                                                    aCounter3.cancel();
                                                                    aCounter4.cancel();
                                                                    aCounter5.cancel();
                                                                    aCounter6.cancel();

                                                                    aCounter8.cancel();
                                                                    aCounter9.cancel();
                                                                    aCounter11.cancel();
                                                                    }
                                                                    catch (Exception e) {

                                                                    }
                                                                }
                                                                else
                                                                {
                                                                aCounter8 = new CountDownTimer(2000, 1000) {
                                                                    public void onTick(long millisUntilFinished) {
                                                                        try
                                                                        {
                                                                        xmPlayer8.release();
                                                                        xmPlayer9.start();
                                                                        tran_banner.setTypeface(face, Typeface.BOLD);
                                                                        tran_banner.setText("Sale Sign");
                                                                        //tran_imgwv_anim_image.setImageResource(R.drawable.sm_sale);
                                                                        bm.recycle();
                                                                        bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_sale);
                                                                        tran_imgwv_anim_image.setImageBitmap(bm);
                                                                        }catch (Exception e) {

                                                                        }
                                                                        }
                                                                    public void onFinish() {
                                                                        if(flag==true)
                                                                        {
                                                                            try
                                                                            {
                                                                            xmPlayer2.stop();
                                                                            xmPlayer3.stop();
                                                                            xmPlayer4.stop();
                                                                            xmPlayer5.stop();
                                                                            xmPlayer6.stop();
                                                                            xmPlayer7.stop();
                                                                            xmPlayer8.stop();
                                                                            xmPlayer9.stop();
                                                                            xmPlayer10.stop();
                                                                            xmPlayer11.stop();


                                                                            aCounter.cancel();
                                                                            aCounter10.cancel();
                                                                            aCounter2.cancel();
                                                                            aCounter3.cancel();
                                                                            aCounter4.cancel();
                                                                            aCounter5.cancel();
                                                                            aCounter6.cancel();
                                                                            aCounter7.cancel();

                                                                            aCounter9.cancel();
                                                                            aCounter11.cancel();
                                                                            }
                                                                            catch (Exception e) {
                                                                                // TODO: handle exception
                                                                            }
                                                                        }
                                                                        else
                                                                        {
                                                                        aCounter9 = new CountDownTimer(2000, 1000) {
                                                                            public void onTick(long millisUntilFinished) {
                                                                                try
                                                                                {
                                                                                xmPlayer9.release();
                                                                                xmPlayer10.start();
                                                                                tran_banner.setTypeface(face, Typeface.BOLD);
                                                                                tran_banner.setText("Stop Sign");

                                                                                //tran_imgwv_anim_image.setImageResource(R.drawable.sm_stop);
                                                                                bm.recycle();
                                                                                bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_stop);
                                                                                tran_imgwv_anim_image.setImageBitmap(bm);
                                                                                }catch (Exception e) {
                                                                                    // TODO: handle exception
                                                                                }
                                                                                }
                                                                            public void onFinish() {
                                                                                if(flag==true)
                                                                                {   
                                                                                    try
                                                                                    {
                                                                                    xmPlayer2.stop();
                                                                                    xmPlayer3.stop();
                                                                                    xmPlayer4.stop();
                                                                                    xmPlayer5.stop();
                                                                                    xmPlayer6.stop();
                                                                                    xmPlayer7.stop();
                                                                                    xmPlayer8.stop();
                                                                                    xmPlayer9.stop();
                                                                                    xmPlayer10.stop();
                                                                                    xmPlayer11.stop();


                                                                                    aCounter.cancel();
                                                                                    aCounter10.cancel();
                                                                                    aCounter2.cancel();
                                                                                    aCounter3.cancel();
                                                                                    aCounter4.cancel();
                                                                                    aCounter5.cancel();
                                                                                    aCounter6.cancel();
                                                                                    aCounter7.cancel();
                                                                                    aCounter8.cancel();

                                                                                    aCounter11.cancel();
                                                                                    }
                                                                                    catch (Exception e) {

                                                                                    }
                                                                                }
                                                                                else
                                                                                {
                                                                                aCounter10 = new CountDownTimer(2000, 1000) {
                                                                                    public void onTick(long millisUntilFinished) {
                                                                                        try
                                                                                        {
                                                                                        xmPlayer10.release();
                                                                                        xmPlayer11.start();
                                                                                        tran_banner.setTypeface(face, Typeface.BOLD);
                                                                                        tran_banner.setText("Turn off Tap Sign");

                                                                                        //tran_imgwv_anim_image.setImageResource(R.drawable.sm_turn_off);
                                                                                        bm.recycle();
                                                                                        bm = BitmapFactory.decodeResource(getResources(),R.drawable.sm_turn_off);
                                                                                        tran_imgwv_anim_image.setImageBitmap(bm);
                                                                                        }catch (Exception e) {
                                                                                        }
                                                                                        }
                                                                                    public void onFinish() {

                                                                                        aCounter11 = new CountDownTimer(2000, 1000) {
                                                                                            public void onTick(long millisUntilFinished) {

                                                                                                invisibleControl();
                                                                                            }
                                                                                            public void onFinish() {
                                                                                                try {
                                                                                                /*xmPlayer2
                                                                                                .stop();
                                                                                        xmPlayer3
                                                                                                .stop();
                                                                                        xmPlayer4
                                                                                                .stop();
                                                                                        xmPlayer5
                                                                                                .stop();
                                                                                        xmPlayer6
                                                                                                .stop();
                                                                                        xmPlayer7
                                                                                                .stop();*/


                                                                                            aCounter.cancel();
                                                                                            aCounter2
                                                                                                    .cancel();
                                                                                            aCounter3
                                                                                                    .cancel();
                                                                                            aCounter4
                                                                                                    .cancel();
                                                                                            aCounter5
                                                                                                    .cancel();
                                                                                            aCounter6
                                                                                                    .cancel();


                                                                                                startActivity(tranningIntent);
                                                                                                finish();
                                                                                        }catch (Exception e) {

                                                                                        }
                                                                                            }}; aCounter11.start();
                                                                                    }
                                                                                }; aCounter10.start();
                                                                                }//else
                                                                            }
                                                                        }; aCounter9.start();
                                                                        }//else
                                                                    }
                                                                }; aCounter8.start();
                                                                }//else
                                                            }
                                                        }; aCounter7.start();
                                                        }//else
                                                    }
                                                }; aCounter6.start();
                                                }//else
                                            }
                                        }; aCounter5.start();
                                        }//else
                                    }
                                }; aCounter4.start();
                                }//else
                                }
                        }; aCounter3.start();



                        }//else
                    }
                    };
                    aCounter2.start();

                }//second else 
            }}; aCounter.start();   

            }

            }catch (Exception e) {

            }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            try
            {       flag=true;
                stopService(new Intent(this, backServices.class));



            finish();
            }catch (Exception e) {

            }
        }
        return super.onKeyDown(keyCode, event);
    }
     @Override
     protected void onDestroy() {
         super.onDestroy();
 try
 {
            xmPlayer2.release();
            xmPlayer3.release();
            xmPlayer4.release();
            xmPlayer5.release();
            xmPlayer6.release();
            xmPlayer7.release();
            xmPlayer8.release();
            xmPlayer9.release();
            xmPlayer10.release();
            xmPlayer11.release();
            System.gc();

 }catch (Exception e) {

 } 
 }
    private void invisibleControl() {

        RelativeLayout r;
        LinearLayout trans_linearll;
        trans_linearll = (LinearLayout) findViewById(R.id.trans_linearll);
        r = (RelativeLayout) findViewById(R.id.rela_tranning);
        r.setBackgroundResource(R.drawable.playnow);

        tran_btn_skip.setVisibility(View.INVISIBLE);
        trans_linearll.setVisibility(View.INVISIBLE);
        tran_banner.setVisibility(View.INVISIBLE);

        tran_imgwv_anim_image.setVisibility(View.INVISIBLE);
    /*  tran_imgwv_playnow.setVisibility(View.VISIBLE);*/


    }


}

1 个答案:

答案 0 :(得分:5)

Java有垃圾收集器。不再使用对象时,对象将自动删除。你不需要做任何事情。

但是如果你想提高速度,请看看: Bitmap.recycle()

http://developer.android.com/reference/android/graphics/Bitmap.html#recycle%28%29

Bitmap myBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.myImage1);
imageView.setImageBitmap(myBitmap1);
/* ... */
Bitmap myBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.myImage2);
imageView.setImageBitmap(myBitmap2);
myBitmap1.recycle(); // Don't need myBitmap1 anymore