Android ListView动画实现方法

在Android中listview是最常用的控件之一,但是有时候我们会觉得千篇一律的listview看起来过于单调,于是就产生了listView动画,listview加载了动画会让用户体验更好,本期就分享一些listview动画以及实现方法,效果图



相信大家都熟悉Android的Tween动画,前四种动画就是Translate,Alpha,Rotate,Scale,最后一种Rotate3d则是用了一个3D旋转动画工具类Rotate3dAnimation,这个类的构造函数中接收一些3D旋转时所需用到的参数,比如旋转开始和结束的角度,旋转的中心点等

LayoutAnimationController可以控制一组控件按照规定显示,ListView中的mListView.setLayoutAnimation相信大家都知道是用来干什么的了,接下来上代码


	private Button button, button2, button3, button4, button5;
	private ListView mListView;
	private Animation animation;
	private LayoutAnimationController controller;
	private String[] arry = { "一", "二", "三", "四", "五", "六" };
	private ArrayAdapter<String> adapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, arry);
		mListView.setAdapter(adapter);

	}

	private void initView() {
		// TODO Auto-generated method stub
		mListView = (ListView) findViewById(R.id.list);
		button = (Button) findViewById(R.id.btn_tran);
		button.setOnClickListener(this);
		button2 = (Button) findViewById(R.id.btn_alpha);
		button2.setOnClickListener(this);
		button3 = (Button) findViewById(R.id.btn_rotate);
		button3.setOnClickListener(this);
		button4 = (Button) findViewById(R.id.btn_scale);
		button4.setOnClickListener(this);
		button5 = (Button) findViewById(R.id.rotate3d);
		button5.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// LayoutAnimationController.ORDER_NORMAL; 顺序显示
		// LayoutAnimationController.ORDER_REVERSE;反显示
		// LayoutAnimationController.ORDER_RANDOM; 随机显示
		switch (arg0.getId()) {
		case R.id.btn_tran:
			animation = new TranslateAnimation(-50f, 0f, 0f, 0f);
			animation.setDuration(500);
			//1f为延时
			controller = new LayoutAnimationController(animation, 1f);
			controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
			mListView.setLayoutAnimation(controller);
			adapter.notifyDataSetInvalidated();
			break;
		case R.id.btn_alpha:
			animation = new AlphaAnimation(0f, 1f);
			animation.setDuration(500);
			controller = new LayoutAnimationController(animation, 1f);
			controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
			mListView.setLayoutAnimation(controller);
			adapter.notifyDataSetInvalidated();
			break;
		case R.id.btn_rotate:
			animation = new RotateAnimation(0f, 360f);
			animation.setDuration(500);
			controller = new LayoutAnimationController(animation, 1f);
			controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
			mListView.setLayoutAnimation(controller);
			adapter.notifyDataSetInvalidated();
			break;
		case R.id.btn_scale:
			animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);
			animation.setDuration(500);
			controller = new LayoutAnimationController(animation, 1f);
			controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
			mListView.setLayoutAnimation(controller);
			adapter.notifyDataSetInvalidated();
			break;
		case R.id.rotate3d:
			animation = new Rotate3dAnimation(0, 360, 200, 200, 0, true);
			animation.setDuration(1000);
			controller = new LayoutAnimationController(animation, 0.1f);
			controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
			mListView.setLayoutAnimation(controller);
			adapter.notifyDataSetInvalidated();
			break;
		default:
			break;
		}

	}

这样大家可以随心所欲的编写自己喜欢的动画效果


项目源码


  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值