一个可以一直滚动的ImageView(可做视差效果)

2024-09-04 18:08
文章标签 效果 滚动 imageview 视差

本文主要是介绍一个可以一直滚动的ImageView(可做视差效果),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!




Java代码   收藏代码
  1. import android.content.Context;  
  2. import android.content.res.TypedArray;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Rect;  
  7. import android.util.AttributeSet;  
  8. import android.view.View;  
  9.   
  10. import static java.lang.Math.abs;  
  11. import static java.lang.Math.floor;  
  12.   
  13. /** 
  14.  * Created by thijs on 08-06-15. 
  15.  */  
  16. public class ScrollingImageView extends View {  
  17.     private final int speed;  
  18.     private final Bitmap bitmap;  
  19.   
  20.     private Rect clipBounds = new Rect();  
  21.     private int offset = 0;  
  22.   
  23.     private boolean isStarted;  
  24.   
  25.     public ScrollingImageView(Context context, AttributeSet attrs) {  
  26.         super(context, attrs);  
  27.         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ParallaxView, 00);  
  28.         try {  
  29.             speed = ta.getDimensionPixelSize(R.styleable.ParallaxView_speed, 10);  
  30.             bitmap = BitmapFactory.decodeResource(getResources(), ta.getResourceId(R.styleable.ParallaxView_src, 0));  
  31.         } finally {  
  32.             ta.recycle();  
  33.         }  
  34.         start();  
  35.     }  
  36.   
  37.     @Override  
  38.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  39.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  40.         setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bitmap.getHeight());  
  41.     }  
  42.   
  43.     @Override  
  44.     public void onDraw(Canvas canvas) {  
  45.         super.onDraw(canvas);  
  46.         if (canvas == null) {  
  47.             return;  
  48.         }  
  49.   
  50.         canvas.getClipBounds(clipBounds);  
  51.   
  52.         int normalizedOffset = offset;  
  53.         int layerWidth = bitmap.getWidth();  
  54.         if (offset < -layerWidth) {  
  55.             offset += (int) (floor(abs(normalizedOffset) / (float) layerWidth) * layerWidth);  
  56.         }  
  57.   
  58.         int left = offset;  
  59.         while (left < clipBounds.width()) {  
  60.             canvas.drawBitmap(bitmap, getBitmapLeft(layerWidth, left), 0null);  
  61.             left += layerWidth;  
  62.         }  
  63.   
  64.         if (isStarted) {  
  65.             offset -= speed;  
  66.             postInvalidateOnAnimation();  
  67.         }  
  68.     }  
  69.   
  70.     private float getBitmapLeft(int layerWidth, int left) {  
  71.         float bitmapLeft = left;  
  72.         if (speed < 0) {  
  73.             bitmapLeft = clipBounds.width() - layerWidth - left;  
  74.         }  
  75.         return bitmapLeft;  
  76.     }  
  77.   
  78.     /** 
  79.      * Start the animation 
  80.      */  
  81.     public void start() {  
  82.         if (!isStarted) {  
  83.             isStarted = true;  
  84.             postInvalidateOnAnimation();  
  85.         }  
  86.     }  
  87.   
  88.     /** 
  89.      * Stop the animation 
  90.      */  
  91.     public void stop() {  
  92.         if (isStarted) {  
  93.             isStarted = false;  
  94.             invalidate();  
  95.         }  
  96.     }  
  97. }  


attr.xml
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="ParallaxView">  
  4.         <attr name="speed" format="dimension" />  
  5.         <attr name="src" format="reference" />  
  6.     </declare-styleable>  
  7. </resources>  


usage:
Xml代码   收藏代码
  1. <com.....ScrollingImageView  
  2. xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     android:id="@+id/scrolling_background"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="wrap_content"  
  6.     app:speed="1dp"  
  7.     app:src="@drawable/scrolling_background" />  


Java代码   收藏代码
  1. ScrollingImageView scrollingBackground = (ScrollingImageView) loader.findViewById(R.id.scrolling_background);  
  2. scrollingBackground.stop();  
  3. scrollingBackground.start();  


Parallax effect:
只要设置不同的滚动背景速率就可以达到视差效果
Java代码   收藏代码
  1. <FrameLayout  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="wrap_content">  
  4.   
  5.   <com......ScrollingImageView  
  6.       android:id="@+id/scrolling_background"  
  7.       android:layout_width="match_parent"  
  8.       android:layout_height="wrap_content"  
  9.       app:speed="1dp"  
  10.       app:src="@drawable/scrolling_background" />  
  11.   
  12.   <com.....ScrollingImageView  
  13.       android:id="@+id/scrolling_foreground"  
  14.       android:layout_width="match_parent"  
  15.       android:layout_height="wrap_content"  
  16.       app:speed="2.5dp"  
  17.       app:src="@drawable/scrolling_foreground" />  
  18. </FrameLayout>  
  • 查看图片附件

这篇关于一个可以一直滚动的ImageView(可做视差效果)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136649

相关文章

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

React实现原生APP切换效果

《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

使用Python实现生命之轮Wheel of life效果

《使用Python实现生命之轮Wheeloflife效果》生命之轮Wheeloflife这一概念最初由SuccessMotivation®Institute,Inc.的创始人PaulJ.Meyer... 最近看一个生命之轮的视频,让我们珍惜时间,因为一生是有限的。使用python创建生命倒计时图表,珍惜时间

防近视护眼台灯什么牌子好?五款防近视效果好的护眼台灯推荐

在家里,灯具是属于离不开的家具,每个大大小小的地方都需要的照亮,所以一盏好灯是必不可少的,每个发挥着作用。而护眼台灯就起了一个保护眼睛,预防近视的作用。可以保护我们在学习,阅读的时候提供一个合适的光线环境,保护我们的眼睛。防近视护眼台灯什么牌子好?那我们怎么选择一个优秀的护眼台灯也是很重要,才能起到最大的护眼效果。下面五款防近视效果好的护眼台灯推荐: 一:六个推荐防近视效果好的护眼台灯的

Imageview在百度地图中实现点击事件

1.首先第一步,需要声明的全局有关类的引用 private BMapManager mBMapMan; private MapView mMapView; private MapController mMapController; private RadioGroup radiogroup; private RadioButton normalview; private RadioBu

【Godot4.3】多边形的斜线填充效果基础实现

概述 图案(Pattern)填充是一个非常常见的效果。其中又以斜线填充最为简单。本篇就探讨在Godot4.3中如何使用Geometry2D和CanvasItem的绘图函数实现斜线填充效果。 基础思路 Geometry2D类提供了多边形和多边形以及多边形与折线的布尔运算。按照自然的思路,多边形的斜线填充应该属于“多边形与折线的布尔运算”范畴。 第一个问题是如何获得斜线,这条斜线应该满足什么样

UniApp实现漂亮的音乐歌词滚动播放效果

在现代的音乐播放应用中,歌词的展示和滚动播放已经成为了一个非常常见的功能。今天,我们将通过UniApp来实现一个漂亮的歌词滚动播放功能。我们将使用UniApp提供的组件和API来完成这个任务。 页面结构 在页面的模板部分,我们需要创建一个音频播放器和歌词展示区域。使用<scroll-view>组件来实现歌词的滚动效果。 <template><view class="audio-co

Nuxt3入门:过渡效果(第5节)

你好同学,我是沐爸,欢迎点赞、收藏、评论和关注。 Nuxt 利用 Vue 的 <Transition> 组件在页面和布局之间应用过渡效果。 一、页面过渡效果 你可以启用页面过渡效果,以便对所有页面应用自动过渡效果。 nuxt.config.js export default defineNuxtConfig({app: {pageTransition: {name: 'fade',mode