android任意view的跑马灯效果

package ndk.com.guass.www.dnxy_demo;

/**
* Created by guass on 2017/12/16.
*/

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

public class MarqueeView extends HorizontalScrollView implements Runnable{

private Context context;
private LinearLayout mainLayout;//跑马灯滚动部分
private int scrollSpeed = 5;//滚动速度
private int scrollDirection = LEFT_TO_RIGHT;//滚动方向
private int currentX;//当前x坐标
private int viewMargin = 20;//View间距
private int viewWidth;//View总宽度
private int screenWidth;//屏幕宽度

public static final int LEFT_TO_RIGHT = 1;
public static final int RIGHT_TO_LEFT = 2;

public MarqueeView(Context context) {
    this(context, null);
}

public MarqueeView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MarqueeView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    initView();
}

void initView() {
    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    screenWidth = wm.getDefaultDisplay().getWidth();
    mainLayout = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.scroll_content, null);
    this.addView(mainLayout);
}

public void addViewInQueue(View view){
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(viewMargin, 0, 0, 0);
    view.setLayoutParams(lp);
    mainLayout.addView(view);
    view.measure(0, 0);//测量view
    viewWidth = viewWidth + view.getMeasuredWidth() + viewMargin;
}

//开始滚动
public void startScroll(){
    removeCallbacks(this);
    currentX = (scrollDirection == LEFT_TO_RIGHT ? viewWidth : -screenWidth);
    post(this);
}

//停止滚动
public void stopScroll(){
    removeCallbacks(this);
}

//设置View间距
public void setViewMargin(int viewMargin){
    this.viewMargin = viewMargin;
}

//设置滚动速度
public void setScrollSpeed(int scrollSpeed){
    this.scrollSpeed = scrollSpeed;
}

//设置滚动方向 默认从左向右
public void setScrollDirection(int scrollDirection){
    this.scrollDirection = scrollDirection;
}

@Override
public void run() {
    switch (scrollDirection){
        case LEFT_TO_RIGHT:
            mainLayout.scrollTo(currentX, 0);
            currentX --;

            if (-currentX >= screenWidth) {
                mainLayout.scrollTo(viewWidth, 0);
                currentX = viewWidth;
            }
            break;
        case RIGHT_TO_LEFT:
            mainLayout.scrollTo(currentX, 0);
            currentX ++;

            if (currentX >= viewWidth) {
                mainLayout.scrollTo(-screenWidth, 0);
                currentX = -screenWidth;
            }
            break;
        default:
            break;
    }

    postDelayed(this, 50 / scrollSpeed);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return false;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值