【问题标题】:How can I animate a custom view (ImageView) programmatically?如何以编程方式为自定义视图 (ImageView) 设置动画?
【发布时间】:2012-03-12 14:09:44
【问题描述】:

我想以编程方式为自定义视图设置动画。我希望它仅在 xml 文件中声明的位置进行动画处理,并且我不希望它弄乱布局。这个动画也受到其他因素的影响,所以 android Animation 类是不可能的。

我已经测试了这个样本tutorial 并观看了这个videos。我的问题是大多数教程都是用于将精灵动画到画布中,您将在其中跟踪精灵的位置。

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

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

【问题讨论】:

  • 你想做什么?你想怎么做动画?
  • 动画就像一个加载条,它会根据时间运行,但是当一个事件发生时,另一组动画将在该视图上播放。动画必须只影响自定义视图。
  • 使用 AnimationDrawable,您可以轻松切换到另一组帧,如果您调用:rocketImage.setBackgroundResource(R.drawable.>);当你需要的时候。

标签: android animation view animated-gif


【解决方案1】:

您可以创建动画 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>

你的 ma​​in.xml

<?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>

然后你的活动类myActivity.java

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);
}
}

【讨论】:

    【解决方案2】:

    如果您想要像动画 gif 一样“运行”的“逐帧”动画,您可以按照以下步骤操作: drawable-animation official tutorial.

    引用教程:

    <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>
    

    在你的代码中

    AnimationDrawable rocketAnimation;
    
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    
      ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
      rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
      rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
    }
    
    public boolean onTouchEvent(MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
        rocketAnimation.start();
        return true;
      }
      return super.onTouchEvent(event);
    }
    

    【讨论】:

    • AnimationDrawable 类是我需要的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多