Android 如何以编程方式设置自定义视图(ImageView)的动画?

Android 如何以编程方式设置自定义视图(ImageView)的动画?,android,animation,view,animated-gif,Android,Animation,View,Animated Gif,我想以编程方式设置自定义视图的动画。我希望它只在xml文件中声明的位置上设置动画,并且我不希望它弄乱布局。此外,该动画还受到其他因素的影响,因此android动画类是不可能的 我已经测试了这个样品并观看了这个。我的问题是,大多数教程都是为了在画布上设置精灵的动画,在画布上可以跟踪精灵的位置 如何在不影响布局和不使用Android动画类的情况下设置自定义视图的动画 编辑: 自定义视图将充当动画gif,并在事件激活时切换另一组帧。如果希望“逐帧”动画像动画gif一样“运行”,可以执行以下操作: 引

我想以编程方式设置自定义视图的动画。我希望它只在xml文件中声明的位置上设置动画,并且我不希望它弄乱布局。此外,该动画还受到其他因素的影响,因此android动画类是不可能的

我已经测试了这个样品并观看了这个。我的问题是,大多数教程都是为了在画布上设置精灵的动画,在画布上可以跟踪精灵的位置

如何在不影响布局和不使用Android动画类的情况下设置自定义视图的动画

编辑: 自定义视图将充当动画gif,并在事件激活时切换另一组帧。

如果希望“逐帧”动画像动画gif一样“运行”,可以执行以下操作:

引用教程中的内容:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

您可以创建动画XML,并将该动画从程序应用到ImageView检查,如下示例

您可以为自定义视图启动动画,只需调用
viewobject.startAnimation(animationobject)

animation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:shareInterpolator="false">
<scale
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="1.0" 
    android:toXScale="1.4" 
    android:fromYScale="1.0" 
    android:toYScale="0.6" 
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillAfter="false"
    android:duration="700" />
<set
    android:interpolator="@android:anim/accelerate_interpolator"
    android:startOffset="700">
    <scale
        android:fromXScale="1.4" 
        android:toXScale="0.0"
        android:fromYScale="0.6"
        android:toYScale="0.0" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="400" />
    <rotate
        android:fromDegrees="0" 
        android:toDegrees="-45"
        android:toYScale="0.0" 
        android:pivotX="50%" 
        android:pivotY="50%"
        android:duration="400" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

</LinearLayout>

你想做什么?您想如何设置动画?动画就像一个加载条,它根据时间运行,但当事件发生时,另一组动画将在该视图上播放。动画必须只影响自定义视图。如果调用AnimationDrawable,则可以轻松切换到另一组帧:rocketImage.setBackgroundResource(R.drawable.);当你需要的时候。AnimationDrawable类是我需要的。谢谢
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
/>
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

</LinearLayout>
public class myActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView image = (ImageView) findViewById(R.id.ImageView01);

    //create animation class object
    Animation hyperspaceJump = 
        AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
    //start animation for image
    image.startAnimation(hyperspaceJump);
}
}