Android位图,自定义ImageSwitcher和内存不足错误

时间:2012-10-01 13:27:32

标签: android memory-leaks

我已经实现了一个随机图像切换器,它接收一系列图像,然后随着时间的推移循环,从线程接收消息。

package com.silice.smallshi;

import com.silice.smallshi.R;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
import android.widget.RelativeLayout.LayoutParams;
// esta clase es una vista custom basada en un ImageSwitcher que realiza transiciones de manera aleatoria
public class RandomSlider extends ImageSwitcher implements ViewFactory {
    private RandomSliderManager thisManager;
    private Handler innerHandler;
    private int ImagesId[] = {R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5};

    public RandomSlider(Context context) {
        this(context,null);
    }

    public RandomSlider(Context context, AttributeSet attr) {
        super(context,attr);
        this.setFactory(this);
        this.setInAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in));
        this.setOutAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_out));
        innerHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) 
            {
                Bundle b = msg.getData();
                int index = b.getInt("index");
                setImageResource(ImagesId[index]);
            }
        };
    }
    // funcion que permite empezar a cambiar imagenes de modo random 
    public void startRandom(){
        thisManager = new RandomSliderManager();
        thisManager.setHandler(innerHandler);
        thisManager.start();
    }
    // esta funcion detiene el cambio de imagen de manera random
    public void stopRandom(){
        if(thisManager.isAlive()){
            thisManager.interrupt();
            thisManager = null;
        }
    }
    public View makeView() {
        ImageView iView = new ImageView(getContext());
        iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iView.setLayoutParams(new
                    ImageSwitcher.LayoutParams(
                                LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        iView.setBackgroundColor(0xFF000000);

        return iView;
    }
    @Override
    protected void onFinishInflate(){
        super.onFinishInflate();
        this.startRandom();
    }

这里是RandomSlider类

package com.silice.smallshi;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
// este es el responsable de pedirle al RandomSlider que cambie de imagen
public class RandomSliderManager extends Thread{
    private Handler innerHandler, objHandler;
    private boolean running = false;
    private int lastindex = 0;

    @Override
    public void run(){
        /*Looper.prepare();
        innerHandler = new Handler();
        Message message = innerHandler.obtainMessage();
        innerHandler.dispatchMessage(message);
        Looper.loop();*/
        running = true;
        while(running){
            try {
                int index = (int)(Math.random() * ((4) + 1));
                Log.v("Index:", ""+index);
                if(index!=lastindex){
                    lastindex = index;  
                }else{

                    Bundle b = new Bundle();
                    b.putInt("index", index);
                    Message msg = new Message();
                    msg.setData(b);
                    objHandler.sendMessage(msg);
                    Thread.sleep(7000);
            }
            } catch (InterruptedException e) {
                running = false;
            }
        }
    }

    public boolean isRunning(){
        return running;

    }

    public void setHandler(Handler rHandler){
        objHandler = rHandler;
    }

}

这是RadomSliderManager类,其中线程每x秒发送一次消息到随机滑块,说嘿这是新的随机索引改变它。 有时我的应用程序会让我失去内存,特别是在4.0+设备上。我怎么能弄清楚这个错误。

0 个答案:

没有答案