Button animations in android

不问归期 提交于 2019-12-03 21:16:26
  1. Make images for your animation and keep in a folder in png format.
  2. Animate those images with code below.
<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>

Then

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

Create list of images in drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loading" 
android:oneshot="false" >
<item android:drawable="@drawable/preloader_01" android:duration="50" />
<item android:drawable="@drawable/preloader_02" android:duration="50" />
<item android:drawable="@drawable/preloader_03" android:duration="50" />
<item android:drawable="@drawable/preloader_04" android:duration="50" />

Use ImageView img instead of button and set the list as background in xml, then in code use AnimationDrawable to start the animation

    AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
     // Start the animation (looped playback by default).
    frameAnimation.start();`

You can animate Alpha, location and just about any property of a button using ObjectAnimators and ValueAnimators.

If you want custom animations such as the ice effect, you'll have to make these yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!