【问题标题】:Imageview parallax as you scroll滚动时的 Imageview 视差
【发布时间】:2019-12-09 07:58:02
【问题描述】:

我看到了许多示例,在您滚动或列表视图视差时显示视差背景,但我找不到一个清晰的示例,如何在您在活动中滚动时对图像实现视差效果。

可以在 Airbnb 应用中找到示例实现。当您向下滚动时,您可以看到更多图像底部,当您向上滚动时,您可以看到更多图像顶部。

关于如何创建这种效果的任何提示和技巧?

【问题讨论】:

标签: android


【解决方案1】:

有一些库可以产生视差效果,这取决于您的应用程序是否对您的特定情况有用,例如:

Google 是您的朋友;)如果这些都不适合您的需求,那么您必须创建一个自定义 ScrollView,但这是一个较长的故事,首先尝试一下并发布您的结果。

编辑

如果这些都不符合您的要求,那么您必须这样做:

首先,创建一个自定义ScrollView,以便您可以监听滚动变化。

public class ObservableScrollView extends ScrollView {

    public interface OnScrollChangedListener {
        public void onScrollChanged(int deltaX, int deltaY);
    }

    private OnScrollChangedListener mOnScrollChangedListener;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if(mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(l - oldl, t - oldt);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }
}

显然你需要在你的布局中使用它而不是默认的ScrollView

<your.app.package.ObservableScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

您还需要将 ImageView 包装在容器中以使视差起作用:

<FrameLayout
    android:id="@+id/img_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />
</FrameLayout>

最后将您的Activity 设置为您全新的ObservableScrollView 的侦听器,然后开始视差:

public class MyActivity extends Activity implements ObservableScrollView.OnScrollChangedListener {
    private ObservableScrollView mScrollView;
    private View imgContainer;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Init your layout and set your listener
        mScrollView = (ObservableScrollView)findViewById(R.id.scroll_view);
        mScrollView.setOnScrollChangedListener(this);
        // Store the reference of your image container
        imgContainer = findViewById(R.id.img_container);
    }

     @Override
    public void onScrollChanged(int deltaX, int deltaY) {
        int scrollY = mScrollView.getScrollY();
        // Add parallax effect
        imgContainer.setTranslationY(scrollY * 0.5f);
    }
}

您可以根据需要多少视差来修改 0.5 的值。

编辑

如果您的 ImageView 位于活动的顶部,则上述答案可以正常工作。我在下面发布了一些代码,以添加功能以使 ImageView 在我成功工作的活动布局中的任何位置。这些是通用计算(可能有一些错误),只需稍加调整,您就可以让它适用于您自己的情况。

对于这个例子,我为图像容器 200dp 和图像 240dp 设置了一个固定高度。主要目的是当图像容器位于屏幕中间时没有视差效果,并且当用户向上或向下滚动以应用该效果时。因此,当图像容器靠近屏幕顶部或靠近屏幕底部时,将应用更多的视差效果。下面的计算读起来有点难理解,所以试着用纸上的实数做一个例子。

public class MyActivity extends Activity implements ObservableScrollView.OnScrollChangedListener {
    private ObservableScrollView mScrollView;
    private View imgContainer;
    private ImageView mImageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // Init your layout and set your listener
        mScrollView = (ObservableScrollView)findViewById(R.id.scroll_view);
        mScrollView.setOnScrollChangedListener(this);
        // Store the reference of your image container
        imgContainer = findViewById(R.id.img_container);
        // Store the reference of your image
        mImageView =  findViewById(R.id.img);
    }

     @Override
    public void onScrollChanged(int deltaX, int deltaY) {
        // Get scroll view screen bound
        Rect scrollBounds = new Rect();
        mScrollView.getHitRect(scrollBounds);

        // Check if image container is visible in the screen
        // so to apply the translation only when the container is visible to the user
        if (imgContainer.getLocalVisibleRect(scrollBounds)) {
            Display display = getWindowManager().getDefaultDisplay();
            DisplayMetrics outMetrics = new DisplayMetrics ();
            display.getMetrics(outMetrics);
            // Get screen density
            float density  = getResources().getDisplayMetrics().density;

            // Get screen height in pixels            
            float dpHeight = outMetrics.heightPixels / density;
            int screen_height_pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpHeight, getResources().getDisplayMetrics());
            int half_screen_height = screen_height_pixels/2;

            // Get image container height in pixels
            int container_height_pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());

            // Get the location that consider a vertical center for the image (where the translation should be zero)
            int center = half_screen_height - (container_height_pixels/2);

            // get the location (x,y) of the image container in pixels
            int[] loc_screen = {0,0};
            imgContainer.getLocationOnScreen(loc_screen);

            // trying to transform the current image container location into percentage
            // so when the image container is exaclty in the middle of the screen percentage should be zero
            // and as the image container getting closer to the edges of the screen should increase to 100%
            int final_loc = ((loc_screen[1]-center)*100)/half_screen_height;

            // translate the inner image taking consideration also the density of the screen
            mImageView.setTranslationY(-final_loc * 0.4f * density);
        }
    }
}

我希望它可以帮助正在寻找类似功能的人。

【讨论】:

  • 感谢您的回答。我已经看到了它们,但是因为我无法清楚地看到我正在寻找的功能,所以我决定问一下,以防我错过了一个图书馆!但你说得对,我必须花时间先尝试一下。
  • @xinaris 有用吗?如果是这样,请接受我的帖子作为答案:)
  • 感谢您为我提供的帮助!实际上,我有一个类似的代码来为我在活动顶部的图像创建视差效果,效果很好。我的问题实际上是对滚动视图中任何位置的图像视图产生视差效果。您的代码不适用于此,但您的指导方针很好。该解决方案将类似于您的代码,但需要进行一些数学计算来计算滚动视图中图像视图的当前位置。
  • 我向@peguerosdc anwser 添加了一些代码,以获得我要求的功能。感谢您花在这方面的时间。
  • @xinaris 我已接受您的编辑。我很高兴看到我的代码很有用。
【解决方案2】:
   scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            int top = scrollView.getScrollY(); // Increases when scrolling up ^
            if(top != 0) {
                int newTop = (int) (top * .5f);
                imageFrame.setTop(newTop < 0 ? 0 : newTop);
            }
        }
    });

【讨论】:

  • 这会在快速移动时在ImageView 顶部产生间隙。
  • 删除top != 0检查将修复快速滚动时显示在顶部的间隙。
  • 移除顶部!= 0,快速移动时仍然会在 ImageView 顶部产生间隙。还有其他解决方案吗?
猜你喜欢
  • 2013-09-05
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 2016-01-23
相关资源
最近更新 更多