让一个ImageView在屏幕中来回运动

155 篇文章 0 订阅
79 篇文章 0 订阅
已不再推荐补间动画,请使用属性动画;
http://blog.csdn.net/guolin_blog/article/details/43536355
http://blog.csdn.net/guolin_blog/article/details/43816093



onCreate()中:

iv=(ImageView)this.findViewById(R.id.iv);
	        iv.setTag("toRight");
	        iv.setOnClickListener(listener);
	        ani_0 = new TranslateAnimation(
		            Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,
		            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
			ani_1= new TranslateAnimation(
		            Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
		            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
			ani_0.setInterpolator(new AccelerateDecelerateInterpolator());   
			ani_0.setDuration(1000);
			ani_0.setFillEnabled(true);
			ani_0.setFillAfter(true);
			ani_0.setAnimationListener(animationListener);
			ani_1.setInterpolator(new AccelerateDecelerateInterpolator());   
			ani_1.setDuration(1000);
			ani_1.setFillEnabled(true);
			ani_1.setFillAfter(true);
			ani_1.setAnimationListener(animationListener);

点击Imageview启动动画:

 OnClickListener listener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.iv:
                                iv.startAnimation(ani_0);
				break;
}
}

添加动画监听,在AnimationEnd的时候切换动画:

final AnimationListener animationListener = new AnimationListener() {
		@Override
		public void onAnimationEnd(Animation animation) {
			if (animation == ani_0) {
				iv.startAnimation(ani_1);
			}
			if (animation == ani_1) {
				iv.startAnimation(ani_0);
			}
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
		}

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
		}
	};

布局,让ImageView一开始的时候在屏幕右边:
<?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">
        <ImageView android:id="@+id/iv"
		android:layout_width="wrap_content" 
		    android:layout_height="wrap_content" 
		    android:src="@drawable/search2"
		    android:scaleType="fitCenter"
		    android:layout_gravity="right"
		    />
</LinearLayout>

本实例只是实现连续动画的一种简单办法,针对本例子应该还有更简单的方法.


///
// 下面是anroid Api demo自带的一个shake(摇头)效果
///




import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class Animation1 extends Activity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_1);

        View loginButton = findViewById(R.id.login);
        loginButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        findViewById(R.id.pw).startAnimation(shake);
    }

}

anim/shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="10" android:duration="1000" android:interpolator="@anim/cycle_7" />

anim/cycle_7.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />


Android混合型动画AnimationSet
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 
    // 要使用findViewById, 一定要使用layout / *.xml 做为使用者介面
    setContentView( R.layout.main );
 
    // 取得UI 介面中的View 物件
    // 取得View 物件后,再透过转换成实际的物件
    ImageView ivPic = (ImageView)this.findViewById(R.id.widget10);  //底图
    ImageView iv = (ImageView)this.findViewById(R.id.widget28);
 
    // 设定ImageView 的图片来源
    ivPic.setImageResource( R.drawable.a2 );
    iv.setImageResource( R.drawable.icon );
 
    // 透明度动画设定(startAlpha, endAlpha)
    Animation am1 = new AlphaAnimation ( 1, 0 );
    // 动画开始到结束的执行时间(1000 = 1 秒)
    am1. setDuration ( 2000 );
    // 动画重复次数(-1 表示一直重复)
    am1. setRepeatCount ( -1 );
 
    // 旋转动画设定(startAngle, endAngle, rotateX, rotateY)
    Animation am2 = new RotateAnimation ( 0, 360, 30, 30 );
    // 动画开始到结束的执行时间(1000 = 1 秒)
    am2. setDuration ( 2000 );
    // 动画重复次数(-1 表示一直重复)
    am2. setRepeatCount ( -1 );
 
    // 动画集合
    AnimationSet am = new AnimationSet ( false );
    am. addAnimation ( am1 );
    am. addAnimation ( am2 );
 
    // 图片配置动画
    iv. setAnimation (am);
 
    // 动画开始
    am. startNow ();
}



附:android中所有的interpolator
android平台提供了如下的interpolater
AccelerateDecelerateInterpolator
AccelerateInterpolator
CycleInterpolator
DecelerateInterpolator
LinearInterpolator
AnticipateInterpolator
AnticipateOvershootInterpolator
BounceInterpolator
OvershootInterpolator
这些interpolater实现的是
android.view.animation.Interpolator接口
该接口只有一个方法
getInterpolation(float num)
也就是不同的interpolater计算出的值不一样,比如:
public float getInterpolation(float input)
{
if (mFactor == 1.0f)
{
return (float)(input * input);
}
else
{
return (float)Math.pow(input, 2 * mFactor);
}
}
当输入的值为1时候,那么返回值是平方,否则返回立方的值。
其实interpolator是按照这种方式计算的
This interpolator’s goal is to provide a multiplication factor given a time interval
based on a hyperbolic curve。
基于双曲线计算时间的间隔,进行相关的动画处理。
比如常用的accelerator_interpolator其xml定义的结构为:
<accelerator xmlns:android="http://schemas.android.com/apk/res/android"
factor="1" /> 其乘数为1;基于此进行计算。
在大家学习的过程中建议大家多看英文的东西,这样会事半功倍的。
  • 大小: 2.8 KB
  • 大小: 14.8 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值