这是个仿淘宝 首页上下滚动消息的layout
+使用方式
/** * Created by mocaris on 2017/12/20. * 垂直跑马灯 */public class VerticalRollingLayout extends ViewFlipper { public static final int TOP_BOTTOM = 0; public static final int BOTTOM_TOP = 1; //滚动方向 上下 下上 private int rollingType = 0; //是否重复滚动 否,只将所有滚动完毕停止// private boolean mRepeat; //滚动间隔 默认1000 private int mRollingInterval; //动画时间 private int mAnimDuration; private ListAdapter mListAdapter; public VerticalRollingLayout(Context context) { this(context, null); } public VerticalRollingLayout(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.VerticalRollingLayout); rollingType = typedArray.getInt(R.styleable.VerticalRollingLayout_rolling_type, 0);// mRepeat = typedArray.getBoolean(R.styleable.VerticalRollingLayout_repeat, false); mRollingInterval = typedArray.getInt(R.styleable.VerticalRollingLayout_rolling_interval, 3500); mAnimDuration = typedArray.getInt(R.styleable.VerticalRollingLayout_rolling_duration, 1000); typedArray.recycle(); InitRollingAnim(); } public int getRollingType() { return rollingType; } /** * @param rollingType {@link #TOP_BOTTOM} {@link #BOTTOM_TOP} */ public void setRollingType(int rollingType) { this.rollingType = rollingType; } public void setAdapter(ListAdapter listAdapter) { this.mListAdapter = listAdapter; initChildView(); } public void notifyDataSetChanged() { initChildView(); } private void initChildView() { if (null == mListAdapter) { return; } this.removeAllViews(); for (int i = 0; i < mListAdapter.getCount(); i++) { View itemView = mListAdapter.getView(i, null, this); this.addView(itemView); } } /** * 初始化动画 */ private void InitRollingAnim() { this.clearAnimation(); TranslateAnimation rollingAnimIn = null; TranslateAnimation rollingAnimOut = null; switch (rollingType) { case BOTTOM_TOP://从下到上 rollingAnimOut = getRollingAnim(0f, 1f); rollingAnimIn = getRollingAnim(-1f, 0f); break; case TOP_BOTTOM://从上到下 default: rollingAnimIn = getRollingAnim(1f, 0f); rollingAnimOut = getRollingAnim(0f, -1f); break; } this.setFlipInterval(mRollingInterval); this.setInAnimation(rollingAnimIn); this.setOutAnimation(rollingAnimOut); } /** * 动画 * * @param fromY * @param toY * @return */// //int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue private TranslateAnimation getRollingAnim(float fromY, float toY) { TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF , 0 , Animation.RELATIVE_TO_SELF , 0 , Animation.RELATIVE_TO_SELF , fromY , Animation.RELATIVE_TO_SELF , toY); translateAnimation.setDuration(mAnimDuration); return translateAnimation; } }
自定义属性
<!--垂直滚动--> <declare-styleable name="VerticalRollingLayout"> <!--滚动方向--> <attr name="rolling_type" format="enum"> <enum name="top2bottom" value="0" /> <enum name="bottom2top" value="1" /> </attr> <!--是否重复 default false--> <!--<attr name="repeat" format="boolean" />--> <!--滚动时间间隔 毫秒 millisecond default 1000--> <attr name="rolling_interval" format="integer" /> <!--动画时间--> <attr name="rolling_duration" format="integer" /> </declare-styleable>
使用方法
<com.zfzx.investor.custom.weight.VerticalRollingLayout android:id="@+id/vrl_home" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" />
。。。
vrl_home.rollingType = VerticalRollingLayout.BOTTOM_TOP//homeRollingAdapter 是继承ListAdapter 的 和ListAdapter 用法一样 vrl_home.setAdapter(homeRollingAdapter) vrl_home.startFlipping()
作者:Mocaris
链接:https://www.jianshu.com/p/06c0f30ef47e