简而言之,我有一个活动A,它应用setContentView(R.id.layout_a)
,A将运行5秒。
我尝试定义自定义View类并使用Movie类加载gif,然后将自定义视图附加到layout_a
,但它只显示为空白。
我尝试过WebView,它可以很好地显示gif,但gif只能在我第一次启动此活动时制作动画。它将仅显示后期活动A启动时的最后一帧。
我尝试了WebView.reload()
,但没有尝试。
这是我尝试应用WebView时的代码:
public class activityA extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_a);
WebView splashFrame = (WebView)findViewById(R.id.splashFrame);
splashFrame.setBackgroundColor(Color.TRANSPARENT);
splashFrame.loadDataWithBaseURL("file:///android_res/drawable/", "<img align='middle' src='face_morph_gif.gif' width='100%' />", "text/html", "utf-8", null);
splashFrame.reload();
Handler handler = new Handler();
// run a thread after 3 seconds to start the home screen
handler.postDelayed(new Runnable() {
@Override
public void run() {
// make sure we close the splash screen so the user won't come back when it presses back key
finish();
// start the home screen
Intent intent = new Intent(activityA.this, activityB.class);
SplashScreenActivity.this.startActivity(intent);
}
}, 5000);
}
}
所以我的问题1 :当我启动活动A时,如何从头开始制作这个gif游戏?我注意到当我使用Safari / Chrome打开这个gif时,它只播放一次,直到我手动刷新浏览器。
这是我调查的第一种方式,但我连一次都找不到工作。
活动A代码:
public class activityA extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_a);
Handler handler = new Handler();
// run a thread after 3 seconds to start the home screen
handler.postDelayed(new Runnable() {
@Override
public void run() {
// make sure we close the splash screen so the user won't come back when it presses back key
finish();
// start the home screen
Intent intent = new Intent(activityA.this, activityB.class);
SplashScreenActivity.this.startActivity(intent);
}
}, 5000);
}
}
客户视图:
public class GifView extends View {
public Movie mMovie;
public long mMovieStart;
public GifView(Context context) {
super(context);
setFocusable(true);
InputStream is = context.getResources().openRawResource(R.drawable.face_morph_gif);
mMovie = Movie.decodeStream(is);
}
public GifView(Context context, AttributeSet attr) {
super(context, attr);
setFocusable(true);
InputStream is = context.getResources().openRawResource(R.drawable.face_morph_gif);
mMovie = Movie.decodeStream(is);
}
public GifView(Context context, AttributeSet attr, int defStyle) {
super(context, attr, defStyle);
setFocusable(true);
InputStream is = context.getResources().openRawResource(R.drawable.face_morph_gif);
mMovie = Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawColor(Color.TRANSPARENT);
final long now = SystemClock.uptimeMillis();
if (mMovieStart == 0) {
mMovieStart = now;
}
if (mMovie != null) {
int dur = mMovie.duration();
// Log.d("gif Canvas", "duration: " + mMovie.duration());
if (dur == 0) {
dur = 4000;
}
int relTime = (int)((now - mMovieStart) % dur);
mMovie.setTime(relTime);
mMovie.draw(canvas, 10, 10);
// Log.d("gif Canvas", mMovie.width() + "x" + mMovie.height());
invalidate();
}
}
@Override
protected void onMeasure(final int widthMeasureSpec,
final int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());
}
}
和layout_a的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_background"
tools:ignore="ContentDescription" >
<ImageView
... />
<ImageView
... />
<ImageView
... />
<ImageView
... />
<com.xxx.GifView
android:id="@+id/splashFrame"
android:layout_width="95dp"
android:layout_height="156dp"
android:visibility="visible" />
所以我的问题2 :为什么这个自定义视图无法显示gif?我在onDraw
方法中设置了断点,我确信mMovie可以加载gif(mMovie可以返回正确的gif宽度和高度)
我知道让GIF在Android上工作是一个相当古老的话题,AFAIK有3种方式:
电影课
自定义视图
拆分GIF /使用Android动画
我不能使用第三种方式,因为我使用的gif有100帧,而且不便宜。
我已经完成了所有教程和SO问题,但没有一个可以使这项工作。
感谢您阅读我的问题。
答案 0 :(得分:1)
为了在Android手机中播放GIF动画,我建议您使用WebView(仅适用于Android 2.2更高版本)。
可以在这里找到一个好的教程: http://droid-blog.net/2011/10/17/tutorial-how-to-play-animated-gifs-in-android-part-3/
每次打开WebView时都要小心清除缓存以播放动画GIF。使用webView.clearCache(false);
享受。
答案 1 :(得分:0)
您可以将GIF转换为WebP格式。
在线提供了一些实用程序,其中之一是:http://www.zamzar.com/convert/gif-to-webp/