应用程序背景中的视差效果

卡米尔

我是Android世界的新手。我想在我的应用程序中添加一些视差背景效果。

我该怎么做?如何在Android中解决此问题?

有什么有效的方法来创建2-3层视差背景吗?Android API中是否有某些工具或类?

还是我必须在代码中“手动”修改背景图像的位置或边距?

我正在使用API​​级别19。

我已经尝试了解Paralloid库,但是如果没有任何解释,它太大了以至于无法理解。我是Android和Java的新手,并不熟悉所有的Layouts和其他UI对象,但是我熟悉MVC。

我开始赏金,也许有人可以逐步解释该库的工作原理。

萨加尔·瓦格玛尔

您可以执行以下操作:

在活动/片段布局文件中,指定2个ScrollView的(例如background_sv和content_sv)。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.parallax.MyScrollView
        android:id="@+id/background_sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/parallax_bg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="..." />
    </com.example.parallax.MyScrollView>

    <com.example.parallax.MyScrollView
        android:id="@+id/content_sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </com.example.parallax.MyScrollView>

</RelativeLayout>

在背景高度的内容滚动视图中添加一个虚拟视图,并使其透明。现在,将滚动侦听器附加到content_sv。当内容滚动视图滚动时,调用

mBgScrollView.scrollTo(0, (int)(y /*scroll Of content_sv*/ / 2f));

现有的API不支持获取滚动事件。因此,我们需要创建一个Custom ScrollView,以提供ScrollViewListener。

package com.example.parallax;

// imports;

public class MyScrollView extends ScrollView {

    public interface ScrollViewListener {
        void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy);
    }

    private ScrollViewListener scrollViewListener = null;

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

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

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

    public void setScrollViewListener(ScrollViewListener 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);
        }
    }
}

这是承载内容ScrollView和后台ScrollView的活动

package com.example.parallax;

// imports;

public class ParallaxActivity extends Activity implements ScrollViewListener {

    private MyScrollView mBgScrollView;
    private MyScrollView mContentScrollView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        mBgScrollView = findViewById(R.id.background_sv);
        mContentScrollView = findViewById(R.id.content_sv);
        mContentScrollView.setOnScrollListener(this);
    }

    // this is method for onScrollListener put values according to your need
    @Override
    public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
        super.onScrollChanged(scrollView, x, y, oldx, oldy);
        // when the content scrollview will scroll by say 100px, 
        // the background scrollview will scroll by 50px. It will 
        // look like a parallax effect where the background is 
        // scrolling with a different speed then the content scrollview.
        mBgScrollView.scrollTo(0, (int)(y / 2f));
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS视差滚动效果(例如Yahoo News Digest应用程序中的效果)

来自分类Dev

Facebook News纸应用程序,全尺寸图像视差效果

来自分类Dev

在Android应用程序中以模糊效果设置背景图片

来自分类Dev

Windows Form应用程序中的滑动效果

来自分类Dev

具有视差效果的背景视频

来自分类Dev

视差效果使背景比其所在的容器更大

来自分类Dev

具有视差效果的背景视频

来自分类Dev

没有JS的背景视频视差效果

来自分类Dev

GPS在Cordova应用程序中的背景中坐标

来自分类Dev

React Native应用程序中的全屏背景图像

来自分类Dev

Xamarin.Mac中的应用程序背景

来自分类Dev

在应用程序委托中更改背景颜色

来自分类Dev

如何在Webstorms应用程序中删除烦人的背景

来自分类Dev

在UILongPressgestureRecognizer中处理应用程序运行背景

来自分类Dev

xCodeSimulator中的电晕应用程序的紫色背景

来自分类Dev

在Windows Phone应用程序中添加模糊背景

来自分类Dev

在android应用程序中播放背景声音

来自分类Dev

如何在SwiftUI macOS应用程序中模糊背景?

来自分类Dev

Android应用程序中的图像获取奇怪的背景渐变

来自分类Dev

xCodeSimulator中的电晕应用程序的紫色背景

来自分类Dev

在UILongPressgestureRecognizer中处理应用程序运行背景

来自分类Dev

在Windows Phone应用程序中添加模糊背景

来自分类Dev

在Android应用程序中设置背景图片

来自分类Dev

在uwp应用程序中具有透明背景的框架

来自分类Dev

Flutter 应用程序中屏幕底部的背景图像

来自分类Dev

将视差效果应用到 GridView

来自分类Dev

基于套接字连接的应用程序在Android 2.3中效果很好,但在Android 4.2中效果不佳

来自分类Dev

如何在ASP.NET Web应用程序中实现这种加载效果

来自分类Dev

如何在Windows Phone 8.1 RT应用程序的控件中添加倾斜效果?

Related 相关文章

  1. 1

    iOS视差滚动效果(例如Yahoo News Digest应用程序中的效果)

  2. 2

    Facebook News纸应用程序,全尺寸图像视差效果

  3. 3

    在Android应用程序中以模糊效果设置背景图片

  4. 4

    Windows Form应用程序中的滑动效果

  5. 5

    具有视差效果的背景视频

  6. 6

    视差效果使背景比其所在的容器更大

  7. 7

    具有视差效果的背景视频

  8. 8

    没有JS的背景视频视差效果

  9. 9

    GPS在Cordova应用程序中的背景中坐标

  10. 10

    React Native应用程序中的全屏背景图像

  11. 11

    Xamarin.Mac中的应用程序背景

  12. 12

    在应用程序委托中更改背景颜色

  13. 13

    如何在Webstorms应用程序中删除烦人的背景

  14. 14

    在UILongPressgestureRecognizer中处理应用程序运行背景

  15. 15

    xCodeSimulator中的电晕应用程序的紫色背景

  16. 16

    在Windows Phone应用程序中添加模糊背景

  17. 17

    在android应用程序中播放背景声音

  18. 18

    如何在SwiftUI macOS应用程序中模糊背景?

  19. 19

    Android应用程序中的图像获取奇怪的背景渐变

  20. 20

    xCodeSimulator中的电晕应用程序的紫色背景

  21. 21

    在UILongPressgestureRecognizer中处理应用程序运行背景

  22. 22

    在Windows Phone应用程序中添加模糊背景

  23. 23

    在Android应用程序中设置背景图片

  24. 24

    在uwp应用程序中具有透明背景的框架

  25. 25

    Flutter 应用程序中屏幕底部的背景图像

  26. 26

    将视差效果应用到 GridView

  27. 27

    基于套接字连接的应用程序在Android 2.3中效果很好,但在Android 4.2中效果不佳

  28. 28

    如何在ASP.NET Web应用程序中实现这种加载效果

  29. 29

    如何在Windows Phone 8.1 RT应用程序的控件中添加倾斜效果?

热门标签

归档