gpt4 book ai didi

android - 在android中处理gif图像

转载 作者:太空狗 更新时间:2023-10-29 15:20:56 24 4
gpt4 key购买 nike

在我的 android 应用程序中,我正在使用 Movie 来显示 gif,但我面临的问题是,如果我收到扩展名为 .gif 且只有一帧图像的图像,它会显示空白屏幕。
请让我知道在 android 中显示 gif 的更好方法。
我使用的代码是

public MYGIFView(Context context, InputStream is, int i, int j,int screenWidth,int screenHeight) {
super(context);

//is=context.getResources().openRawResource(red);
movie=Movie.decodeStream(is);
xcoordinate = i;
ycoordinate = j;
viewWidth = screenWidth;
viewHeight = screenHeight;


setLayoutParams(new LayoutParams(viewWidth,viewHeight));

}

@Override
protected void onDraw(Canvas canvas) {
if(movie != null) {
long now = android.os.SystemClock.uptimeMillis();
int dur = Math.max(movie.duration(), 1); // is it really animated?
int pos = (int)(now % dur);
movie.setTime(pos);
movie.draw(canvas,xcoordinate, ycoordinate);
}

最佳答案

API demos itself provides a solution对此。

我认为你缺少的是这部电影的触发。

这样做:

Handler handler = new Handler();
new Thread() {
@Override public void run() {
// ... setup the movie (using the code from above)
// ... create and display the custom view, passing the movie

while(!Thread.currentThread().isInterrupted()) {
handler.post(new Runnable() {
public void run(){
view.invalidate();
}
});
try {
Thread.sleep(50); // yields 20 fps
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}.start();

或者如 API 演示所示,使 onDraw() 事件本身中的 View 无效,导致重绘,直到电影的持续时间结束。

关于android - 在android中处理gif图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7868275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com