一个可以一直滚动的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

相关文章

html 滚动条滚动过快会留下边框线的解决方案

《html滚动条滚动过快会留下边框线的解决方案》:本文主要介绍了html滚动条滚动过快会留下边框线的解决方案,解决方法很简单,详细内容请阅读本文,希望能对你有所帮助... 滚动条滚动过快时,会留下边框线但其实大部分时候是这样的,没有多出边框线的滚动条滚动过快时留下边框线的问题通常与滚动条样式和滚动行

Kotlin Compose Button 实现长按监听并实现动画效果(完整代码)

《KotlinComposeButton实现长按监听并实现动画效果(完整代码)》想要实现长按按钮开始录音,松开发送的功能,因此为了实现这些功能就需要自己写一个Button来解决问题,下面小编给大... 目录Button 实现原理1. Surface 的作用(关键)2. InteractionSource3.

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)

《Vue项目的甘特图组件之dhtmlx-gantt使用教程和实现效果展示(推荐)》文章介绍了如何使用dhtmlx-gantt组件来实现公司的甘特图需求,并提供了一个简单的Vue组件示例,文章还分享了一... 目录一、首先 npm 安装插件二、创建一个vue组件三、业务页面内 引用自定义组件:四、dhtmlx

禁止HTML页面滚动的操作方法

《禁止HTML页面滚动的操作方法》:本文主要介绍了三种禁止HTML页面滚动的方法:通过CSS的overflow属性、使用JavaScript的滚动事件监听器以及使用CSS的position:fixed属性,每种方法都有其适用场景和优缺点,详细内容请阅读本文,希望能对你有所帮助... 在前端开发中,禁止htm