我已经实现了一个随机图像切换器,它获取一组图像,然后随着时间的推移循环它,从一个线程接收消息。
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+ 设备上。我怎样才能弄清楚这个错误。