在Android中如何与不同的滚动速度滚动型同步? [英] In Android how to synchronize scrollview with different scrolling speeds?

查看:98
本文介绍了在Android中如何与不同的滚动速度滚动型同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些相关的滚动型的手段(当我滚动一种观点认为别人也会滚动)。我能够正确地做到这一点,但现在我要管理的滚动装置的速度(当我滚动我scrollview1比scrollview2应该滚动+10 scrollview3应滚动+20或任何速度)同其他(scrollview2和scrollview3)也。

I have some dependent scrollview means (when I scroll one view others will also scroll). I am able to do it properly, but now I want to manage the speed of scroll means (when I scroll my scrollview1 than scrollview2 should scroll +10 and scrollview3 should scroll +20 or whatever speed) same for other (scrollview2, and scrollview3) also.

我检查有一个名为scrollview.scrollto方法(X,Y)。它用于管理滚动但是当我增加scollto(X,Y + scrollviews(I).getSpeed​​()),比它给了我的StackOverflow例外。

I check there is a method called scrollview.scrollto(x,y). Which used to manage the scroll but when i increase scollto(x, y+scrollviews(i).getSpeed()) than it gives me stackOverflow exception.

我附上我的code请看这个,给我一些建议,怎样才能解决这个问题。

I am attaching my code please look into this and give me some suggestion how can solve this problem.

我的自定义滚动视图类是:

My custom scrollView class is:

public class CustomVerticalObserveScroll extends ScrollView {
    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;


    public CustomVerticalObserveScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
        // TODO Auto-generated constructor stub
    }

    private CustomScrollLisner scrollViewListener = null;

    public CustomVerticalObserveScroll(Context context) {
        super(context);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    public CustomVerticalObserveScroll(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    public void setScrollViewListener(CustomScrollLisner scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
         if (scrollViewListener != null) {
         scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
         }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev)
                && mGestureDetector.onTouchEvent(ev);
    }

    // Return false if we're scrolling in the x direction
    class YScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
            if (Math.abs(distanceY) > Math.abs(distanceX)) {
                return true;
            }
            if (Math.abs(distanceX) > Math.abs(distanceY)) {
                return true;
            }
            return false;
        }
    }

}

这code我使用,使滚动滚动相关意见。

and this code I am using to make scroll dependent scroll views.

public class RelativePanoFeature implements IFeaturetype, OnTouchListener {
    private String type;
    private FeatureCordinates locationCordinates;
    private int mOrientation;
    private String image;
    private FeatureCordinates triggerCordinates;
    private String scrollDirection;
    private String scrollSpeed;
    private String scrollHandler;
    CustomVerticalObserveScroll vertical_scroll;
    CustomHorizontalObserveScroll horizontal_scroll;
    RelativeLayout vsChild;

    long timeonDown, timeonUp;
    Thread t;
    Handler mhandler;
    float downx, downy;

    int touchId;

    public static int scrollid = 0;
    public static ArrayList<RelativePanoHandler> storePanoHandler = new ArrayList<RelativePanoHandler>();

    public RelativePanoFeature(String type) {
        this.type = type;

    }

    @Override
    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String getType() {
        return type;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getImage() {
        return image;
    }

    public FeatureCordinates getLocation() {
        return locationCordinates;
    }

    public void setLocation(FeatureCordinates featureCordinates) {
        this.locationCordinates = featureCordinates;
    }

    public void setOrientation(int mOrientation) {
        this.mOrientation = mOrientation;
    }

    public int getOrientation() {
        return mOrientation;
    }

    public FeatureCordinates getTrigger() {
        return triggerCordinates;
    }

    public void setTrigger(FeatureCordinates trigger) {
        this.triggerCordinates = trigger;
    }

    public void setScrollDirection(String scrollDirection) {
        this.scrollDirection = scrollDirection;
    }

    public String getScrollDirection() {
        return scrollDirection;
    }

    public void setScrollSpeed(String scrollSpeed) {
        this.scrollSpeed = scrollSpeed;
    }

    public String getScrollSpeed() {
        return scrollSpeed;
    }

    public void setScrollHandler(String scrollHandler) {
        this.scrollHandler = scrollHandler;
    }

    public String getScrollHandler() {
        return scrollHandler;
    }

    public void setTouchId(int touchid) {
        this.touchId = touchid;
    }

    public int getTOuchId() {
        return touchId;
    }

    /* function to draw relative pano */
    public void drawRelativePano(final Context con,
            final RelativeLayout parent, final Handler handle) {
        /* splitting the path from images key in the string */
        RelativePanoHandler panHandler = new RelativePanoHandler();
        final int height;
        final int width;
        vsChild = new RelativeLayout(con);
        mhandler = handle;

        /* giving size of of vertical scroll's child */
        LayoutParams imageViewLayoutParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        vsChild.setLayoutParams(imageViewLayoutParams);

        /* splitting the path from images key in the string */
        String path[] = getImage().split("images");
        try {
            /* Initialise loader to load the image inside child */
            BackgroundImageLoader loader = new BackgroundImageLoader(vsChild,
                    Property.FILEPATH + path[1], con);
            try {
                loader.execute();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
            /* getting height and width of image from loader object */
            height = loader.get().getHeight();
            width = loader.get().getWidth();
            /*
             * condition for putting the child view in vertical scroll and
             * implementing the multi directional scroll for event pano
             */
            int locWidth = getLocation().getWidth(), locHeight = getLocation()
                    .getHeight();
            System.out.println("Width= " + width + " Location width= "
                    + locWidth);

            System.out.println("Heoght= " + height + " Location Height= "
                    + locHeight

            );

            if (width > (getLocation().getWidth())
                    || height > (getLocation().getHeight())) {

                vertical_scroll = new CustomVerticalObserveScroll(con);
                horizontal_scroll = new CustomHorizontalObserveScroll(con);

                vertical_scroll.setFillViewport(true);
                horizontal_scroll.setFillViewport(true);

                vertical_scroll.setId(scrollid);

                horizontal_scroll.setFadingEdgeLength(0);

                /*
                 * adding the soft later on vertical and horizontal scroll if
                 * the detected device is on api level 10 or more than that
                 */
                if (Build.VERSION.SDK_INT > 10) {
                    vertical_scroll
                            .setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                    horizontal_scroll.setLayerType(View.LAYER_TYPE_SOFTWARE,
                            null);
                }

                vsChild.setEnabled(true);

                /*
                 * parameters for setting the height and width of vertical
                 * scroll
                 */
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        getLocation().getWidth(), getLocation().getHeight());
                params.leftMargin = getLocation().getX();
                params.topMargin = getLocation().getY();
                vertical_scroll.setLayoutParams(params);

                /* adding vertical scroll child this child will hold the image */
                vertical_scroll.addView(vsChild, width, height);
                horizontal_scroll.setLayoutParams(params);
                /*
                 * adding vertical scroll as a child of horizontal scroll for
                 * multidirectional scrolling
                 */
                horizontal_scroll.addView(vertical_scroll);

                /*
                 * at last add this horizontal scroll in side parent which will
                 * hold the multidirectional scroll
                 */
                parent.setTag(getScrollHandler());
                parent.addView(horizontal_scroll);

                // vertical_scroll.setId(id)

                panHandler.setVerticalScroll(vertical_scroll);
                panHandler.setHandlerTag(getScrollHandler());
                panHandler.setPanoSpeed(Integer.parseInt(getScrollSpeed()));

                storePanoHandler.add(panHandler);
                int size = storePanoHandler.size();
                System.out.println("Vertical Scroll objec size=" + size);

            }
            System.out.println("TAg= " + parent.getTag());
            String scdir = getScrollDirection();
            System.out.println("Scroll Directoion= " + scdir);

            scrollid++;

            if (getScrollDirection().equalsIgnoreCase("Y")) {
                vertical_scroll.setScrollViewListener(new CustomScrollLisner() {

                    @Override
                    public void onScrollChanged(
                            CustomHorizontalObserveScroll scrollView, int x,
                            int y, int oldx, int oldy) {
                    }

                    @Override
                    public void onScrollChanged(
                            CustomVerticalObserveScroll scrollView, int x,
                            int y, int oldx, int oldy) {
                        if (scrollView == storePanoHandler.get(getTOuchId())
                                .getVerticalScroll()) {

                            for (int i = 0; i < storePanoHandler.size(); i++) {

                                storePanoHandler.get(i).getVerticalScroll()
                                        .scrollTo(x, y);

                                storePanoHandler.get(i).getVerticalScroll().

                                // storePanoHandler.
                                // .get(i)
                                // .getVerticalScroll()
                                // .scrollTo(
                                // x,
                                // oldy
                                // + storePanoHandler.get(
                                // i)
                                // .getPanoSpeed());
                            }

                        }
                    }
                });
            }

            // if (getScrollDirection().equalsIgnoreCase("X")) {
            // vertical_scroll.setScrollViewListener(new CustomScrollLisner() {
            //
            // @Override
            // public void onScrollChanged(
            // CustomHorizontalObserveScroll scrollView, int x,
            // int y, int oldx, int oldy) {
            // // if (scrollView == storePanoHandler.get(getTOuchId())
            // // .getVerticalScroll()) {
            // //
            // // for (int i = 0; i < storePanoHandler.size(); i++) {
            // // storePanoHandler.get(i).getVerticalScroll()
            // // .smoothScrollTo(x, y);
            // //
            // // }
            // // }
            // }
            //
            // @Override
            // public void onScrollChanged(
            // CustomVerticalObserveScroll scrollView, int x,
            // int y, int oldx, int oldy) {
            // }
            // });
            // }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        /*
         * set touch listeners on vertical and horizontal scrolls it will use to
         * disable the scroll for it's parent like [image or view pager when
         * user interacting with any of custom scroll]
         */
        horizontal_scroll.setOnTouchListener(this);
        vertical_scroll.setOnTouchListener(this);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        boolean scrollingPanaroma = true;
        // changing token value for getting scroll

        if (v == vertical_scroll || v == horizontal_scroll) {
            /*
             * Disabling the parent control [list, pager] when user interacting
             * with multidirectional scroll
             */
            System.out.println("Pano touch Id= " + vertical_scroll.getId());

            setTouchId(vertical_scroll.getId());

            if (scrollingPanaroma == true) {
                v.getParent().getParent().getParent()
                        .requestDisallowInterceptTouchEvent(true);
            }
            /*
             * enable the parent control [list, pager] when user done with
             * multidirectional scroll
             */
            else {
                v.getParent().getParent().getParent()
                        .requestDisallowInterceptTouchEvent(false);
            }
        }
        return false;

    }
}

请帮我解决了这一点,因为我在这一点上确实被卡住。 Thnaks。

Please help me to solve this out because I am really stucked at this point. Thnaks.

推荐答案

下面是code我用来降低滚动型的滚动速度编程,

Here is the code I used to slow down the scroll speed of ScrollView programmatically,

 ObjectAnimator anim = ObjectAnimator.ofInt(mScrollView, "scrollY", mScrollView.getBottom());                               
 anim.setDuration(9000);                     
 anim.start();

mScrollView - 您的滚动型

mScrollView - Your ScrollView

mScrollView = (ScrollView) findViewById(R.id.scrollView1);

anima.setDuration(int值) - 数值越大,滚动速度较慢
我在切换按钮OnCheckedChangedListener使用code座。

anima.setDuration(int Value) - greater the value, slower the scroll I used the code block in Switch Button OnCheckedChangedListener.

这篇关于在Android中如何与不同的滚动速度滚动型同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆