Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Parallax Scroll in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章将为大家详细讲解有关Android中如何实现视差滚动,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

什么是视差滚动?

视差滚动原本是一个天文学术语,当我们观察星空的时候,离我们比较远的星星移动速度比较慢,离我们比较近的星星移动速度比较快,当我们坐在车上向车窗外看的时候也会有这种体验,远处的群山似乎没有移动,但近处的行道树却在飞速掠过。

在工程设计中,视差滚动是指通过为背景图像设定比前景图像更慢的移动速度模拟现实世界中人类的视觉体验,从而在 2D 场景中产生深度的错觉,增加沉浸感。

以下是几个设计实例:

如何在 Android 中实现视差滚动?

首先创建一个新项目

新建 Android project

选择 Empty Activity

Name:ParallaxAndroid

Package name:com.example.parallaxandroid

Language:Kotlin

然后添加需要的依赖:

implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"implementation 'androidx.recyclerview:recyclerview:1.1.0'implementation 'com.google.android.material:material:1.2.0-alpha06'

此处以创建一个具有视差滚动效果的展示书籍磁贴的页面为例。

首先从 XML 布局开始。

在 main activity XML 中添加 collapsing toolbar layout,collapsing toolbar layout 类似 FrameLayout,所有最后加入的元素都将被放置在顶部。这种放置方式对实现视差滚动非常重要。

activity_main.xml 文件如下:

toolbar layout:

在上面的布局中,我们添加了这些属性:CollapsingToolbarLayout

app:layout_scrollFlags="scroll|exitUntilCollapsed"

ImageView

app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"app:layout_collapseMode="parallax"

Toolbar

app:layout_scrollFlags="scroll|enterAlways"

接下来配置 ManActivity 文件。

class MainActivity : FragmentActivity() { private lateinit var mPager: ViewPager private lateinit var tabLayout : TabLayout override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) mPager = findViewById(R.id.viewPager) tabLayout = findViewById(R.id.tabs) tabLayout.setupWithViewPager(mPager) val pagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager) mPager.adapter = pagerAdapter } override fun onBackPressed() { if (mPager.currentItem == 0) { // If the user is currently looking at the first step, allow the system to handle the // Back button. This calls finish() on this activity and pops the back stack. super.onBackPressed() } else { // Otherwise, select the previous step. mPager.currentItem = mPager.currentItem - 1 } } private inner class ScreenSlidePagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { override fun getCount(): Int = 3 override fun getItem(position: Int): Fragment = BooksFragment().newInstance() override fun getPageTitle(position: Int): CharSequence? { var title = "" when(position) { 0 -> title ="Tech" 1 -> title = "Novels" 2 -> title = "Motivational" } return title } } }

创建用来加载 Recycleview 的 fragment:

class BooksFragment : Fragment() { fun newInstance(): BooksFragment { return BooksFragment() } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view : View? = inflater.inflate(R.layout.books_fragment, container, false) val rvBooks : RecyclerView = view!!.findViewById(R.id.rvBooksList) rvBooks.layoutManager = LinearLayoutManager(activity); val recyclerAdapter = BooksRecyclerAdapter(Util().getBooks()) rvBooks.adapter = recyclerAdapter return view } }

然后需要创建一个用来加载所有元素的 adapter。

class BooksRecyclerAdapter(privateval mBooks: List) : RecyclerView.Adapter() { inner class ViewHolder(listItemView: View) : RecyclerView.ViewHolder(listItemView) { val titleTextView: TextView = itemView.findViewById(R.id.text_title) val authorTextView: TextView = itemView.findViewById(R.id.text_author) val subTitleTextView: TextView = itemView.findViewById(R.id.text_subtitle) } override fun onCreateViewHolder( parent: ViewGroup, viewType: Int): ViewHolder { val context: Context = parent.context val inflater = LayoutInflater.from(context) val booksView: View = inflater.inflate(R.layout.item_books, parent, false) return ViewHolder(booksView) } override fun onBindViewHolder( viewHolder: ViewHolder, position: Int) { val titleTextView: TextView = viewHolder.titleTextView titleTextView.text = mBooks[position].titleval authorTextView: TextView = viewHolder.authorTextView authorTextView.text = mBooks[position].author val subTitleTextView: TextView = viewHolder.subTitleTextView subTitleTextView.text = mBooks[position].subtitle } override fun getItemCount(): Int { return mBooks.size } }

以上是主要的 Kotlin 文件和 layout 文件。

在 toolbar layout 的 ImageView 中可以设置滚动速度和其它属性。

关于"Android中如何实现视差滚动"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report