我按照本指南 了解如何将动画 gif 合并到 Android 应用程序布局中。
我的 GIFView java 文件如下:
package com.example.bilisattendancerecorder;
public class GIFView extends View {
private Movie mMovie;
private long movieStart;
private int gifId;
private void setAttrs(AttributeSet attrs) {
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GIFView, 0, 0);
String gifSource = a.getString(R.styleable.GIFView_src);
//little workaround here. Who knows better approach on how to easily get resource id - please share
String sourceName = Uri.parse(gifSource).getLastPathSegment().replace(".gif", "");
setGIFResource(getResources().getIdentifier(sourceName, "drawable", getContext().getPackageName()));
a.recycle();
}
}
public GIFView(Context context) {
super(context);
initializeView();
}
public GIFView(Context context, AttributeSet attrs) {
super(context, attrs);
setAttrs(attrs);
initializeView();
}
public GIFView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setAttrs(attrs);
initializeView();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
super.onDraw(canvas);
long now = android.os.SystemClock.uptimeMillis();
if (movieStart == 0) {
movieStart = now;
}
if (mMovie != null) {
int relTime = (int) ((now - movieStart) % mMovie.duration());
mMovie.setTime(relTime);
mMovie.draw(canvas, getWidth() - mMovie.width(), getHeight() - mMovie.height());
this.invalidate();
}
}
public void setGIFResource(int resId) {
this.gifId = resId;
initializeView();
}
public int getGIFResource() {
return this.gifId;
}
private void initializeView() {
if (gifId != 0) {
InputStream is = getContext().getResources().openRawResource(gifId);
mMovie = Movie.decodeStream(is);
movieStart = 0;
this.invalidate();
}
}}
我的 XML 布局如下。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:components="http://schemas.android.com/apk/res/com.example.bilisattendancerecorder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:hardwareAccelerated="false"
android:background="@drawable/gradient_background"
tools:context=".Main" >
<com.example.bilisattendancerecorder.widget.GIFView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/imageView1"
components:src="@drawable/loading_66_drk" />
<TextView
android:id="@+id/hellogoodbye"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Checking..."
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/biliswhite"
android:textSize="50sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="161dp"
android:layout_height="46dp"
android:src="@drawable/logo" />
atrrs 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GIFView">
<attr name="src" format="reference" />
</declare-styleable>
</resources>
logcat 说明了以下内容,但值得注意的是,我只会在布局 xml 文件的 gifView 声明中添加简短的“小部件”时遇到错误。对此,但没有画出 GIF。有什么我忽略的吗?
09-25 16:28:50.590: E/AndroidRuntime(19830): FATAL EXCEPTION: main
09-25 16:28:50.590: E/AndroidRuntime(19830): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bilisattendancerecorder/com.example.bilisattendancerecorder.Main}: android.view.InflateException: Binary XML file line #18: Error inflating class com.example.bilisattendancerecorder.widget.GIFView
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.os.Looper.loop(Looper.java:137)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-25 16:28:50.590: E/AndroidRuntime(19830): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 16:28:50.590: E/AndroidRuntime(19830): at java.lang.reflect.Method.invoke(Method.java:525)
09-25 16:28:50.590: E/AndroidRuntime(19830): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-25 16:28:50.590: E/AndroidRuntime(19830): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-25 16:28:50.590: E/AndroidRuntime(19830): at dalvik.system.NativeStart.main(Native Method)
09-25 16:28:50.590: E/AndroidRuntime(19830): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class com.example.bilisattendancerecorder.widget.GIFView
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-25 16:28:50.590: E/AndroidRuntime(19830): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.Activity.setContentView(Activity.java:1895)
09-25 16:28:50.590: E/AndroidRuntime(19830): at com.example.bilisattendancerecorder.Main.onCreate(Main.java:159)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.Activity.performCreate(Activity.java:5133)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-25 16:28:50.590: E/AndroidRuntime(19830): ... 11 more
09-25 16:28:50.590: E/AndroidRuntime(19830): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.bilisattendancerecorder.widget.GIFView" on path: DexPathList[[zip file "/data/app/com.example.bilisattendancerecorder-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.bilisattendancerecorder-2, /vendor/lib, /system/lib]]
09-25 16:28:50.590: E/AndroidRuntime(19830): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
09-25 16:28:50.590: E/AndroidRuntime(19830): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
09-25 16:28:50.590: E/AndroidRuntime(19830): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
09-25 16:28:50.590: E/AndroidRuntime(19830): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
任何真正的帮助将不胜感激。在过去的 4 个小时里,我一直在尝试插入一个 gif,而我唯一(有限)的成功是使用 webview。(由于加载时间,我认为这不是我的方法)。
谢谢。