2012-01-04 31 views
15

Tôi gặp sự cố nhỏ khi tải trình quay phim hoạt ảnh để làm việc cho trang splash. Không có gì xuất hiện khi tôi cố chạy đoạn mã sau. Bất kỳ đề xuất? Dường như một vài người có vấn đề với điều này trên google nhưng tôi không hiểu tại sao tôi không làm việc. Cảm ơn!sử dụng danh sách hoạt ảnh android

animationloader.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:oneshot="false"> 
<item android:drawable="@drawable/loadingspinner1" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner2" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner3" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner4" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner5" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner6" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner7" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner8" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner9" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner01" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner11" android:duration="200" /> 
<item android:drawable="@drawable/loadingspinner12" android:duration="200" /> 
</animation-list> 

SplashScreen.java

package com.secure.inmatecanteen; 

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

public class SplashScreen extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splashscreen); 

    //Beginning the loading animation as we attempt to verify registration with SIP 
    ImageView ivLoader = (ImageView) findViewById(R.id.IVloadinganimation); 
    ivLoader.setBackgroundResource(R.anim.animationloader); 


    AnimationDrawable frameAnimation = (AnimationDrawable) ivLoader.getBackground(); 
    frameAnimation.start(); 
} 
} 

splashscreen.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" 
android:background="@android:color/white" > 

<ImageView 
android:id="@+id/iclogo" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:src="@drawable/iclogo" 
android:adjustViewBounds="true" 
/> 

<ImageView 
android:id="@+id/IVloadinganimation" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:adjustViewBounds="true" 
/> 

    </LinearLayout> 
+0

có u bao gồm hoạt động này trên tệp manifest.xml e? – sampathpremarathna

+0

Có, tôi có. Hoạt động hiển thị mà không có hoạt ảnh – tier1

Trả lời

22

Giải Quyết vấn đề của riêng tôi, Bạn không thể bắt đầu hình ảnh động trong onCreate. Nó phải ở trong một người nghe onclick hoặc bên trong một runnable.

+4

nhưng công việc của tôi trên oncreate, nhưng không phải trên onclick. –

+3

Theo http://developer.android.com/guide/topics/graphics/drawable-animation.html bạn không bao giờ nên gọi nó tại onCreate(). – rude

4

Bạn có thể phát/bắt đầu animation từ phương thức onWindowFocusChanged(boolean hasFocus).

15

Tôi nghĩ là lựa chọn thanh lịch và đa năng nhất là mở rộng từ lớp ImageView:

public class Loader extends ImageView { 

    public Loader(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public Loader(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public Loader(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     setBackgroundResource(R.drawable.loader); 
     final AnimationDrawable frameAnimation = (AnimationDrawable) getBackground(); 
     post(new Runnable(){ 
      public void run(){ 
       frameAnimation.start(); 
      } 
     }); 
    } 
} 

Các loader.xml nằm trong thư mục drawable:

<?xml version="1.0" encoding="utf-8"?> 
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:drawable="@drawable/loader_1" android:duration="50" /> 
    <item android:drawable="@drawable/loader_2" android:duration="50" /> 
    <item android:drawable="@drawable/loader_3" android:duration="50" /> 
    <item android:drawable="@drawable/loader_4" android:duration="50" /> 
    ..... 
</animation-list> 

Bây giờ đưa vào xem một cái gì đó của bạn đơn giản như sau:

<com.yourpackage.Loader 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+0

có 3 thiết bị, chỉ có thể làm việc này trên Nexus 4 chạy Android 4.2.2, không hoạt động trên các thiết bị tổ ong hoặc bánh gừng. Có lẽ sẽ phải gắn bó với viewflippers – CQM

+0

Tôi đã cập nhật phương pháp init thêm khả năng tương thích với tổ ong và bánh gừng. Kiểm tra nó. –

+0

hoạt động như một sự quyến rũ, cảm ơn! – CQM

2

Trong bộ xử lý này hoạt ảnh không được đính kèm đầy đủ vào t cửa sổ của anh ấy, vì vậy không thể bắt đầu hoạt ảnh; thay vào đó, điều này thường được thực hiện do hành động của người dùng (chẳng hạn như nhấn nút) hoặc trong số onWindowFocusChangedhandler.

tham khảo: chuyên nghiệp android phát triển 4 ứng dụng

0

Đừng đặt tài nguyên hình ảnh trong mã xml.

XML của tôi là:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:gravity="center" 
android:keepScreenOn="true" 
android:id="@+id/splashLayout" 
android:background="@color/colorPrimary" 
android:layout_height="match_parent"> 


<ImageView 
    android:layout_width="230dp" 
    android:layout_height="230dp" 
    android:id="@+id/iv_splash" 
    android:layout_marginTop="-80dp" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" /> 


</RelativeLayout> 

Trong Hoạt động tôi làm

public class SplashActivity extends Activity { 

ImageView iv_splash; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash); 

    iv_splash=(ImageView)findViewById(R.id.iv_splash); 
    iv_splash.setBackgroundResource(R.drawable.splash); 
    final AnimationDrawable progressAnimation =(AnimationDrawable)iv_splash.getBackground(); 
    progressAnimation.start(); 
    } 
} 

tập tin drawable

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:oneshot="false" > 

<item android:drawable="@drawable/logo" android:duration="400"/> 
<item android:drawable="@drawable/logo1" android:duration="400"/> 

</animation-list> 

Nó làm việc tốt :)

Các vấn đề liên quan