Android animation detail series [2] -- flying butterfly

Source: Internet
Author: User

This article uses frame-by-frame animation and complementary animation to implement a small example. First, let's take a look at the complementary animation in Android.

In Android, Animation is used to represent an abstract Animation class. This class includes the following sub-classes:

AlphaAnimation: transparent animation change.

ScaleAnimation: resize animation.

TranslateAnimation: displacement change animation.

RotateAnimation: rotation animation.

Let's use the displacement animation and frame-by-frame animation to implement this small example. Let's first look at the running effect:

Frame-by-frame animation file for butterfly wings:

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Define the animation Loop play --> <animation-list xmlns: android = "http://schemas.android.com/apk/res/android" android: oneshot = "false"> <item android: drawable = "@ drawable/butterfly_f01" android: duration = "120"/> <item android: drawable = "@ drawable/butterfly_f02" android: duration = "120"/> <item android: drawable = "@ drawable/butterfly_f03" android: duration = "120" type = "codeph" text = "/codeph"/> <item android: drawable = "@ drawable/butterfly_f04" android: duration = "120"/> <item android: drawable = "@ drawable/butterfly_f05" android: duration = "120"/> <item android: drawable = "@ drawable/butterfly_f06" android: duration = "120"/> </animation-list>
Interface layout file:

<?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"android:background="@drawable/background"><ImageViewandroid:id="@+id/butterfly"android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/butteryfly"/></LinearLayout>
Specific logic and displacement Animation:


package com.example.butteryfly;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.os.Handler;import android.view.Display;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class MainActivity extends Activity {private float curX = 0;private float curY = 30;private float nextX = 0;private float nextY = 0;private int windowW = 0;private int windowH = 0;private ImageView imageView;Handler handler = new Handler(){public void handleMessage(android.os.Message msg) {if(msg.what == 0x123){if(nextX > windowW || nextY > windowH){curX = nextX = 0;}else{nextX += 8;}nextY = curY + (float) (Math.random() * 20 - 10);TranslateAnimation anim = new TranslateAnimation(curX, nextX, curY, nextY);curX = nextX;curY = nextY;anim.setDuration(200);imageView.startAnimation(anim);}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageView = (ImageView) findViewById(R.id.butterfly);Display display = getWindowManager().getDefaultDisplay(); windowW = display.getWidth();windowH = display.getHeight();final AnimationDrawable butterfly = (AnimationDrawable) imageView.getBackground();imageView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {butterfly.start();new Timer().schedule(new TimerTask() {@Overridepublic void run() {handler.sendEmptyMessage(0x123);}}, 0, 200);}});}}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.