帧动画

motionleft.xml




    
    


motionright.xml




    
    


translateleft.xml



	
	 

translateright.xml



	
	 

fragment_main。xml


    


Mianactivity.java

package com.xwy.yezhu;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
	private AnimationDrawable anim;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
		
		final ImageView iv=(ImageView)findViewById(R.id.imageView1);	//获取要应用动画效果的ImageView
		final Animation translateright=AnimationUtils.loadAnimation(this, R.anim.translateright);	//获取“向右奔跑”的动画资源
		final Animation translateleft=AnimationUtils.loadAnimation(this, R.anim.translateleft);	//获取“向左奔跑”的动画资源
		anim=(AnimationDrawable)iv.getBackground();//获取应用的帧动画
		LinearLayout ll=(LinearLayout)findViewById(R.id.linearLayout1);	//获取线性布局管理器
		Toast.makeText(this,"触摸屏幕开始播放...", Toast.LENGTH_SHORT).show();	//显示一个消息提示框
		ll.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				anim.start();	//开始播放帧动画
				iv.startAnimation(translateright);	//播放“向右奔跑”的动画
				return false;
			}
		});
		translateright.setAnimationListener(new AnimationListener() {
			
			public void onAnimationStart(Animation animation) {}
			
			public void onAnimationRepeat(Animation animation) {}
			
			public void onAnimationEnd(Animation animation) {
				iv.setBackgroundResource(R.anim.motionleft);	//重新设置ImageView应用的帧动画
				iv.startAnimation(translateleft);		//播放“向左奔跑”的动画	
				anim=(AnimationDrawable)iv.getBackground();//获取应用的帧动画
				anim.start();	//开始播放帧动画
			}
		});
		translateleft.setAnimationListener(new AnimationListener() {
			
			public void onAnimationStart(Animation animation) {}
			
			public void onAnimationRepeat(Animation animation) {}
			
			public void onAnimationEnd(Animation animation) {
				iv.setBackgroundResource(R.anim.motionright);	//重新设置ImageView应用的帧动画
				iv.startAnimation(translateright);	//播放“向右奔跑”的动画		
				anim=(AnimationDrawable)iv.getBackground();//获取应用的帧动画
				anim.start();	//开始播放帧动画
			}
		});

	}
		
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}


你可能感兴趣的:(Android)