Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 应用程序背景中的Paralax效果大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 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://scheR_357_11845@as.android.com/apk/res/android"
    xmlns:tools="http://scheR_357_11845@as.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.滚动内容滚动视图时,请调用

@H_356_20@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 oldy) {
        super.onScrollChanged(x,y,oldx,oldy);
        if(scrollViewListener != null) {
            scrollViewlistener.onScrollChanged(this,x,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 oldy) {
        super.onScrollChanged(scrollView,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));
    }
}

大佬总结

以上是大佬教程为你收集整理的android – 应用程序背景中的Paralax效果全部内容,希望文章能够帮你解决android – 应用程序背景中的Paralax效果所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。