ListView的淡入淡出和Activity的淡入淡出补间动画效果Animation
Posted 巫山码农
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ListView的淡入淡出和Activity的淡入淡出补间动画效果Animation相关的知识,希望对你有一定的参考价值。
//=========主页面=======================
package com.bw.lianxi7;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
lv = (ListView) findViewById(R.id.lv);
//通过加载XML动画设置文件来创建一个Animation对象;
Animation animation=AnimationUtils.loadAnimation(this, R.anim.item_animation);
//得到一个LayoutAnimationController对象;控制器
LayoutAnimationController controller=new LayoutAnimationController(animation);
//设置控件显示的顺序;
controller.setOrder(LayoutAnimationController.ORDER_REVERSE);//背面
//设置条目动画显示间隔时间;
controller.setDelay(2f);
//为ListView设置LayoutAnimationController属性;
lv.setLayoutAnimation(controller);
}
public void go2Other(View view){
Intent intent=new Intent(this,OtherActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
}
@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;
}
}
//=============主页面的布局 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"
android:entries="@array/names"></ListView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转页面"
android:layout_below="@+id/lv"
android:onClick="go2Other"/>
</RelativeLayout>
//=========此为 values包下的string.xml 中添加的====是Listview展示的条目内容================
<string-array name="names">
<item>张三1</item>
<item>张三2</item>
<item>张三3</item>
<item>张三4</item>
<item>张三5</item>
<item>张三6</item>
<item>张三7</item>
<item>张三8</item>
<item>张三9</item>
</string-array>
//=======此为在res 包下创建一个anim 包 ,再在anim包下 创建 item_animation.xml ===================
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%p"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="1000">
</translate>
//===========在anim包下 创建 slide_left_in.xml ======表示左进================================
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-100%p"
android:toXDelta="0"
android:toYDelta="0"
android:fromYDelta="0"
android:duration="2000">
</translate>
//==============在anim包下 创建 slide_left_out.xml ======表示左出====================
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="-100%p"
android:toYDelta="0"
android:duration="2000">
</translate>
//==========在anim包下 创建 slide_right_in.xml ======表示右进===================
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="0"
android:duration="2000">
</translate>
//============在anim包下 创建 slide_right_out.xml ======表示右出=====================
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%p"
android:toYDelta="0"
android:duration="2000">
</translate>
//=======创建一个OtherActivity====================
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
}
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.other, menu);
return true;
}
}
//=========OtherActivity的布局文件没有做处理=====================
当点击button时,会淡入淡出进入OtherActivity
以上是关于ListView的淡入淡出和Activity的淡入淡出补间动画效果Animation的主要内容,如果未能解决你的问题,请参考以下文章
React-Native ListView加载图片淡入淡出效果的组件
Android Activity 淡入淡出和从底部向上弹出动画效果
Android四大组件——Activity跳转动画淡出淡入滑出滑入自定义退出进入