0

这个问题已经被问过了,但我没有找到任何合适的答案,所以我发布了这个问题。我有一个ProgressBar动画,它有多个图像并像进度条一样加载这些图像。我可以在 4.0 及更高版本中成功运行它,但它在 2.3 中不起作用。如何使其在较低版本中运行?

我的动画可绘制 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    android:pivotX="50%"
    android:pivotY="50%" >
    <item android:drawable="@drawable/p1_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p2_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p3_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p4_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p5_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p6_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p7_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p8_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p9_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p10_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p11_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p12_wheel" android:duration="200"/>
</animation-list>

我的 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/imageView1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"/>


</RelativeLayout>

我的 Java 文件

package com.example.progressbarsample;

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

public class MainActivity extends Activity {
    ImageView progressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressbar = (ImageView) findViewById(R.id.imageView1);
        progressbar.setBackgroundResource(R.drawable.progressbar);
        final AnimationDrawable frame = (AnimationDrawable) progressbar.getBackground();
        progressbar.post(new Runnable() {

            @Override
            public void run() {
                frame.start();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
4

3 回答 3

1

最后我找到了解决方案。

 progressbar = (ImageView) findViewById(R.id.imageView1);
 progressbar.setBackgroundResource(R.drawable.progressbar);
 final AnimationDrawable frame = (AnimationDrawable)getResources.getDrawable(R.drawable.progressbar);  \\progressbar is the anim.xml file
 progressbar.setBackgroundDrawable(frame);
 progressbar.post(new Runnable() {

            @Override
            public void run() {
                frame.start();
            }
        });
    }
于 2014-01-30T11:01:44.557 回答
1

在视图可见后尝试制作动画,如下所示:

@Override
public void onWindowFocusChanged (bool hasFocus)
{
    super.onWindowFocusChanged (hasFocus);
    //get the animation from your view
    AnimationDrawable explosionAnimation = (AnimationDrawable) mImageView.getBackground();
    //set it visible and start animation
    explosionAnimation.setVisible (true, true);
    explosionAnimation.start ();
}

Activity 中的onWindowFocusChanged方法在视图已经可见并获得焦点后被调用。因此,您将能够为您的图像设置动画。我试过不使用setVisible但只显示静态图像。当我从 onResume、onStart 等启动动画时也会发生同样的情况。上面的代码对我来说很好,包括 Android 2.3。我目前正在使用单声道(使用 Xamarin),但我希望它在 Java 中对你有用。

于 2014-05-07T18:14:51.900 回答
0

我使用过 Drawable Animations,它适用于我的代码:

mImageView.setBackgroundResource(R.drawable.anim);
AnimationDrawable explosionAnimation = (AnimationDrawable) mImageView.getBackground();
explosionAnimation.start();

但我认为这是因为帖子,在不使用progressbar.post的情况下尝试代码

于 2014-01-29T09:00:24.140 回答