来自Android官方API DEMO中动画效果实例。
/** * FlipperView文字效果动画之:文字滚动动画 * * @description: * @author ldm * @date 2016-5-17 上午9:58:26 */
public class Animation2 extends Activity implements AdapterView.OnItemSelectedListener {
// Spinner数据源
private String[] mStrings = { "Push up", "Push left", "Cross fade",
"Hyperspace" };
// 控件ViewFlipper
private ViewFlipper mFlipper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_2);
// 初始化UI控件
initViews();
}
private void initViews() {
mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));
mFlipper.startFlipping();
Spinner s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, mStrings);
// 定义Spinner下拉菜单模式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 设置数据
s.setAdapter(adapter);
// 添加监听
s.setOnItemSelectedListener(this);
}
/** * Spinner的item选择监听事件处理 */
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
switch (position) {
case 0:// 文字从下进入,从上移出,伴随透明度变化
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_up_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_up_out));
break;
case 1:// 文字从右侧向左进入,从右侧移出,伴随透明度变化
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
break;
case 2:// 文字透明度改变,从0-1-0
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
break;
default:// 多维空间动画(复合动画效果)
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.hyperspace_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.hyperspace_out));
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
// DO NOTHING
}
}
—-布局文件,TextView中添加自己想显示的文字——
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dip" >
<ViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dip" android:flipInterval="2000" >
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/animation_2_text_1" android:textSize="26sp" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/animation_2_text_2" android:textSize="26sp" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/animation_2_text_3" android:textSize="26sp" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/animation_2_text_4" android:textSize="26sp" />
</ViewFlipper>
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dip" android:text="@string/animation_2_instructions" />
<Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
—-动画文件res/anim文件夹下——
1,push_up_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:duration="300"<!--动画时长-->
android:fromYDelta="100%p"<!--Y方向初始位置-->
android:toYDelta="0" /><!--Y方向动画结束位置-->
<alpha android:duration="300" android:fromAlpha="0.0"<!--初始透明度-->
android:toAlpha="1.0" /><!--动画结束时透明度-->
</set>
2,push_up_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:duration="300" android:fromYDelta="0" android:toYDelta="-100%p" />
<alpha android:duration="300" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
3,push_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:duration="300" android:fromXDelta="100%p" android:toXDelta="0" />
<alpha android:duration="300" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>
4,push_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:duration="300" android:fromXDelta="0" android:toXDelta="-100%p" />
<alpha android:duration="300" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>
5,fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_longAnimTime" android:fromAlpha="0.0" android:interpolator="@interpolator/decelerate_quad" android:toAlpha="1.0" />
6,fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_mediumAnimTime" android:fromAlpha="1.0" android:interpolator="@interpolator/accelerate_quad"<!--设置动画插值器-->
android:toAlpha="0.0" />
7,hyperspace_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromAlpha="0.0" android:startOffset="1200"<!--设置启动时间-->
android:toAlpha="1.0" />
8,hyperspace_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <scale android:duration="700" android:fillAfter="false"<!--动画结束画面是否停留在最后一帧--> android:fillEnabled="true"<!--使能填充效果--> android:fromXScale="1.0"<!--X方向起始缩放值--> android:fromYScale="1.0"<!--Y方向起始缩放值--> android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%"<!--动画相对于物件的X、Y坐标的开始位置--> android:pivotY="50%" android:toXScale="1.4" android:toYScale="0.6" /> <set android:interpolator="@android:anim/accelerate_interpolator" > <scale<!--缩放动画--> android:duration="400" android:fillAfter="true" android:fillBefore="false" android:fillEnabled="true" android:fromXScale="1.4" android:fromYScale="0.6" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:toXScale="0.0" android:toYScale="0.0" /> <rotate<!--旋转动画--> android:duration="400" android:fillAfter="true" android:fillBefore="false" android:fillEnabled="true" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:startOffset="700" android:toDegrees="-45" android:toYScale="0.0" /> </set> </set>
开源代码:https://github.com/ldm520/ANDROID_API_DEMOS