Android 动画(二)

以下演示如何通过代码创建动画

1》activity_main.xml布局文件的代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!--
         <ImageView
        android:id="@+id/s7_iv"
        android:layout_width="40dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:background="#ff0000"
        android:src="@drawable/ic_launcher"
        android:visibility="gone" />
    -->

    <Button
        android:id="@+id/start_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />

</RelativeLayout>

2》MainActivity.java的代码如下(内有详细注示):

package com.demo.animationtest;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	// 声明控件
	private ImageView s7_iv;
	private Button start_btn;
	// 用代码控制动画
	private TranslateAnimation translateAnimation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		init();

	}

	// 初始化操作
	@SuppressLint("NewApi")
	private void init() {
		// 得到控件
		start_btn = (Button) findViewById(R.id.start_btn);
		// 创建控件
		s7_iv = new ImageView(this);
		s7_iv.setBackgroundColor(getResources()
				.getColor(R.color.background_red));
		LayoutParams layoutParams = new LayoutParams(40, 200);
		s7_iv.setVisibility(View.GONE);
		// 将创建的控件添加到Activity中
		this.addContentView(s7_iv, layoutParams);

		// 创建动画效果
		translateAnimation = new TranslateAnimation(0.0f, 0.0f, -200.0f, 800);
		// 设置动画持续时间
		translateAnimation.setDuration(5000);

		// 设置动画重复模式与重复次数
		translateAnimation.setRepeatCount(Animation.INFINITE);
		translateAnimation.setRepeatMode(Animation.RESTART);

		// 注册动画监听器
		translateAnimation.setAnimationListener(myAnimationListener);

		// 注册事件
		start_btn.setOnClickListener(myButtOnClickListener);

	}

	OnClickListener myButtOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.i("CXC", "myButtonClickListener--click");
			switch (v.getId()) {

			case R.id.start_btn:
				// 启动动画
				s7_iv.startAnimation(translateAnimation);
				break;

			default:
				break;
			}

		}
	};
	// 动画监听器
	AnimationListener myAnimationListener = new AnimationListener() {

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			Log.i("CXC", "---onAnimationStart");
			s7_iv.setVisibility(View.VISIBLE);

		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			Log.i("CXC", "---onAnimationRepeat");

		}

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			Log.i("CXC", "---onAnimationEnd");
			s7_iv.setVisibility(View.GONE);

		}
	};
}


你可能感兴趣的:(动画)