我关注如何将动画gif合并到Android应用程序布局中this guide。
我的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声明中添加短“widget”时出错。但是没有提出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。 (由于加载次数,我认为这不是我的方法。)
感谢。
答案 0 :(得分:1)
试试这个..因为您的包裹名称为package com.example.bilisattendancerecorder;
但您在xml中输入了com.example.bilisattendancerecorder.widget.GIFView
这就是ClassNotFoundException
来的原因..
<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.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" />