布局动画,给布局添加动画之后,里面的全部控件都受影响。
package com.example.l01layoutanimationlinearlayout;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
public class MainActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null){
getFragmentManager().beginTransaction()
.add(R.id.container,new PlaceholderFragment()).commit();
}
}
</pre><pre name="code" class="java">
public static class PlaceholderFragment extends android.app.Fragment{
public PlaceholderFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final LinearLayout rootView = (LinearLayout) inflater
.inflate(R.layout.fragment_main, container, false);
//创建一个从0到1的缩放效果
ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1);
//设置延迟
sa.setDuration(2000);
//添加一个布局动画控制
//这玩意的延迟不能用xml文件设置,折腾了我一个小时。。调试,加上去按钮就不出来了。
// LayoutAnimationController lac = new LayoutAnimationController(sa,(float)R.string.delay);
LayoutAnimationController lac = new LayoutAnimationController(sa,0.5f);
//设置出现的顺序为由下往上
// lac.setOrder(LayoutAnimationController.ORDER_REVERSE);
rootView.setLayoutAnimation(lac);
return rootView;
}
}
}
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity2" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_Name" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</FrameLayout>
布局内容改变动画
package com.example.l02layoutchangeanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout rootView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//往屏幕上添加加号
rootView = (LinearLayout) findViewById(R.id.rootView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//添加一个按钮并且给该按钮添加点击事件
private void addButton(){
Button btn = new Button(this);
btn.setText("Remove me");
rootView.addView(btn);
/*
如果想自定义动画效果
可以通过以下方法传入一个动画效果
rootView.setLayoutTransition(null);
*/
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rootView.removeView(v);
}
});
}
//重写此方法,当点击到+号时运行添加按钮的方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_settings:
return true;
case R.id.action_add:
addButton();
default: break;
}
return super.onOptionsItemSelected(item);
}
}
xml文件
android:animateLayoutChanges="true" 加上此句
在menu 菜单上加入一个按钮
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item android:id="@+id/action_add"
android:showAsAction="always"
android:icon="@android:drawable/ic_input_add"/>
</menu>
为列表添加布局添加布局动画效果
package com.example.l03layoutabunatuibublistview;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
private List<String> arr ;
private ArrayAdapter<String> adapter;
private LayoutAnimationController lac;
private ScaleAnimation sa ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//创建Arr=容器
arr = new ArrayList<String>();
arr.add("hello");
arr.add("佛山");
arr.add("新疆");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
setListAdapter(adapter);
sa = new ScaleAnimation(0, 1, 0, 1);
sa.setDuration(1000);
lac = new LayoutAnimationController(sa,0.5f);
getListView().setLayoutAnimation(lac);
}
@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;
}
}
用xml文件给列表添加动画效果
package com.example.l03layoutabunatuibublistview;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
private List<String> arr ;
private ArrayAdapter<String> adapter;
private LayoutAnimationController lac;
// private ScaleAnimation sa ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建Arr=容器
arr = new ArrayList<String>();
arr.add("hello");
arr.add("佛山");
arr.add("新疆");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
setListAdapter(adapter);
//通过手工设置也可以通过xml文件进行设置动画效果
/* sa = new ScaleAnimation(0, 1, 0, 1);
sa.setDuration(1000);
lac = new LayoutAnimationController(*/ //sa,0.5f);
// getListView().setLayoutAnimation();
}
@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;
}
}
package com.example.l03layoutabunatuibublistview;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
private List<String> arr ;
private ArrayAdapter<String> adapter;
private LayoutAnimationController lac;
// private ScaleAnimation sa ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建Arr=容器
arr = new ArrayList<String>();
arr.add("hello");
arr.add("佛山");
arr.add("新疆");
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);
setListAdapter(adapter);
//通过手工设置也可以通过xml文件进行设置动画效果
/* sa = new ScaleAnimation(0, 1, 0, 1);
sa.setDuration(1000);
lac = new LayoutAnimationController(*/ //sa,0.5f);
// getListView().setLayoutAnimation();
}
@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;
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/scale_0_1"
android:delay="0.5" >
</layoutAnimation>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/list_anim" >
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:duration="1000"
>
</scale>