长图片走马灯效果

package com.oxbix.beile.orepool.view;

/**
 * 创建者     ZCL
 * 创建时间   2018/5/29 9:45
 * 描述	      ${滚动长图}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */

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

import com.oxbix.beile.R;

/**
 * 跑马灯View
 * Created by xuyy on 2016/8/23.
 */
public class MarqueeView extends HorizontalScrollView implements Runnable{

    private static final String TAG = "MarqueeView";
    private Context      context;
    private LinearLayout mainLayout;//跑马灯滚动部分
    private int scrollSpeed     = 15;//滚动速度
    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.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
       // lp.setMargins(viewMargin, 0, 0, 0);
        view.setLayoutParams(lp);

        mainLayout.addView(view);
        view.measure(0, 0);//测量view
        viewWidth = view.getMeasuredWidth();
        android.util.Log.d(TAG, "返回数据是getMeasuredWidth:" + view.getMeasuredWidth());
    }

    //开始滚动
    public void startScroll() {
        removeCallbacks(this);
        currentX = (scrollDirection == LEFT_TO_RIGHT ? viewWidth-screenWidth : 0);
        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);
                mainLayout.measure(0, 0);
               // LogUtil.util(TAG, "LEFT_TO_RIGHT, 返回数据是scrollTo:" + currentX + "-------" + viewWidth);

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

            case RIGHT_TO_LEFT:
                //LogUtil.util(TAG, "RIGHT_TO_LEFT,返回数据是scrollTo:" + currentX + "-------" + viewWidth);
                mainLayout.scrollTo(currentX, 0);
                currentX ++;

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

        postDelayed(this, 50 / scrollSpeed);
    }

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



<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutandroid:id="@+id/orepool_bg"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">


<com.oxbix.beile.orepool.view.MarqueeView
    android:id="@+id/orepool_marqueeeview"
    android:layout_width="match_parent"
    android:layout_height="500dp">
</com.oxbix.beile.orepool.view.MarqueeView>
</LinearLayout>
public class AboutOreActivity extends BaseZcActivity {
    
@BindView(R.id.orepool_marqueeeview)
MarqueeView        mOrepoolMarqueeeview
@Override protected int setLayoutId() { return R.layout.activity_about_ore; } @Override protected void initData() {      
mOrepoolMarqueeeview.addViewInQueue(iv);//设置背景图
mOrepoolMarqueeeview.setScrollSpeed(10);//设置速度
mOrepoolMarqueeeview.setScrollDirection(MarqueeView.RIGHT_TO_LEFT);//设置从那边开始滑动
mOrepoolMarqueeeview.startScroll();开始滑动
}}

猜你喜欢

转载自blog.csdn.net/qq_33143459/article/details/80734202