与imageviews作为一个孩子Horizo​​ntalScrollView虽然滚动图像的中心位于屏幕应该扮演动画? [英] HorizontalScrollView with imageviews as a child and while scrolling the center of the image in the screen should play animation?

查看:174
本文介绍了与imageviews作为一个孩子Horizo​​ntalScrollView虽然滚动图像的中心位于屏幕应该扮演动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的android平台的应用开发。我的问题是中心锁定图像,而无需使用库视图和图像,我们发挥animation.while滚动动画形象应该与屏幕中心进行调整。目前我使用Horizo​​ntalScrollView用的LinearLayout作为HSV的孩子elment。有多个图像的线性布局。请给我意见如何提前做acheive it.Thanks。

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.

推荐答案

继承了Horizo​​ntalScrollView
和覆盖/添加方法

Inherit the HorizontalScrollView and override/add the Methods

EDITED

我的完整源代码

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添加填充到的LinearLayout这样在LinearLayout中的第一个孩子,并最后一个孩子将永远在中央。

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

这篇关于与imageviews作为一个孩子Horizo​​ntalScrollView虽然滚动图像的中心位于屏幕应该扮演动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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