2013-09-25 54 views
0

我跟着this guide瞭解如何將動畫gif合併到Android應用程序佈局中。Android:無法在佈局中使用動畫GIF。 (接下來的指南)

我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中說以下,但它是值得注意的,我只會打一個錯誤添加短的「小部件」在該gifView聲明佈局xml文件。 Priot,但沒有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。 (由於加載時間的原因,我決定這不是我的方法)。

謝謝。

回答

1

試試這個.. bacause你的包的名字是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" /> 
+0

謝謝,我已經離開但我明天可以測試它。但是,這看起來與沒有繪製gif時的情況完全相同。 – Margaret

+0

即使有了這個修復,gif也不顯示。 – Margaret