运行效果:
View布局:
Control层代码:
public class MainActivity extends AppCompatActivity {
private ConstraintLayout constraint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraint=findViewById(R.id.constraint);
ScaleAnimation scale=new ScaleAnimation(0,1,0,1);//定义一个缩放动画
scale.setDuration(5000);
//LayoutAnimationController:可以控制一组控件按照规定显示,用于一个布局或者ViewGroup的动画控制器,
// 它能够让每个子View在不同的时间点分别执行相同的动画
LayoutAnimationController controller=new LayoutAnimationController(scale,0.5f);//item动画延时,控件出来一半时下一个出
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);//控件出来的顺序
constraint.setLayoutAnimation(controller);//部署到布局中
}
}
运行效果:
View布局:
Control层代码:
public class LayoutChangeAnimation extends AppCompatActivity {
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_change_animation);
linearLayout=findViewById(R.id.linear);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.action_add){
Button add_button=new Button(this);
add_button.setText("remove me");
add_button.setTextSize(24);
linearLayout.addView(add_button);//布局中添加button
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout.removeView(v);//移除button
}
});
}
return super.onOptionsItemSelected(item);
}
}
运行效果:
View布局:
资源配置文件anim:
item动画scale.xml:
主布局动画list_view_anim.xml:
Control层代码:
public class ListViewLayoutAnimation extends ListActivity{
//ListActivity类中集成了一个ListView控件。
//通过继承ListActivity类可方便地使用ListView控件
private ArrayAdapter arrayAdapter;
//private ScaleAnimation scaleAnimation;
//private LayoutAnimationController controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] strings=new String[]{"小明","小红","小兰","小绿","小明","小红","小兰","小绿","小明",
"小红","小兰","小绿","小明","小红","小兰","小绿","小明","小红","小兰","小绿","小明",
"小红","小兰","小绿","小明","小红","小兰","小绿","小明","小红","小兰","小绿"};
//添加子布局和数据
arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,strings);
setListAdapter(arrayAdapter);//部署适配器
//第一种方法:Java代码
// scaleAnimation=new ScaleAnimation(0,1,0,1);
// scaleAnimation.setDuration(1000);
// controller=new LayoutAnimationController(scaleAnimation,0.5f);
//
// getListView().setLayoutAnimation(controller);//getListView():获取ListActivity自带的ListView对象
//第二种方法:布局文件的配置
setContentView(R.layout.activity_listview_layout_animation);
}
}