像视图中心的图像缩放库 [英] Gallery like view with center image zoom

查看:77
本文介绍了像视图中心的图像缩放库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我需要像视图中显示同时在屏幕上只有三张图片画廊。在此,中间图像将比在其两侧的两个其他图像更大。

Here I need a gallery like view with only three images to be shown at a time on screen. In this the middle image will be larger than the two other images on its sides.

如果用户滚动视图下一个图像上滑动屏幕,它做的画廊,并在只有三个图像将显示出其中的中心图像会自动放大时,它显示在屏幕上剩下的两个应该是一个时间比它小。

If the user scrolls the view next images will slide on screen as it does in gallery and at a time only three images will be shown out of which the center image should automatically zoom when it is shown on screen and remaining two should be smaller than it.

下面我不能使用画廊,因为它是pciated Android中去$ P $。

Here I can't use gallery because it is depreciated in android.

我可以用code在此链接。它显示只有三个在一个时间,它适合我的一个要求的图像在屏幕上。但我不能够得到中央图像在屏幕上可见,并放大。虽然我是能够得到在屏幕上点击的形象。

I was able to make a gallery like view with help of viewpager using code on this link. It shows only three images on screen at a time, which fits my one requirement. But i am not able to get the central image that is visible on screen and zoom it. Although I was able to get the clicked image on screen.

有人能告诉我在哪里,我需要修改 code和什么我需要在它添加到获取是从屏幕上显示的图像中心的形象,并放大。

Can someone please tell me where do I need to modify this code and what I need to add in it to get the image that is in center from the images shown on screen and zoom it.

我知道,有根据viewpager在屏幕上无中心的形象和它只是显示,因为在code修改屏幕上的三幅图像的时间。

我也曾尝试: -

  • 在GridView控件与水平滚动
  • Horizo​​ntalScrollView与水平线性布局

但viewpager似乎是一个更好的解决方案,因为它停止了滚动使用,因为viewpager的固有特性,只有三个项目在屏幕上。

but viewpager seems to be a better solution, because it stops the scrolling with only three items on screen because of viewpager's inherent properties.

在此先感谢您的帮助。

和如果有人知道任何其他方法来实现它,请告诉我,我会尽力的。

and If someone knows any other method to achieve it, please tell me and I'll try it.

PS 谁想要充分code,我将它作为一个答案,其中有变焦能力也。 只是一些补充的接受的答案的:)

P.S. For anyone who wants the full code, I have added it as an answer, which has zoom capability also. Just few additions in accepted answer. :)

推荐答案

覆盖<一href="http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#setPrimaryItem%28android.view.View,%20int,%20java.lang.Object%29">setPrimaryItem在ViewPager,使中心项目做大。

Override setPrimaryItem in your ViewPager and make the center item bigger.

什么是问题,使用Horizo​​ntalScrollView与LinearLayout中?如果它的中心,你可能能够做一些类似的(假设你

What was the issue with using a HorizontalScrollView with a LinearLayout? If it's centering you may be able to do something similar to this (assuming you've

/**
 * A centering HSV loosely based on http://iotasol.blogspot.com/2011/08/creating-custom-horizontal-scroll-view.html
 */
public class CenteringHorizontalScrollView extends HorizontalScrollView implements View.OnTouchListener {

    private static final int SWIPE_PAGE_ON_FACTOR = 10;

    private int mActiveItem;

    private float mPrevScrollX;

    private boolean mStart;

    private int mItemWidth;

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

        mItemWidth = 100; // or whatever your item width is.
        setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getRawX();

        boolean handled = false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                if (mStart) {
                    mPrevScrollX = x;
                    mStart = false;
                }

                break;
            case MotionEvent.ACTION_UP:
                mStart = true;
                int minFactor = mItemWidth / SWIPE_PAGE_ON_FACTOR;

                if ((mPrevScrollX - (float) x) > minFactor) {
                    if (mActiveItem < getMaxItemCount() - 1) {
                        mActiveItem = mActiveItem + 1;
                    }
                }
                else if (((float) x - mPrevScrollX) > minFactor) {
                    if (mActiveItem > 0) {
                        mActiveItem = mActiveItem - 1;
                    }
                }

                scrollToActiveItem();

                handled = true;
                break;
        }

        return handled;
    }

    private int getMaxItemCount() {
        return ((LinearLayout) getChildAt(0)).getChildCount();
    }

    private LinearLayout getLinearLayout() {
        return (LinearLayout) getChildAt(0);
    }

    /**
     * Centers the current view the best it can.
     */
    public void centerCurrentItem() {
        if (getMaxItemCount() == 0) {
            return;
        }

        int currentX = getScrollX();
        View targetChild;
        int currentChild = -1;

        do {
            currentChild++;
            targetChild = getLinearLayout().getChildAt(currentChild);
        } while (currentChild < getMaxItemCount() && targetChild.getLeft() < currentX);

        if (mActiveItem != currentChild) {
            mActiveItem = currentChild;
            scrollToActiveItem();
        }
    }

    /**
     * Scrolls the list view to the currently active child.
     */
    private void scrollToActiveItem() {
        int maxItemCount = getMaxItemCount();
        if (maxItemCount == 0) {
            return;
        }

        int targetItem = Math.min(maxItemCount - 1, mActiveItem);
        targetItem = Math.max(0, targetItem);

        mActiveItem = targetItem;

        // Scroll so that the target child is centered
        View targetView = getLinearLayout().getChildAt(targetItem);

        int targetLeft = targetView.getLeft();
        int childWidth = targetView.getRight() - targetLeft;

        int width = getWidth() - getPaddingLeft() - getPaddingRight();
        int targetScroll = targetLeft - ((width - childWidth) / 2);

        super.smoothScrollTo(targetScroll, 0);
    }

    /**
     * Sets the current item and centers it.
     * @param currentItem The new current item.
     */
    public void setCurrentItemAndCenter(int currentItem) {
        mActiveItem = currentItem;
        scrollToActiveItem();
    }
}

这篇关于像视图中心的图像缩放库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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