首页 > 代码库 > 动画切换view布局
动画切换view布局
动画切换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>
动画切换view布局
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。