我的 gif 图像无法在我的 tabhost 上运行。当我尝试按下锁定/电源按钮然后返回到它运行的应用程序时。我想修复它,但我不知道我的代码中的问题是什么,因为它没有任何错误。我尝试制作一个不包含 tabhost 并且我的 gif 图像运行的新项目。
这是我的代码
AnimatedUtils.java
public class AnimatedUtils extends Activity {
/**
* start/stop animation
*
* @param view
* @param start
*/
static public void startViewAnimation(View view, boolean start) {
if (view != null) {
// background drawable
startDrawableAnimation(view.getBackground(), start);
if (view instanceof ImageView) {
// image drawable
startDrawableAnimation(((ImageView)view).getDrawable(), start);
}
}
}
/**
* start/stop animation
*
* @param d
* @param start
*/
static public void startDrawableAnimation(Drawable d, boolean start) {
if (d instanceof AnimationDrawable) {
if (start) {
((AnimationDrawable)d).start();
} else {
((AnimationDrawable)d).stop();
}
}
}
/**
* load drawable from resource id
*
* @param rsrc
* @param resid
* @return
*/
static public Drawable loadDrawableFromResource(Resources rsrc, int resid) {
// load from resource
Movie movie = Movie.decodeStream(rsrc.openRawResource(resid));
if ((movie != null) && movie.duration() > 0) {
return makeMovieDrawable(rsrc, movie);
} else {
// not animated GIF
return rsrc.getDrawable(resid);
}
}
/**
* load drawable from file path
*
* @param rsrc
* @param path
* @return
*/
static public Drawable loadDrawableFromFile(Resources rsrc, String path) {
// load from file
// Movie movie = Movie.decodeFile(path);
Movie movie = null;
try {
File file = new File(path);
FileInputStream is = new FileInputStream(file);
byte data[] = new byte[(int)file.length()];
is.read(data);
is.close();
movie = Movie.decodeByteArray(data, 0, data.length);
} catch (Exception e) {
}
if ((movie != null) && movie.duration() > 0) {
return makeMovieDrawable(rsrc, movie);
} else {
// not animated GIF
return Drawable.createFromPath(path);
}
}
/**
* make AnimationDrawable from Movie instance
*
* @param rsrc
* @param movie
* @return
*/
static private Drawable makeMovieDrawable(Resources rsrc, Movie movie) {
int duration = movie.duration();
int width = movie.width(), height = movie.height();
AnimationDrawable result = new AnimationDrawable();
result.setOneShot(false); // for loop
Drawable frame = null;
int start = 0;
for (int time = 0; time < duration; time += 10) {
if (movie.setTime(time)) {
if (frame != null) {
// add previous frame
result.addFrame(frame, time - start);
}
// make frame
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565); // save heap
//Bitmap.Config.ARGB_8888); // high quality
movie.draw(new Canvas(bitmap), 0, 0);
frame = new BitmapDrawable(rsrc, bitmap);
start = time;
}
}
if (frame != null) {
// add last frame
result.addFrame(frame, duration - start);
}
return result;
}
}
gifimage.java
public class gifimage extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Drawable d;
d = AnimatedUtils.loadDrawableFromResource(getResources(),
R.drawable.gif1);
((ImageView)findViewById(R.id.imageView1)).setImageDrawable(d);
Drawable e;
e = AnimatedUtils.loadDrawableFromResource(getResources(),
R.drawable.gif2);
((ImageView)findViewById(R.id.imageView2)).setImageDrawable(e);
Drawable f;
f = AnimatedUtils.loadDrawableFromResource(getResources(),
R.drawable.gif3);
((ImageView)findViewById(R.id.imageView3)).setImageDrawable(f);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// start/stop animation
AnimatedUtils.startViewAnimation(findViewById(R.id.imageView1), hasFocus);
AnimatedUtils.startViewAnimation(findViewById(R.id.imageView2), hasFocus);
AnimatedUtils.startViewAnimation(findViewById(R.id.imageView3), hasFocus);
}
}