简而言之,我有一个活动a,它应用了setContentView(R.id.layout_a),a将运行5秒。我试图定义一个自定义视图类,并使用movie类加载gif,然后将自定义视图附加到layout_a中,但它仅显示为空。webview方法我尝试了webview,它可以很好地显示gif,但gif只能在我第一次启动此活动时设置动画。稍后启动活动A时,它将只显示最后一帧。我试过但没有甜味。下面是我在尝试应用webview时的代码:public class activityA extends Activity {@Overridepublic 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 {@Overridepublic 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);}@Overrideprotected 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());}}以及布局的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?我在WebView.reload()方法中设置了断点,我确信mmovie可以加载gif(mmovie可以返回正确的gif宽度和高度)我知道让gif在android上运行是一个很老的话题,不过有三种方法:电影课自定义视图分割gif/使用android动画我不能使用第三种方式,因为我使用的gif有100帧,而且不便宜。我已经完成了所有的教程和问题,但没有一个能让这工作。谢谢你阅读我的问题。 最佳答案 为了在你的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。使用享受。
07-24 09:47
查看更多