這是個(gè)仿淘寶 首頁(yè)上下滾動(dòng)消息的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;
//滾動(dòng)方向 上下 下上
private int rollingType = 0;
//是否重復(fù)滾動(dòng) 否,只將所有滾動(dòng)完畢停止
// private boolean mRepeat;
//滾動(dòng)間隔 默認(rèn)1000
private int mRollingInterval;
//動(dòng)畫時(shí)間
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);
}
}
/**
* 初始化動(dòng)畫
*/
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);
}
/**
* 動(dòng)畫
*
* @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;
}
}
自定義屬性
<!--垂直滾動(dòng)-->
<declare-styleable name="VerticalRollingLayout">
<!--滾動(dòng)方向-->
<attr name="rolling_type" format="enum">
<enum name="top2bottom" value="0" />
<enum name="bottom2top" value="1" />
</attr>
<!--是否重復(fù) default false-->
<!--<attr name="repeat" format="boolean" />-->
<!--滾動(dòng)時(shí)間間隔 毫秒 millisecond default 1000-->
<attr name="rolling_interval" format="integer" />
<!--動(dòng)畫時(shí)間-->
<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()