HorizontalScrollView with imageviews as a child and while scrolling the center of the image in the screen should play animation?

这一生的挚爱 提交于 2019-12-08 12:43:57

问题


I am new android platform app development. my problem is center lock the image without using gallery view and that image we play an animation.while scrolling animated image should be adjust with center of screen. Currently i am using HorizontalScrollView with linearlayout as child elment of HSV. linear layout having multiple images. Please give me advices how do acheive it.Thanks in advance.


回答1:


Inherit the HorizontalScrollView and override/add the Methods

EDITED

My Complete Source

public class HorizontalScrollSpinner extends HorizontalScrollView {

    private ListAdapter mAdapter;

    private int mCenterViewPosition = -1;

    private OnSelectedItemChanged onSelectedItemChanged = new OnSelectedItemChanged() {
        @Override
        public void onSelectedChanged(View view, int newPosition) {

        }
    };

    public HorizontalScrollSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setHorizontalFadingEdgeEnabled(true);
        this.setHorizontalScrollBarEnabled(false);
        this.setFadingEdgeLength(5);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        if(getChildCount() == 0)
            return;

        initCenterView();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if(getChildCount() == 0)
            return;

        ViewGroup parent = (ViewGroup) getChildAt(0);

        if(parent.getChildCount() == 0)
            return;

        View FirstChild = parent.getChildAt(0);

        int LeftPadding = (getWidth() / 2) - (FirstChild.getMeasuredWidth() / 2);

        View LastChild = parent.getChildAt(getChildCount() - 1);

        int RightPadding = (getWidth() / 2) - (LastChild.getMeasuredWidth() / 2);

        if(parent.getPaddingLeft() != LeftPadding && parent.getPaddingRight() != RightPadding)
        {
            parent.setPadding(LeftPadding, parent.getPaddingTop(), RightPadding, parent.getPaddingBottom());
            requestLayout();
        }
    }

    private int getInternalCenterView()
    {
        if(getChildCount() == 0)
            return -1;

        int CenterView= 0;
        int CenterX = getScrollX() + (getWidth() / 2); 

        ViewGroup parent = (ViewGroup) getChildAt(0);

        if(parent.getChildCount() == 0)
            return -1;

        View child = parent.getChildAt(0);

        while(child != null && child.getRight() <= CenterX && CenterView < parent.getChildCount())
        {
            CenterView++;
            child = parent.getChildAt(CenterView);
        }

        if(CenterView >= parent.getChildCount())
            CenterView = parent.getChildCount() - 1;

        return CenterView;
    }

    private int getCenterPositionFromView()
    {
        int CenterView = getInternalCenterView();

        if(mCenterViewPosition != CenterView)
        {
            onSelectedItemChanged.onSelectedChanged(this, CenterView);
        }

        mCenterViewPosition = CenterView;

        return mCenterViewPosition;
    }

    public int getCenterViewPosition()
    {
        return mCenterViewPosition;
    }

    public ListAdapter getAdapter() {
        return mAdapter;
    }

    public void setAdapter(ListAdapter mAdapter) {

        this.mAdapter = mAdapter;
        fillViewWithAdapter();      
    }

    private void fillViewWithAdapter()
    {
        if(getChildCount() == 0 || mAdapter == null)
            return;

        ViewGroup parent = (ViewGroup) getChildAt(0);

        parent.removeAllViews();

        for (int i = 0; i < mAdapter.getCount(); i++) {
            parent.addView(mAdapter.getView(i, null, parent));          
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        getCenterPositionFromView();

        initCenterView();
    }

    private void initCenterView()
    {
        if(getChildCount() == 0)
            return;

        ViewGroup parent = (ViewGroup) getChildAt(0);

        if(parent.getChildCount() == 0)
            return;

        int CenterView = getCenterViewPosition();

        if(CenterView == -1)
        {
            mCenterViewPosition = 0;
            CenterView = 0;
        }

        if(CenterView != -1 && CenterView != getInternalCenterView() && parent.getChildAt(0).getLeft() >= 0)
        {
            scrollToSelectedIndex();
        }

        if(CenterView < 0 || CenterView > parent.getChildCount())
            return;

        for (int i = 0; i <= parent.getChildCount(); i++) {

            if(!(parent.getChildAt(i) instanceof TextView))
                continue;

            if(i == CenterView)
            {
                // Start Animation
            }
            else
            {
                // Remove Animation for other Views
            }
        }


    }

    public int getSelectedIndex()
    {
        return getCenterViewPosition();
    }

    public void setSelectedIndex(int index)
    {
        if(getChildCount() == 0)
            return;

        ViewGroup parent = (ViewGroup) getChildAt(0);

        if(index < 0 || index > parent.getChildCount())
        {
            throw new ArrayIndexOutOfBoundsException(index);
        }

        mCenterViewPosition = index;

        onSelectedItemChanged.onSelectedChanged(this, mCenterViewPosition);

        requestLayout();
    }

    protected void scrollToSelectedIndex()
    {
        ViewGroup parent = (ViewGroup) getChildAt(0);

        View child = parent.getChildAt(mCenterViewPosition);

        int ChildCenterX = child.getLeft() + (child.getMeasuredWidth() / 2);

        int ScreenCenterX = getWidth() / 2;

        int ChildScrollToX = ChildCenterX - ScreenCenterX;


        scrollTo(ChildScrollToX, 0);
    }

    public interface OnSelectedItemChanged
    {
        public void onSelectedChanged(View view, int newPosition);
    }

    public OnSelectedItemChanged getOnSelectedItemChanged() {
        return onSelectedItemChanged;
    }

    public void setOnSelectedItemChanged(OnSelectedItemChanged onSelectedItemChanged) {
        this.onSelectedItemChanged = onSelectedItemChanged;
    }


}

This will show Animation to the View which are at center

onMeasure adds Padding to LinearLayout so that the First Child and Last Child in LinearLayout will always be at center.



来源:https://stackoverflow.com/questions/11345673/horizontalscrollview-with-imageviews-as-a-child-and-while-scrolling-the-center-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!