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

发布于 2025-01-08 03:03:53 字数 445 浏览 4 评论 0原文

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

我已经测试了这个示例教程并观看这个视频。我的问题是,大多数教程都是为了将​​精灵动画化到画布中,您将在其中追踪精灵的位置。

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

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

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. Also this animation is affected by other factor so the android Animation class is out of the question.

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?

Edited:
The custom view will act as a animated-gif and will toggle another set of frames whenever an event activates it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸵鸟症 2025-01-15 03:03:53

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

you can create animation XML and apply that animation from program to your ImageView check following example

you can start animation for your customized view just call 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>

your 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

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);
}
}
静水深流 2025-01-15 03:03:53

如果您想要“逐帧”动画,就像动画 gif 一样“运行”,您可以按照以下步骤操作:
drawable-animation 官方教程

引用教程:

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

If you want "frame by frame" animation that just "runs" like an animated gif, you can follow:
drawable-animation official tutorial.

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文