码迷,mamicode.com
首页 > 其他好文 > 详细

动画切换view布局

时间:2015-01-04 23:14:42      阅读:372      评论:0      收藏:0      [点我收藏+]

标签:

动画切换view布局,可用于滚屏显示评论等例子

package com.example.animationviewdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ListAdapter;

/**
 * 动画切换View
 */
public class AnimationView extends FrameLayout implements Runnable {
	private static final int DEFAULT_TIME_SPAN = 3000;
	/**
	 * 当前显示view序号
	 */
	private int index = 0;
	/**
	 * 切换时间
	 */
	private int timeSpan = DEFAULT_TIME_SPAN;
	
	/**
	 * 切出view
	 */
	private View firstView;
	/**
	 * 切入view
	 */
	private View secondView;
	
	/**
	 * 切入动画
	 */
	private Animation inAnim;
	/**
	 * 切出动画
	 */
	private Animation outAnim;
	/**
	 * view列表适配器
	 */
	private ListAdapter adapter;
	private Handler handler = new Handler();
	public AnimationView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public AnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.AnimationView);
		
		timeSpan = ta.getInteger(R.styleable.AnimationView_time_span, DEFAULT_TIME_SPAN);
		
		int animInId = ta.getResourceId(R.styleable.AnimationView_anim_in, android.R.anim.slide_in_left);
		inAnim = AnimationUtils.loadAnimation(context, animInId);
		inAnim.setFillAfter(true);
		
		int animOutId = ta.getResourceId(R.styleable.AnimationView_anim_out, android.R.anim.slide_out_right);
		outAnim = AnimationUtils
				.loadAnimation(context, animOutId);
		outAnim.setFillAfter(true);
		
		ta.recycle();
	}

	/**
	 * 开始
	 */
	public void start() {
		if(adapter == null || adapter.getCount() == 0)
			return;
		removeAllViews();
		firstView = adapter.getView(index, null, null);
		addView(firstView);
		secondView = null;
		handler.postDelayed(this, timeSpan);
	}

	/**
	 * 停止
	 */
	public void stop() {
		handler.removeCallbacks(this);
	}

	public ListAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(ListAdapter adapter) {
		this.adapter = adapter;
		handler.removeCallbacks(this);
		index = 0;
	}

	@Override
	public void run() {
		if (firstView == null) {
			firstView = adapter.getView(index, null, null);
			addView(firstView);
		} else {
			firstView = adapter.getView(index, firstView, this);
		}
		++index;
		if (index >= adapter.getCount()) {
			index = 0;
		}
		if (secondView == null) {
			secondView = adapter.getView(index, null, null);
			addView(secondView);
		} else {
			secondView = adapter.getView(index, secondView, this);
		}
		firstView.startAnimation(outAnim);
		secondView.startAnimation(inAnim);
		handler.postDelayed(this, timeSpan);
	}

	public int getTimeSpan() {
		return timeSpan;
	}

	public void setTimeSpan(int timeSpan) {
		this.timeSpan = timeSpan;
	}

}



<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="AnimationView">
        <attr name="time_span" format="integer" />
        <attr name="anim_in" format="reference" />
        <attr name="anim_out" format="reference" />
    </declare-styleable>

</resources>



demo链接: http://git.oschina.net/lonelycoder/AnimationViewDemo

动画切换view布局

标签:

原文地址:http://my.oschina.net/bankofchina/blog/363649

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!