简体   繁体   English

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

[英]How can I animate a custom view (ImageView) programmatically?

I want to animate a custom view programmatically. 我想以编程方式为自定义视图设置动画。 I want it to only animate in the position where it is declare in the xml file, and I don't want it to mess the layout. 我希望它只在xml文件中声明它的位置动画,我不希望它弄乱布局。 Also this animation is affected by other factor so the android Animation class is out of the question. 此动画也受其他因素的影响,因此android Animation类是不可能的。

I have test this sample tutorial and watch this videos . 我测试了这个示例教程并观看了这些视频 My problem is that most tutorial is for animating a sprite into a canvas where you will trace your sprite's location. 我的问题是,大多数教程都是为了将​​精灵设置为画布动画,您将在其中跟踪精灵的位置。

How can you animate the custom view without affecting layout and without using the Android Animation class? 如何在不影响布局且不使用Android Animation类的情况下为自定义视图设置动画?

Edited: The custom view will act as a animated-gif and will toggle another set of frames whenever an event activates it. 已编辑:自定义视图将充当动画gif,并在事件激活时切换另一组帧。

you can create animation XML and apply that animation from program to your ImageView check following example 您可以创建动画XML并将该动画从程序应用到ImageView检查以下示例

you can start animation for your customized view just call viewobject.startAnimation(animationobject); 您可以为自定义视图启动动画,只需调用viewobject.startAnimation(animationobject);

animation.xml 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>

your main.xml 你的main.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>

then your activity class myActivity.java 然后你的活动类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);
}
}

If you want "frame by frame" animation that just "runs" like an animated gif, you can follow: drawable-animation official tutorial . 如果你想要像动画gif一样“运行”的“逐帧”动画,你可以遵循: drawable-animation官方教程

Quoting from the 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>

and in your code 并在你的代码中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM