2013-04-05 72 views
10

Tôi mới sử dụng Android và gặp sự cố khi phát tệp .gif hoạt ảnh trong nền bố cục của tôi. Khi tôi chạy ứng dụng, nền là một khung hình duy nhất của ảnh động .gif, giống như một bức ảnh. Tôi đã thực hiện nghiên cứu chủ yếu ở hai khu vực:Đặt hoạt ảnh .GIF Làm nền Android

Liên kết bên dưới là nơi tôi được trợ giúp nhiều nhất.

Android OS Beginning BlogSpot

Các liên kết dưới đây khiến tôi nghĩ rằng các lỗi Tôi nhận được có cái gì để làm với các lớp học được thiết lập để tư nhân, công cộng, hoặc được bảo vệ.

Java Forums

Và đây là mã ------------------------------------- -------------------------------------------------- ----------------------------

Lỗi được biểu thị bằng :: ERROR # :: với mô tả đầy đủ bên dưới mã.

import java.io.InputStream; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Movie; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 

     super.onCreate(savedInstanceState); 

     setContentView(new MYGIFView()); :: ERROR 1 :: 
} 
} 

private class MYGIFView extends View{ :: ERROR 2 :: 
    Movie movie,movie1; 
    InputStream is=null , is1=null; 
    long moviestart; 
    public GIFView(Context context) { :: ERROR 3 :: 

     super(context);     :: ERROR 4 :: 

     is=context.getResources().openRawResource(R.drawable.swing); 

     movie=Movie.decodeStream(is); 
} 

@Override 
protected void onDraw(Canvas canvas) { 

    canvas.drawColor(Color.WHITE); 
    super.onDraw(canvas); 
    long now=android.os.SystemClock.uptimeMillis(); 
    System.out.println("now="+now); 
    if (moviestart == 0) { // first time 
    moviestart = now; 

} 

System.out.println("\tmoviestart="+moviestart); 
int relTime = (int)((now - moviestart) % movie.duration()) ; 
System.out.println("time="+relTime+"\treltime="+movie.duration()); 
movie.setTime(relTime); 
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40); 
this.invalidate(); 

} 
} 

:: LỖI DANH :: LỖI DANH :: LỖI DANH :: LỖI DANH :: LỖI DANH :: LỖI DANH ::

:: ERROR 1 :: Các MYGIFView constructor() không phải là có thể nhìn thấy

:: ERROR 2 :: Implicit Siêu constructor View() là undefined cho constructor có thể nhìn thấy

:: ERROR loại 3 :: Return cho phương pháp này là mất tích

:: ERROR 4 :: Lệnh gọi hàm dựng phải là câu lệnh đầu tiên trong một hàm tạo

.

.

---------- CẬP NHẬT --------- CẬP NHẬT ------------ CẬP NHẬT ------------ --- CẬP NHẬT ------------- CẬP NHẬT ---------- CẬP NHẬT --------- CẬP NHẬT ----------- - CẬP NHẬT --------------- CẬP NHẬT ------------- CẬP NHẬT --------- CẬP NHẬT

.

.

Tôi đã tạo ra danh sách phim hoạt hình = res/drawable-hdpi/progress_animation.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/selected" android:oneshot="false"> 
<item android:drawable="@drawable/frame02" android:duration="50" /> 
<item android:drawable="@drawable/frame03" android:duration="50" /> 
<item android:drawable="@drawable/frame04" android:duration="50" /> 
<item android:drawable="@drawable/frame05" android:duration="50" /> 
<item android:drawable="@drawable/frame06" android:duration="50" /> 
<item android:drawable="@drawable/frame07" android:duration="50" /> 
<item android:drawable="@drawable/frame08" android:duration="50" /> 
<item android:drawable="@drawable/frame09" android:duration="50" /> 
<item android:drawable="@drawable/frame10" android:duration="50" /> 
......................................... 
<item android:drawable="@drawable/frame57" android:duration="50" /> 
<item android:drawable="@drawable/frame58" android:duration="50" /> 
</animation-list> 

.

.

Tôi đã tạo ra tập tin bố trí để lưu trữ các hình ảnh Xem = res/layout/activity_main.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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ImageView android:id="@+id/swing_play" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

.

Java Class Hoạt động để gọi ImageView và AnimationDrawable = com.example.apptwo/MainActivity.java

.

package com.exampleone.apptwo; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.view.Menu; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.acivity_main); 
    ImageView img = (ImageView)findViewById(R.id.swing_play); 
    img.setBackgroundResource(R.drawable.progress_animation); 

    AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); 

    frameAnimation.start(); 
} 

} 

.

IDE không gắn cờ bất kỳ lỗi nào trong mã. Tuy nhiên, khi tôi chạy ứng dụng giả lập của tôi sẽ hiển thị một nền trắng với một thông báo "Thật không may, AppTwo đã dừng lại."

ErrorLog đọc = "Ngoại lệ vòng lặp sự kiện ngoại lệ"

LOGCAT ------------------------------- -------------------------------------------------- --------------------------------------------------

Đây là phiên Lọc logcat

04-06 17:10:48.193: D/AndroidRuntime(989): Shutting down VM 
04-06 17:10:48.193: W/dalvikvm(989): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
04-06 17:10:48.213: E/AndroidRuntime(989): FATAL EXCEPTION: main 
04-06 17:10:48.213: E/AndroidRuntime(989): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apptwo/com.example.apptwo.MainActivity}: java.lang.NullPointerException 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.os.Looper.loop(Looper.java:137) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-06 17:10:48.213: E/AndroidRuntime(989): at java.lang.reflect.Method.invokeNative(Native Method) 
04-06 17:10:48.213: E/AndroidRuntime(989): at java.lang.reflect.Method.invoke(Method.java:511) 
04-06 17:10:48.213: E/AndroidRuntime(989): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-06 17:10:48.213: E/AndroidRuntime(989): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-06 17:10:48.213: E/AndroidRuntime(989): at dalvik.system.NativeStart.main(Native Method) 
04-06 17:10:48.213: E/AndroidRuntime(989): Caused by: java.lang.NullPointerException 
04-06 17:10:48.213: E/AndroidRuntime(989): at com.example.apptwo.MainActivity.onCreate(MainActivity.java:14) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.Activity.performCreate(Activity.java:5104) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-06 17:10:48.213: E/AndroidRuntime(989): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
04-06 17:10:48.213: E/AndroidRuntime(989): ... 11 more 

có quá nhiều logCat (tất cả tin nhắn) để gửi, nhưng điều này dưới đây có vẻ là quan trọng trong khoảng thời gian của lỗi.

04-06 17:10:48.213: E/AndroidRuntime(989): FATAL EXCEPTION: main 

.

CẬP NHẬT ĐĂNG NHẬP VỚI XEM NỘI DUNG BAO GỒM! ! !

: error opening trace file: No such file or directory (2) 

: GC_FOR_ALLOC freed 67K, 7% free 2500K/2688K, paused 49ms, total 54ms 

: I/dalvikvm-heap(1813): Grow heap (frag case) to 4.022MB for 1536016-byte allocation 

: D/dalvikvm(1813): GC_FOR_ALLOC freed 2K, 5% free 3998K/4192K, paused 125ms, total 125ms 

: D/dalvikvm(1813): GC_CONCURRENT freed <1K, 5% free 3998K/4192K, paused 5ms+15ms, total 105ms 

: D/dalvikvm(1813): GC_FOR_ALLOC freed <1K, 5% free 3998K/4192K, paused 36ms, total 37ms 

: Grow heap (frag case) to 5.485MB for 1536016-byte allocation 

: GC_CONCURRENT freed <1K, 4% free 5498K/5696K, paused 75ms+14ms, total 156ms 

: GC_FOR_ALLOC freed <1K, 4% free 5499K/5696K, paused 32ms, total 33ms 

: Grow heap (frag case) to 6.950MB for 1536016-byte allocation 

: GC_CONCURRENT freed <1K, 3% free 6999K/7200K, paused 74ms+17ms, total 156ms 
: D/dalvikvm(1813): GC_FOR_ALLOC freed <1K, 3% free 6999K/7200K, paused 46ms, total 47ms 
: I/dalvikvm-heap(1813): Grow heap (frag case) to 8.416MB for 1536016-byte allocation 
: GC_CONCURRENT freed <1K, 3% free 8499K/8704K, paused 79ms+4ms, total 173ms 
: GC_FOR_ALLOC freed <1K, 3% free 10001K/10208K, paused 33ms, total 34ms 
: GC_FOR_ALLOC freed <1K, 2% free 11501K/11712K, paused 35ms, total 35ms 
: GC_FOR_ALLOC freed <1K, 2% free 13002K/13216K, paused 38ms, total 38ms 
: GC_FOR_ALLOC freed <1K, 2% free 14503K/14720K, paused 35ms, total 36ms 
: GC_FOR_ALLOC freed <1K, 2% free 16003K/16224K, paused 39ms, total 40ms 
: GC_FOR_ALLOC freed <1K, 2% free 17504K/17728K, paused 41ms, total 41ms 
: GC_FOR_ALLOC freed <1K, 2% free 19004K/19232K, paused 42ms, total 43ms 
: GC_FOR_ALLOC freed <1K, 2% free 20505K/20736K, paused 58ms, total 58ms 
: GC_FOR_ALLOC freed <1K, 2% free 22006K/22240K, paused 137ms, total 138ms 
: GC_FOR_ALLOC freed <1K, 1% free 23506K/23744K, paused 47ms, total 47ms 
: GC_FOR_ALLOC freed <1K, 1% free 25007K/25248K, paused 48ms, total 48ms 
: GC_FOR_ALLOC freed <1K, 1% free 26507K/26752K, paused 50ms, total 51ms 
: GC_FOR_ALLOC freed <1K, 1% free 28008K/28256K, paused 56ms, total 57ms 
: GC_FOR_ALLOC freed <1K, 1% free 29509K/29760K, paused 58ms, total 58ms 
: Clamp target GC heap from 32.398MB to 32.000MB 
: GC_FOR_ALLOC freed <1K, 1% free 31009K/31264K, paused 57ms, total 58ms 
: Clamp target GC heap from 33.863MB to 32.000MB 
: GC_FOR_ALLOC freed <1K, 1% free 32510K/32768K, paused 56ms, total 56ms 
: Forcing collection of SoftReferences for 1536016-byte allocation 
:Clamp target GC heap from 33.855MB to 32.000MB 
: GC_BEFORE_OOM freed 9K, 1% free 32500K/32768K, paused 87ms, total 88ms 
: Out of memory on a 1536016-byte allocation. 
: "main" prio=5 tid=1 RUNNABLE 
: | group="main" sCount=0 dsCount=0 obj=0x40a729a0 self=0x2a00bba8 
: | sysTid=1813 nice=0 sched=0/0 cgrp=apps handle=1073849308 
: | state=R schedstat=(3245039730 1438341923 288) utm=275 stm=49 core=0 
: at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502) 
: at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355) 
: at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785) 
: at android.content.res.Resources.loadDrawable(Resources.java:1965) 
: at android.content.res.Resources.getDrawable(Resources.java:660) 
: at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282) 
: at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885) 
: at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822) 
: at android.content.res.Resources.loadDrawable(Resources.java:1950) 
: at android.content.res.Resources.getDrawable(Resources.java:660) 
: at android.view.View.setBackgroundResource(View.java:14468) 
: at com.example.appthree.MainActivity.onCreate(MainActivity.java:15) 
: at android.app.Activity.performCreate(Activity.java:5104) 
: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
: at android.app.ActivityThread.access$600(ActivityThread.java:141) 
: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
: at android.os.Handler.dispatchMessage(Handler.java:99) 
: at android.os.Looper.loop(Looper.java:137) 
: at android.app.ActivityThread.main(ActivityThread.java:5041) 
: at java.lang.reflect.Method.invokeNative(Native Method) 
: at java.lang.reflect.Method.invoke(Method.java:511) 
: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
: at dalvik.system.NativeStart.main(Native Method) 
: --- decoder->decode returned false 
: Shutting down VM 
: threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
: FATAL EXCEPTION: main 
: java.lang.OutOfMemoryError 
: at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
: at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502) 
: at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355) 
: at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785) 
: at android.content.res.Resources.loadDrawable(Resources.java:1965) 
: at android.content.res.Resources.getDrawable(Resources.java:660) 
: at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282) 
: at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885) 
: at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822) 
: at android.content.res.Resources.loadDrawable(Resources.java:1950) 
: at android.content.res.Resources.getDrawable(Resources.java:660) 
: at android.view.View.setBackgroundResource(View.java:14468) 
: at com.example.appthree.MainActivity.onCreate(MainActivity.java:15) 
: at android.app.Activity.performCreate(Activity.java:5104) 
: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
: at android.app.ActivityThread.access$600(ActivityThread.java:141) 
: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
: at android.os.Handler.dispatchMessage(Handler.java:99) 
: at android.os.Looper.loop(Looper.java:137) 
: at android.app.ActivityThread.main(ActivityThread.java:5041) 
: at java.lang.reflect.Method.invokeNative(Native Method) 
: at java.lang.reflect.Method.invoke(Method.java:511) 
: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
: at dalvik.system.NativeStart.main(Native Method) 
: GC_CONCURRENT freed 30038K, 93% free 2539K/32768K, paused 73ms+97ms, total 472ms 
+0

thử hướng dẫn này http://droid-blog.net/2011/10/14/tutorial-how-to-use-animated-gifs-in-android- part-1/nó có một cách khác nhau để hiển thị một gif động .. có các liên kết cho các phần khác của hướng dẫn ở dưới cùng của trang .. – Elior

+0

Tôi đề nghị bạn hãy xem cách này để hiển thị một gif động http: // droid-blog.net/2011/10/17/tutorial-how-to-play-hoạt hình-gifs-in-android-part-3/ – Elior

+0

Android không hỗ trợ hoạt ảnh Gifs – Martin

Trả lời

21

Như Martin nói, Android không hỗ trợ GIF. Để giải quyết sự cố này, Android cung cấp Animation List/AnimationDrawable. Bạn sẽ cần phải chuyển đổi GIF thành các khung riêng lẻ [.png files]. Tôi sử dụng GIMP cho việc chuyển đổi

Original GIF image

GIF này có thể được chia nhỏ thành các khung hình của nó:

GIF frames in GIMP

Lưu chúng như frame01.png, frame02.png, và như vậy, và tạo tệp XML danh sách hoạt ảnh, ví dụ: progress_animation.xml

<animation-list android:id="@+id/selected" android:oneshot="false"> 
<item android:drawable="@drawable/frame01" android:duration="50" /> 
<item android:drawable="@drawable/frame02" android:duration="50" /> 
<item android:drawable="@drawable/frame03" android:duration="50" /> 
.... 
<item android:drawable="@drawable/frameN" android:duration="50" /> 

Để bắt đầu hoạt hình, bạn cần phải cast hình ảnh đến một AnimationDrawable, và gọi start() vào nó

AnimationDrawable progressAnimation = (AnimationDrawable) yourImageView.getBackground(); 
progressAnimation.start(); 
+0

Cảm ơn bạn vì các tài nguyên đã giúp tôi rất nhiều. Tôi đã đăng một bản cập nhật của mã của tôi tuy nhiên như ứng dụng của tôi vẫn sẽ không chạy. –

+1

Bạn có đăng bài theo dõi ngăn xếp LogCat hoàn chỉnh hay ít nhất là đề cập đến dòng mã gây ra UnhandledEventLoopException không? – WeNeigh

+0

Tôi đã thêm LogCat của Bộ lọc phiên. Tôi sẽ đăng tất cả các tin nhắn (không có bộ lọc) nhưng có những trang cơ bản của chúng. –

1

Các hình ảnh động gif được hỗ trợ trong Android khi GIF hoạt hình được chơi như Movie.

Take a look.

0

đưa ảnh gif của bạn trong ứng dụng \ src \ chính \ tài sản

làm cho một lớp học như

public class MyGifView extends View { 
Movie movie; 
InputStream is; 
long startTime; 

public MyGifView(Context context) { 
    super(context); 
    try { 
     is = getResources().getAssets().open("anim.gif"); 
     movie = Movie.decodeStream(is); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawColor(Color.WHITE); 
    super.onDraw(canvas); 
    long now = System.currentTimeMillis(); 
    if (startTime == 0) // first time 
     startTime = now; 
    int relTime = (int) ((now - startTime) % movie.duration()); 
    movie.setTime(relTime); 
    float scalefactorx = (float) this.getWidth()/(float) movie.width(); 
    float scalefactory = (float) this.getHeight()/(float) movie.height(); 
    canvas.scale(scalefactorx,1); 
    movie.draw(canvas, scalefactorx, scalefactory); 
    this.invalidate(); 
} 

}

thêm một bố trí ở đâu đó trong activity_layout xml của bạn (backgroundFrameLayout). Cách sử dụng trong hoạt động của bạn onCreate():

FrameLayout frameLayout = findViewById(R.id.backgroundFrameLayout); 
frameLayout.addView(new MyGifView(this)); 
Các vấn đề liên quan