首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >app背景下的并行效应

app背景下的并行效应
EN

Stack Overflow用户
提问于 2014-10-04 13:52:30
回答 7查看 19.8K关注 0票数 9

我是机器人世界的新成员。我想把一些视差背景效果在我的应用程序。

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

是否有任何有效的方法来创建2-3层视差背景?在android中有什么工具或类吗?

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

Im使用API级别19.

我试着去理解Paralloid库,但这太大了,没有任何解释就无法理解。我是Android和Java的新手,我并不熟悉所有的布局和其他UI对象,但是我对MVC很熟悉。

我开始赏金了,也许有人能一步一步地解释一下图书馆是如何运作的。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2014-10-08 20:38:28

这就是你能做的:

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

代码语言:javascript
复制
<?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。当滚动内容滚动视图时,调用

代码语言:javascript
复制
mBgScrollView.scrollTo(0, (int)(y /*scroll Of content_sv*/ / 2f));

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

代码语言:javascript
复制
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的活动

代码语言:javascript
复制
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));
    }
}
票数 13
EN

Stack Overflow用户

发布于 2014-10-08 21:51:51

我认为这个问题还不清楚,所以这并不是一个真正的答案,而是试图用比我在评论中所能包括的更多的细节来澄清。

我的问题是你想要达到什么样的视差效果。给出这三个例子(它们是你可以从Play Store安装的演示应用程序),如果有的话,哪一个有你想要的视差效果?请在评论中回答。

给出答案,我们都会发现帮助更容易。如果您编辑您的问题,以包括这些信息,它将得到改善。

票数 4
EN

Stack Overflow用户

发布于 2014-10-07 19:28:32

以下是Paralloid作者发布的示例应用程序

https://github.com/chrisjenx/Paralloid/tree/master/paralloidexample

来自“入门”部分下的GitHub页面:

布局 ScrollView 这是一个例子,请参考paralloidexample获得完整的代码。 match_parent android:id=”@+id/滚动视图“android:layout_width="match_parent”android:layout_height="match_parent“android:fillViewport=”true> />” 片段 在你的onViewCreated()onCreateView()里面。 //.FrameLayout topContent = (FrameLayout) rootView.findViewById(R.id.top_content);scrollView ScrollView = (ScrollView) rootView.findViewById(R.id.scroll_view);if ( Parallaxor) { ((Parallaxor) scrollView).parallaxViewBy(topContent,0.5f);} // TODO:向顶部/滚动内容中添加内容 就这样! 查看一下适用的视差方法的Parallaxor接口。

希望这能有所帮助!

此外,这里也是谷歌安卓“入门”页面的链接。

此外,这里是一个链接到'java教程的完全初学者‘。

以及一些关于布局的文档(“定义用户界面的可视化结构”),以及链接

尽管如此,您可以使用布局来定义接口的样子,并使用后续的示例代码来定义与接口交互时发生的情况。

在行动这里中可以看到应用程序

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26193475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档