2010-09-21 53 views
28

Tôi muốn tạo hình ảnh tiến trình xoay và tự hỏi cách tốt nhất để tiếp tục là gì. Tôi có thể làm cho nó hoạt động với một danh sách hoạt hình với ví dụ 12 hình ảnh thay đổi mỗi 100ms. Đây hoạt động tốt, nhưng nó khá tẻ nhạt để tạo ra 12 hình ảnh hoặc cho mỗi kích thước và độ phân giải:Xoay ảnh. Danh sách hoạt ảnh hoặc xoay động? (Android)

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> 
<item android:drawable="@drawable/ic_loading_grey_on_black_01" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_02" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_03" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_04" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_05" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_06" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_07" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_08" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_09" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_10" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_11" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_12" android:duration="100" /> 

Tôi cho rằng đó là một giải pháp dễ dàng hơn là sử dụng một hình ảnh mỗi độ phân giải, nhưng thay vì xoay nó cho mỗi khung. Trong tài nguyên nền tảng (android-sdk-windows/platforms ...) Tôi đã tìm thấy thứ được gọi là xoay động trong tệp có thể vẽ/search_spinner.xml, nhưng nếu tôi sao chép mã, hãy nhận lỗi trình biên dịch về android: framesCount và android: frameDuration (Google API 2.2 trong Eclipse):

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:drawable="@drawable/spinner_black_20" 
android:pivotX="50%" 
android:pivotY="50%" 
android:framesCount="12" 
android:frameDuration="100" /> 

tôi cũng đã cố gắng sử dụng một hình ảnh động xoay lặp lại (sử dụng trong thư mục tài nguyên anim), nhưng tôi thực sự thích giao diện của phiên bản danh sách phim hoạt hình.

Cách được đề xuất để giải quyết vấn đề này là gì?

Trả lời

13

Bạn phải tạo một tập tin xml drawable như dưới đây:

Code:

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" 
android:toDegrees="360" android:drawable="@drawable/imagefile_to_rotate" /> 
+2

Làm thế nào nó không quay? –

+0

nó không hoạt động trên một số thiết bị. xin vui lòng sử dụng rotage thay vì – vuhung3990

2

xem các ví dụ ở đây http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/index.html

cụ thể: Progress Bar

  1. Incremental Thể hiện các chỉ số tiến độ quay lớn và nhỏ có thể tăng hoặc giảm theo đơn vị.
  2. Mịn Thể hiện các chỉ báo tiến trình quay liên tục lớn và nhỏ được sử dụng để biểu thị thông báo "bận" chung.
  3. Hộp thoại Trình bày ProgressDialog, hộp thoại bật lên lưu trữ thanh tiến trình. Ví dụ này thể hiện cả các chỉ số tiến trình xác định và không xác định.
  4. Trong thanh tiêu đề Trình diễn màn hình Hoạt động với chỉ báo tiến trình được tải bằng cách đặt tính năng chỉ báo tiến trình của WindowPolicy.
0

Giải pháp của SACPK chắc chắn hoạt động. Một giải pháp khác có thể là sử dụng <animated-rotate> giống như trong câu hỏi và loại bỏ các thuộc tính android:framesCount="12" android:frameDuration="100" cho các trình biên dịch đó phàn nàn. Nó vẫn hoạt động ngay cả với hình ảnh 8 khung của tôi.

Tuy nhiên, tôi havn't tìm ra cách để kiểm soát tốc độ của hoạt hình :(.

55

Rotate drawable đề xuất bởi Praveen sẽ không cung cấp cho bạn kiểm soát số khung. Giả sử bạn muốn thực hiện một bộ nạp tùy chỉnh trong đó bao gồm từ 8 phần:

gif_icon

Sử dụng animation-list cách tiếp cận, bạn cần phải tạo ra 8 khung quay bằng 45*frameNumber độ bằng tay.Ngoài ra, bạn có thể sử dụng khung 1 và thiết lập hình ảnh động xoay với nó:

progress_icon

file res/anim/progress_anim.xml:

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:repeatCount="infinite" /> 

file MainActivity.java

Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.progress_anim); 
a.setDuration(1000); 
imageView.startAnimation(a); 

này sẽ cung cấp cho bạn hình ảnh động trơn tru thay vì 8 bước. Để khắc phục điều này chúng ta cần phải thực hiện tùy chỉnh xen vào:

a.setInterpolator(new Interpolator() { 
    private final int frameCount = 8; 

    @Override 
    public float getInterpolation(float input) { 
     return (float)Math.floor(input*frameCount)/frameCount; 
    } 
}); 

Ngoài ra bạn có thể tạo một widget tùy chỉnh:

file res/values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ProgressView"> 
     <attr name="frameCount" format="integer"/> 
     <attr name="duration" format="integer" /> 
    </declare-styleable> 
</resources> 

file ProgressView.java:

public class ProgressView extends ImageView { 

    public ProgressView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setAnimation(attrs); 
    } 

    public ProgressView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setAnimation(attrs); 
    } 

    public ProgressView(Context context) { 
     super(context); 
    } 

    private void setAnimation(AttributeSet attrs) { 
     TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ProgressView); 
     int frameCount = a.getInt(R.styleable.ProgressView_frameCount, 12); 
     int duration = a.getInt(R.styleable.ProgressView_duration, 1000); 
     a.recycle(); 

     setAnimation(frameCount, duration); 
    } 

    public void setAnimation(final int frameCount, final int duration) { 
     Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.progress_anim); 
     a.setDuration(duration); 
     a.setInterpolator(new Interpolator() { 

      @Override 
      public float getInterpolation(float input) { 
       return (float)Math.floor(input*frameCount)/frameCount; 
      } 
     }); 
     startAnimation(a); 
    } 
} 

file activity_main.xml:

<com.example.widget.ProgressView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_progress" 
    app:frameCount="8" 
    app:duration="1000"/> 

file res/anim/progress_anim.xml: liệt kê ở trên

+0

tuyệt vời, cảm ơn bạn rất nhiều –

+3

"cảm ơn!", "tôi quá!", – Cephron

+0

Tôi không thể ẩn tiến trình bằng cách sử dụng 'android: visibility =" invisible "' bất kỳ lý do gì? – Prateek

6

Tôi tìm thấy câu trả lời vokilam cho bạn trở thành tốt nhất để tạo ra một bước/so le hình ảnh động đẹp. Tôi đã đi cho đề nghị cuối cùng của ông và tạo ra một widget tùy chỉnh, vấn đề duy nhất tôi gặp phải là khả năng hiển thị thiết lập sẽ không hoạt động bởi vì nó đã được làm ... Xem thêm mà tôi đã đổi tên thành StaggeredProgress.java) như thế này:

public class StaggeredProgress extends ImageView { 

private Animation staggered; 

public StaggeredProgress(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    setAnimation(attrs); 
} 

public StaggeredProgress(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setAnimation(attrs); 
} 

public StaggeredProgress(Context context) { 
    super(context); 
} 

private void setAnimation(AttributeSet attrs) { 
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.StaggeredProgress); 
    int frameCount = a.getInt(R.styleable.StaggeredProgress_frameCount, 12); 
    int duration = a.getInt(R.styleable.StaggeredProgress_duration, 1000); 
    a.recycle(); 

    setAnimation(frameCount, duration); 
} 

public void setAnimation(final int frameCount, final int duration) { 
    Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.progress_anim); 
    a.setDuration(duration); 
    a.setInterpolator(new Interpolator() { 

     @Override 
     public float getInterpolation(float input) { 
      return (float)Math.floor(input*frameCount)/frameCount; 
     } 
    }); 
    staggered = a; 
    //startAnimation(a); 
} 

@Override 
public void setVisibility(int visibility) { 
    super.setVisibility(visibility); 
    if(visibility == View.VISIBLE) 
     startAnimation(staggered); 
    else 
     clearAnimation(); 

} 


} 

Bằng cách này, chế độ hiển thị của chế độ xem bắt đầu và dừng hoạt ảnh theo yêu cầu ... Rất cám ơn một lần nữa đến vokilam!