///////////////////////////////////////////////////////////////////
//
下面是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);
-
-
- setContentView( R.layout.main );
-
-
-
- ImageView ivPic = (ImageView)this
.findViewById(R.id.widget10);
- ImageView iv = (ImageView)this
.findViewById(R.id.widget28);
-
-
- ivPic.setImageResource( R.drawable.a2 );
- iv.setImageResource( R.drawable.icon );
-
-
- Animation am1 = new
AlphaAnimation (
1
,
0
);
-
- am1. setDuration ( 2000
);
-
- am1. setRepeatCount ( -1
);
-
-
- Animation am2 = new
RotateAnimation (
0
,
360
,
30
,
30
);
-
- am2. setDuration ( 2000
);
-
- 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;基于此进行计算。
在大家学习的过程中建议大家多看英文的东西,这样会事半功倍的。