├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── ziwenl
│ │ └── srcscrollframelayout
│ │ └── MainActivity.kt
│ └── res
│ ├── drawable-v24
│ └── ic_launcher_foreground.xml
│ ├── drawable
│ ├── ic_launcher_background.xml
│ └── shape_btn_bg.xml
│ ├── layout
│ └── activity_main.xml
│ ├── mipmap-anydpi-v26
│ ├── ic_launcher.xml
│ └── ic_launcher_round.xml
│ ├── mipmap-hdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-mdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ ├── mipmap-xxhdpi
│ ├── ic_launcher.png
│ ├── ic_launcher_round.png
│ ├── login_icon_red_logo_login.png
│ ├── login_longpic_background.webp
│ └── xhswebview_icon_logo.png
│ ├── mipmap-xxxhdpi
│ ├── ic_launcher.png
│ └── ic_launcher_round.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── library
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── ziwenl
│ │ └── library
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── ziwenl
│ │ │ └── library
│ │ │ └── widgets
│ │ │ ├── SrcLoopScrollFrameLayout.kt
│ │ │ └── SrcScrollFrameLayout.java
│ └── res
│ │ └── values
│ │ └── attrs.xml
│ └── test
│ └── java
│ └── com
│ └── ziwenl
│ └── library
│ └── ExampleUnitTest.kt
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SrcScrollFrameLayout 博客地址
2 |
基本介绍
3 | 仿小红书登陆页面背景图无限滚动 FrameLayout
4 |
5 |
显示效果
6 |
7 |
8 | 属性说明
9 | 自定义属性
10 |
11 | |name|format|default|required|description|
12 | |:---:|:---:|:---:|:---:|:---:|
13 | | src | reference | | false |设置图片背景
14 | | maskLayerColor | color | | false | 设置遮罩层颜色,建议带透明度
15 | | isScroll | boolean | true | false | 设置是否滚动
16 | | speed | integer | ordinary |false| 滚动速度 slow、ordinary、fast ,也可手动赋值 ( 建议取值区间 [1,50] )
17 | | scrollOrientation| integer | toTop |false| 滚动方向 toTop、toBottom、toLeft、toRight ,默认是上移
18 |
19 | 自定义方法
20 |
21 | |name|description|
22 | |:---:|:---:|
23 | | setSrcBitmap (Bitmap srcBitmap) | 设置背景图 bitmap |
24 | | startScroll ( ) | 开始滚动 |
25 | | stopScroll ( ) | 停止滚动 |
26 | | changeScrollOrientation ( ) | 切换滚动方向 , 从上移、下移、左移和右移按顺序切换 |
27 | | setScrollOrientation (@ScrollOrientation int scrollOrientation) | 设置滚动方向 |
28 |
29 | 使用方法
30 |
38 |
39 | About Me
40 |
45 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 |
5 | android {
6 | compileSdkVersion 29
7 | buildToolsVersion "29.0.2"
8 |
9 | defaultConfig {
10 | applicationId "com.ziwenl.srcscrollframelayout"
11 | minSdkVersion 19
12 | targetSdkVersion 29
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 |
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31 | implementation 'androidx.appcompat:appcompat:1.1.0'
32 | implementation 'androidx.core:core-ktx:1.2.0'
33 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
34 | testImplementation 'junit:junit:4.12'
35 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
36 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
37 |
38 | implementation project(':library')
39 | }
40 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ziwenl/srcscrollframelayout/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.ziwenl.srcscrollframelayout
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.view.WindowManager
6 | import kotlinx.android.synthetic.main.activity_main.*
7 |
8 | class MainActivity : AppCompatActivity() {
9 |
10 | override fun onCreate(savedInstanceState: Bundle?) {
11 | super.onCreate(savedInstanceState)
12 | window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
13 | setContentView(R.layout.activity_main)
14 |
15 | btn_start.setOnClickListener {
16 | fl_main.startScroll()
17 | }
18 | btn_stop.setOnClickListener {
19 | fl_main.stopScroll()
20 | }
21 | btn_change.setOnClickListener {
22 | fl_main.changeScrollOrientation()
23 | }
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/shape_btn_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
17 |
18 |
26 |
27 |
35 |
36 |
44 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/login_icon_red_logo_login.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xxhdpi/login_icon_red_logo_login.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/login_longpic_background.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xxhdpi/login_longpic_background.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/xhswebview_icon_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xxhdpi/xhswebview_icon_logo.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFFFFF
4 | #FFFFFF
5 | #03DAC5
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SrcScrollFrameLayout
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.3.71'
5 | repositories {
6 | google()
7 | jcenter()
8 |
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.6.0'
12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | google()
22 | jcenter()
23 |
24 | }
25 | }
26 |
27 | task clean(type: Delete) {
28 | delete rootProject.buildDir
29 | }
30 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
22 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed May 13 11:21:15 CST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 |
5 | android {
6 | compileSdkVersion 29
7 | buildToolsVersion "29.0.2"
8 |
9 | defaultConfig {
10 | minSdkVersion 19
11 | targetSdkVersion 29
12 | versionCode 1
13 | versionName "1.0"
14 |
15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16 | consumerProguardFiles 'consumer-rules.pro'
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 |
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31 | implementation 'androidx.appcompat:appcompat:1.1.0'
32 | implementation 'androidx.core:core-ktx:1.2.0'
33 | testImplementation 'junit:junit:4.12'
34 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
35 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
36 | }
37 |
--------------------------------------------------------------------------------
/library/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ziwenL/SrcScrollFrameLayout/b3624a73e650eeffc63b710e8d2b2cca06be757e/library/consumer-rules.pro
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/com/ziwenl/library/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.ziwenl.library
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.ziwenl.library.test", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/library/src/main/java/com/ziwenl/library/widgets/SrcLoopScrollFrameLayout.kt:
--------------------------------------------------------------------------------
1 | package com.ziwenl.library.widgets
2 |
3 | import android.content.Context
4 | import android.graphics.*
5 | import android.graphics.drawable.BitmapDrawable
6 | import android.graphics.drawable.Drawable
7 | import android.util.AttributeSet
8 | import android.widget.FrameLayout
9 | import androidx.annotation.ColorInt
10 | import androidx.annotation.IntDef
11 | import com.ziwenl.library.R
12 |
13 | /**
14 | * PackageName : com.ziwenl.library.widgets
15 | * Author : Ziwen Lan
16 | * Date : 2021/5/25
17 | * Time : 16:41
18 | * Introduction :仿小红书登陆页面背景图无限滚动 FrameLayout kotlin 版
19 | * 功能特点:
20 | * 1.将选择的图片按比例缩放填满当前 View 高度
21 | * 2.背景图片缩放后宽/高度小于当前 View 宽/高度时自动复制黏贴直到占满当前 View 宽/高度,以此来达到无限滚动效果
22 | * 3.可通过自定义属性 speed 调整滚动速度,提供 slow、ordinary 和 fast 选项,也可自行填入 int 值,值越大滚动速度越快,建议 1 ≤ speed ≤ 50
23 | * 4.可通过自定义属性 maskLayerColor 设置遮罩层颜色,建议带透明度
24 | * 5.提供 startScroll 和 stopScroll 方法控制开始/停止滚动
25 | * 6.可通过自定义属性 scrollOrientation 设置滚动方向,可设置为上移、下移、左移或右移
26 | */
27 | class SrcLoopScrollFrameLayout : FrameLayout {
28 |
29 | companion object {
30 |
31 | /**
32 | * 滚动方向
33 | */
34 | @IntDef(OUT_SLIDE_TOP, OUT_SLIDE_BOTTOM, OUT_SLIDE_LEFT, OUT_SLIDE_RIGHT)
35 | @Retention(AnnotationRetention.SOURCE)
36 | annotation class ScrollOrientation
37 |
38 | /**
39 | * 滚动方向常量
40 | * 0:往上滚出
41 | * 1:往下滚出
42 | * 2:往左滚出
43 | * 3:往右滚出
44 | */
45 | const val OUT_SLIDE_TOP = 0
46 | const val OUT_SLIDE_BOTTOM = 1
47 | const val OUT_SLIDE_LEFT = 2
48 | const val OUT_SLIDE_RIGHT = 3
49 |
50 | /**
51 | * 重绘间隔时间
52 | */
53 | private const val DEFAULT_DRAW_INTERVALS_TIME = 5L
54 | }
55 |
56 |
57 | /**
58 | * 间隔时间内平移距离
59 | */
60 | private var mPanDistance = 0f
61 |
62 | /**
63 | * 间隔时间内平移增距
64 | */
65 | private var mIntervalIncreaseDistance = 0.5f
66 |
67 | /**
68 | * 填满当前view所需bitmap个数
69 | */
70 | private var mBitmapCount = 0
71 |
72 | /**
73 | * 是否开始滚动
74 | */
75 | private var mIsScroll = false
76 |
77 | /**
78 | * 滚动方向,默认往上滚出
79 | */
80 | @ScrollOrientation
81 | private var mScrollOrientation = OUT_SLIDE_TOP
82 |
83 | /**
84 | * 遮罩层颜色
85 | */
86 | @ColorInt
87 | private var mMaskLayerColor = 0
88 |
89 | private var mDrawable: Drawable? = null
90 | private var mSrcBitmap: Bitmap? = null
91 | private lateinit var mPaint: Paint
92 | private lateinit var mMatrix: Matrix
93 |
94 | constructor(context: Context) : super(context) {
95 | initView(context, null, 0)
96 | }
97 |
98 | constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
99 | initView(context, attrs, 0)
100 | }
101 |
102 | constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
103 | context,
104 | attrs,
105 | defStyleAttr
106 | ) {
107 | initView(context, attrs, defStyleAttr)
108 | }
109 |
110 | private fun initView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
111 | val array = context.theme.obtainStyledAttributes(
112 | attrs,
113 | R.styleable.SrcLoopScrollFrameLayout,
114 | defStyleAttr,
115 | 0
116 | )
117 | val speed = array.getInteger(R.styleable.SrcLoopScrollFrameLayout_speed, 3)
118 | mScrollOrientation =
119 | array.getInteger(R.styleable.SrcLoopScrollFrameLayout_scrollOrientation, OUT_SLIDE_TOP)
120 | mIntervalIncreaseDistance *= speed
121 | mDrawable = array.getDrawable(R.styleable.SrcLoopScrollFrameLayout_src)
122 | mIsScroll = array.getBoolean(R.styleable.SrcLoopScrollFrameLayout_isScroll, true)
123 | mMaskLayerColor =
124 | array.getColor(R.styleable.SrcLoopScrollFrameLayout_maskLayerColor, Color.TRANSPARENT)
125 | array.recycle()
126 |
127 | setWillNotDraw(false)
128 | mPaint = Paint()
129 | mMatrix = Matrix()
130 | }
131 |
132 | override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
133 | super.onSizeChanged(w, h, oldw, oldh)
134 | if (mDrawable == null || mDrawable !is BitmapDrawable) {
135 | return
136 | }
137 | if (visibility == GONE) {
138 | return
139 | }
140 | if (w == 0 || h == 0) {
141 | return
142 | }
143 | if (mSrcBitmap == null) {
144 | val bitmap = (mDrawable as BitmapDrawable).bitmap
145 | //调整色彩模式进行质量压缩
146 | val compressBitmap = bitmap.copy(Bitmap.Config.RGB_565, true)
147 | //缩放 Bitmap
148 | mSrcBitmap = scaleBitmap(compressBitmap)
149 | //计算至少需要几个 bitmap 才能填满当前 view
150 | when (mScrollOrientation) {
151 | OUT_SLIDE_TOP, OUT_SLIDE_BOTTOM -> {
152 | mBitmapCount = measuredHeight / mSrcBitmap!!.height + 1
153 | }
154 | OUT_SLIDE_LEFT, OUT_SLIDE_RIGHT -> {
155 | mBitmapCount = measuredWidth / mSrcBitmap!!.width + 1
156 | }
157 | }
158 | if (!compressBitmap.isRecycled) {
159 | compressBitmap.isRecycled
160 | System.gc()
161 | }
162 | }
163 | }
164 |
165 | override fun onDraw(canvas: Canvas) {
166 | super.onDraw(canvas)
167 | if (mSrcBitmap == null) {
168 | return
169 | }
170 | val length = if (scrollOrientationIsVertical()) mSrcBitmap!!.height else mSrcBitmap!!.width
171 | if (length + mPanDistance != 0f) {
172 | //第一张图片未完全滚出屏幕
173 | mMatrix.reset()
174 | when (mScrollOrientation) {
175 | OUT_SLIDE_TOP -> {
176 | mMatrix.postTranslate(0f, mPanDistance)
177 | }
178 | OUT_SLIDE_BOTTOM -> {
179 | mMatrix.postTranslate(0f, measuredHeight - length - mPanDistance)
180 | }
181 | OUT_SLIDE_LEFT -> {
182 | mMatrix.postTranslate(mPanDistance, 0f)
183 | }
184 | OUT_SLIDE_RIGHT -> {
185 | mMatrix.postTranslate(measuredWidth - length - mPanDistance, 0f)
186 | }
187 | }
188 | canvas.drawBitmap(mSrcBitmap!!, mMatrix, mPaint)
189 | }
190 | if (length + mPanDistance < (if (scrollOrientationIsVertical()) measuredHeight else measuredWidth)) {
191 | //用于补充留白的图片出现在屏幕
192 | for (i in 0 until mBitmapCount) {
193 | mMatrix.reset()
194 | when (mScrollOrientation) {
195 | OUT_SLIDE_TOP -> {
196 | mMatrix.postTranslate(0f, (i + 1) * length + mPanDistance)
197 | }
198 | OUT_SLIDE_BOTTOM -> {
199 | mMatrix.postTranslate(0f, measuredHeight - (i + 2) * length - mPanDistance)
200 | }
201 | OUT_SLIDE_LEFT -> {
202 | mMatrix.postTranslate((i + 1) * length + mPanDistance, 0f)
203 | }
204 | OUT_SLIDE_RIGHT -> {
205 | mMatrix.postTranslate(measuredWidth - (i + 2) * length - mPanDistance, 0f)
206 | }
207 | }
208 | canvas.drawBitmap(mSrcBitmap!!, mMatrix, mPaint)
209 | }
210 | }
211 | //绘制遮罩层
212 | if (mMaskLayerColor != Color.TRANSPARENT) {
213 | canvas.drawColor(mMaskLayerColor)
214 | }
215 | //延时重绘实现滚动效果
216 | if (mIsScroll) {
217 | handler.postDelayed(mRedrawRunnable, DEFAULT_DRAW_INTERVALS_TIME)
218 | }
219 | }
220 |
221 | /**
222 | * 重绘
223 | */
224 | private val mRedrawRunnable = Runnable {
225 | val length = if (scrollOrientationIsVertical()) mSrcBitmap!!.height else mSrcBitmap!!.width
226 | if (length + mPanDistance <= 0) {
227 | //第一张已完全滚出屏幕,重置平移距离
228 | mPanDistance = 0f
229 | }
230 | mPanDistance -= mIntervalIncreaseDistance
231 | invalidate()
232 | }
233 |
234 | /**
235 | * 设置背景图 bitmap
236 | * 通过该方法设置的背景图,当 屏幕翻转/暗黑模式切换 等涉及到 activity 重构的情况出现时,需要在 activity 重构后重新设置背景图
237 | * @srcBitmap 背景图
238 | * @isMassReduce 是否进行质量压缩(通过调整色彩模式实现,默认进行压缩)
239 | */
240 | fun setSrcBitmap(srcBitmap: Bitmap, isMassReduce: Boolean = true) {
241 | val oldScrollStatus = mIsScroll
242 | if (oldScrollStatus) {
243 | stopScroll()
244 | }
245 | val compressBitmap: Bitmap =
246 | if (isMassReduce && srcBitmap.config != Bitmap.Config.RGB_565) {
247 | //调整色彩模式进行质量压缩
248 | srcBitmap.copy(Bitmap.Config.RGB_565, true)
249 | } else {
250 | srcBitmap
251 | }
252 | //按当前View宽度比例缩放 Bitmap
253 | mSrcBitmap = scaleBitmap(compressBitmap)
254 | //计算至少需要几个 bitmap 才能填满当前 view
255 | mBitmapCount =
256 | if (scrollOrientationIsVertical()) measuredHeight / mSrcBitmap!!.height + 1 else measuredWidth / mSrcBitmap!!.width + 1
257 | if (!srcBitmap.isRecycled) {
258 | srcBitmap.isRecycled
259 | System.gc()
260 | }
261 | if (!compressBitmap.isRecycled) {
262 | compressBitmap.isRecycled
263 | System.gc()
264 | }
265 | if (oldScrollStatus) {
266 | startScroll()
267 | }
268 | }
269 |
270 | /**
271 | * 开始滚动
272 | */
273 | fun startScroll() {
274 | if (mSrcBitmap != null && mIsScroll) {
275 | return
276 | }
277 | mIsScroll = true
278 | handler.postDelayed(mRedrawRunnable, DEFAULT_DRAW_INTERVALS_TIME)
279 | }
280 |
281 | /**
282 | * 停止滚动
283 | */
284 | fun stopScroll() {
285 | if (!mIsScroll) {
286 | return
287 | }
288 | mIsScroll = false
289 | handler.removeCallbacks(mRedrawRunnable)
290 | }
291 |
292 | /**
293 | * 设置滚动方向
294 | */
295 | fun setScrollOrientation(@ScrollOrientation scrollOrientation: Int) {
296 | mPanDistance = 0f
297 | mScrollOrientation = scrollOrientation
298 | if (mSrcBitmap != null) {
299 | if (mDrawable != null && mDrawable is BitmapDrawable) {
300 | val bitmap = (mDrawable as BitmapDrawable).bitmap
301 | if (!bitmap.isRecycled) {
302 | setSrcBitmap(bitmap)
303 | return
304 | }
305 | }
306 | setSrcBitmap(mSrcBitmap!!)
307 | }
308 | }
309 |
310 | /**
311 | * 切换滚动方向
312 | */
313 | fun changeScrollOrientation() {
314 | mPanDistance = 0f
315 | if (mScrollOrientation == OUT_SLIDE_RIGHT) {
316 | mScrollOrientation = OUT_SLIDE_TOP
317 | } else {
318 | mScrollOrientation++
319 | }
320 | if (mSrcBitmap != null) {
321 | if (mDrawable != null && mDrawable is BitmapDrawable) {
322 | val bitmap = (mDrawable as BitmapDrawable).bitmap
323 | if (!bitmap.isRecycled) {
324 | setSrcBitmap(bitmap)
325 | return
326 | }
327 | }
328 | setSrcBitmap(mSrcBitmap!!)
329 | }
330 | }
331 |
332 | /**
333 | * 判断是否为竖直滚动
334 | */
335 | private fun scrollOrientationIsVertical(): Boolean {
336 | return mScrollOrientation == OUT_SLIDE_TOP || mScrollOrientation == OUT_SLIDE_BOTTOM
337 | }
338 |
339 |
340 | /**
341 | * 缩放 Bitmap
342 | */
343 | private fun scaleBitmap(originBitmap: Bitmap): Bitmap? {
344 | val width = originBitmap.width
345 | val height = originBitmap.height
346 | val newHeight: Int
347 | val newWidth: Int
348 | if (scrollOrientationIsVertical()) {
349 | newWidth = measuredWidth
350 | newHeight = newWidth * height / width
351 | } else {
352 | newHeight = measuredHeight
353 | newWidth = newHeight * width / height
354 | }
355 | return Bitmap.createScaledBitmap(originBitmap, newWidth, newHeight, true)
356 | }
357 | }
--------------------------------------------------------------------------------
/library/src/main/java/com/ziwenl/library/widgets/SrcScrollFrameLayout.java:
--------------------------------------------------------------------------------
1 | package com.ziwenl.library.widgets;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Bitmap;
6 | import android.graphics.Canvas;
7 | import android.graphics.Color;
8 | import android.graphics.Matrix;
9 | import android.graphics.Paint;
10 | import android.graphics.drawable.BitmapDrawable;
11 | import android.graphics.drawable.Drawable;
12 | import android.util.AttributeSet;
13 | import android.widget.FrameLayout;
14 |
15 | import androidx.annotation.ColorInt;
16 | import androidx.annotation.IntDef;
17 | import androidx.annotation.NonNull;
18 | import androidx.annotation.Nullable;
19 |
20 | import com.ziwenl.library.R;
21 |
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 |
25 | /**
26 | * PackageName : com.ziwenl.library.widgets
27 | * Author : Ziwen Lan
28 | * Date : 2020/5/13
29 | * Time : 11:23
30 | * Introduction :仿小红书登陆页面背景图无限滚动 FrameLayout
31 | * 功能特点:
32 | * 1.将选择的图片按比例缩放填满当前 View 高度
33 | * 2.背景图片缩放后宽/高度小于当前 View 宽/高度时自动复制黏贴直到占满当前 View 宽/高度,以此来达到无限滚动效果
34 | * 3.可通过自定义属性 speed 调整滚动速度,提供 slow、ordinary 和 fast 选项,也可自行填入 int 值,值越大滚动速度越快,建议 1 ≤ speed ≤ 50
35 | * 4.可通过自定义属性 maskLayerColor 设置遮罩层颜色,建议带透明度
36 | * 5.提供 startScroll 和 stopScroll 方法控制开始/停止滚动
37 | * 6.可通过自定义属性 scrollOrientation 设置滚动方向,可设置为上移、下移、左移或右移
38 | *
39 | * @Deprecated 建议使用最新的 kotlin 版 {@link SrcLoopScrollFrameLayout},后续 Java 版本可能将放弃维护
40 | */
41 | @Deprecated
42 | public class SrcScrollFrameLayout extends FrameLayout {
43 | /**
44 | * 滚动方向
45 | * 0:往上滚出
46 | * 1:往下滚出
47 | * 2:往左滚出
48 | * 3:往右滚出
49 | */
50 | public final static int OUT_SLIDE_TOP = 0;
51 | public final static int OUT_SLIDE_BOTTOM = 1;
52 | public final static int OUT_SLIDE_LEFT = 2;
53 | public final static int OUT_SLIDE_RIGHT = 3;
54 |
55 | @IntDef({OUT_SLIDE_TOP, OUT_SLIDE_BOTTOM, OUT_SLIDE_LEFT, OUT_SLIDE_RIGHT})
56 | @Retention(RetentionPolicy.SOURCE)
57 | public @interface ScrollOrientation {
58 | }
59 |
60 | /**
61 | * 重绘间隔时间
62 | */
63 | private final static long DEFAULT_DRAW_INTERVALS_TIME = 5L;
64 | /**
65 | * 间隔时间内平移距离
66 | */
67 | private float mPanDistance = 0;
68 | /**
69 | * 间隔时间内平移增距
70 | */
71 | private float mIntervalIncreaseDistance = 0.5f;
72 | /**
73 | * 填满当前view所需bitmap个数
74 | */
75 | private int mBitmapCount = 0;
76 | /**
77 | * 是否开始滚动
78 | */
79 | private boolean mIsScroll;
80 | /**
81 | * 滚动方向,默认往上滚出
82 | */
83 | @ScrollOrientation
84 | private int mScrollOrientation;
85 | /**
86 | * 遮罩层颜色
87 | */
88 | @ColorInt
89 | private int mMaskLayerColor;
90 |
91 | private Drawable mDrawable;
92 | private Bitmap mSrcBitmap;
93 | private Paint mPaint;
94 | private Matrix mMatrix;
95 |
96 | public SrcScrollFrameLayout(@NonNull Context context) {
97 | this(context, null, 0);
98 | }
99 |
100 | public SrcScrollFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
101 | this(context, attrs, 0);
102 | }
103 |
104 | public SrcScrollFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
105 | super(context, attrs, defStyleAttr);
106 |
107 | TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SrcScrollFrameLayout, defStyleAttr, 0);
108 | int speed = array.getInteger(R.styleable.SrcScrollFrameLayout_speed, 3);
109 | mScrollOrientation = array.getInteger(R.styleable.SrcScrollFrameLayout_scrollOrientation, OUT_SLIDE_TOP);
110 | mIntervalIncreaseDistance = speed * mIntervalIncreaseDistance;
111 | mDrawable = array.getDrawable(R.styleable.SrcScrollFrameLayout_src);
112 | mIsScroll = array.getBoolean(R.styleable.SrcScrollFrameLayout_isScroll, true);
113 | mMaskLayerColor = array.getColor(R.styleable.SrcScrollFrameLayout_maskLayerColor, Color.TRANSPARENT);
114 | array.recycle();
115 |
116 | setWillNotDraw(false);
117 | mPaint = new Paint();
118 | mMatrix = new Matrix();
119 |
120 | }
121 |
122 | @Override
123 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
124 | super.onSizeChanged(w, h, oldw, oldh);
125 | if (mDrawable == null || !(mDrawable instanceof BitmapDrawable)) {
126 | return;
127 | }
128 | if (getVisibility() == GONE) {
129 | return;
130 | }
131 | if (w == 0 || h == 0) {
132 | return;
133 | }
134 | if (mSrcBitmap == null) {
135 | Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
136 | //调整色彩模式进行质量压缩
137 | Bitmap compressBitmap = bitmap.copy(Bitmap.Config.RGB_565, true);
138 | //缩放 Bitmap
139 | mSrcBitmap = scaleBitmap(compressBitmap);
140 | //计算至少需要几个 bitmap 才能填满当前 view
141 | mBitmapCount = scrollOrientationIsVertical() ?
142 | getMeasuredHeight() / mSrcBitmap.getHeight() + 1
143 | :
144 | getMeasuredWidth() / mSrcBitmap.getWidth() + 1;
145 | if (!compressBitmap.isRecycled()) {
146 | compressBitmap.isRecycled();
147 | System.gc();
148 | }
149 | }
150 | }
151 |
152 | @Override
153 | protected void onDraw(Canvas canvas) {
154 | super.onDraw(canvas);
155 | if (mSrcBitmap == null) {
156 | return;
157 | }
158 | int length = scrollOrientationIsVertical() ? mSrcBitmap.getHeight() : mSrcBitmap.getWidth();
159 | int measuredHeight = getMeasuredHeight();
160 | int measuredWidth = getMeasuredWidth();
161 | if (length + mPanDistance != 0) {
162 | //第一张图片未完全滚出屏幕
163 | mMatrix.reset();
164 | switch (mScrollOrientation) {
165 | case OUT_SLIDE_TOP:
166 | mMatrix.postTranslate(0, mPanDistance);
167 | break;
168 | case OUT_SLIDE_BOTTOM:
169 | mMatrix.postTranslate(0f, measuredHeight - length - mPanDistance);
170 | break;
171 | case OUT_SLIDE_LEFT:
172 | mMatrix.postTranslate(mPanDistance, 0);
173 | break;
174 | case OUT_SLIDE_RIGHT:
175 | mMatrix.postTranslate(measuredWidth - length - mPanDistance, 0f);
176 | break;
177 |
178 | }
179 | canvas.drawBitmap(mSrcBitmap, mMatrix, mPaint);
180 | }
181 | if (length + mPanDistance < (scrollOrientationIsVertical() ? measuredHeight : measuredWidth)) {
182 | //用于补充留白的图片出现在屏幕
183 | for (int i = 0; i < mBitmapCount; i++) {
184 | mMatrix.reset();
185 | switch (mScrollOrientation) {
186 | case OUT_SLIDE_TOP:
187 | mMatrix.postTranslate(0f, (i + 1) * length + mPanDistance);
188 | break;
189 | case OUT_SLIDE_BOTTOM:
190 | mMatrix.postTranslate(0f, measuredHeight - (i + 2) * length - mPanDistance);
191 | break;
192 | case OUT_SLIDE_LEFT:
193 | mMatrix.postTranslate((i + 1) * length + mPanDistance, 0f);
194 | break;
195 | case OUT_SLIDE_RIGHT:
196 | mMatrix.postTranslate(measuredWidth - (i + 2) * length - mPanDistance, 0f);
197 | break;
198 | }
199 | canvas.drawBitmap(mSrcBitmap, mMatrix, mPaint);
200 | }
201 | }
202 | //绘制遮罩层
203 | if (mMaskLayerColor != Color.TRANSPARENT) {
204 | canvas.drawColor(mMaskLayerColor);
205 | }
206 | //延时重绘实现滚动效果
207 | if (mIsScroll) {
208 | getHandler().postDelayed(mRedrawRunnable, DEFAULT_DRAW_INTERVALS_TIME);
209 | }
210 | }
211 |
212 | /**
213 | * 重绘
214 | */
215 | private Runnable mRedrawRunnable = new Runnable() {
216 | @Override
217 | public void run() {
218 | int length = scrollOrientationIsVertical() ? mSrcBitmap.getHeight() : mSrcBitmap.getWidth();
219 | if (length + mPanDistance <= 0) {
220 | //第一张已完全滚出屏幕,重置平移距离
221 | mPanDistance = 0;
222 | }
223 | mPanDistance -= mIntervalIncreaseDistance;
224 | invalidate();
225 | }
226 | };
227 |
228 | /**
229 | * 开始滚动
230 | */
231 | public void startScroll() {
232 | if (mIsScroll) {
233 | return;
234 | }
235 | mIsScroll = true;
236 | getHandler().postDelayed(mRedrawRunnable, DEFAULT_DRAW_INTERVALS_TIME);
237 | }
238 |
239 | /**
240 | * 停止滚动
241 | */
242 | public void stopScroll() {
243 | if (!mIsScroll) {
244 | return;
245 | }
246 | mIsScroll = false;
247 | getHandler().removeCallbacks(mRedrawRunnable);
248 | }
249 |
250 | /**
251 | * 设置背景图 bitmap
252 | * 通过该方法设置的背景图,当 屏幕翻转/暗黑模式切换 等涉及到 activity 重构的情况出现时,需要在 activity 重构后重新设置背景图
253 | */
254 | public void setSrcBitmap(Bitmap srcBitmap) {
255 | boolean oldScrollStatus = mIsScroll;
256 | if (oldScrollStatus) {
257 | stopScroll();
258 | }
259 | Bitmap compressBitmap;
260 | if (srcBitmap.getConfig() != Bitmap.Config.RGB_565) {
261 | compressBitmap = srcBitmap.copy(Bitmap.Config.RGB_565, true);
262 | } else {
263 | compressBitmap = srcBitmap;
264 | }
265 | //按当前View宽度比例缩放 Bitmap
266 | mSrcBitmap = scaleBitmap(compressBitmap);
267 | //计算至少需要几个 bitmap 才能填满当前 view
268 | mBitmapCount = scrollOrientationIsVertical() ?
269 | getMeasuredHeight() / mSrcBitmap.getHeight() + 1
270 | :
271 | getMeasuredWidth() / mSrcBitmap.getWidth() + 1;
272 | if (!srcBitmap.isRecycled()) {
273 | srcBitmap.isRecycled();
274 | System.gc();
275 | }
276 | if (!compressBitmap.isRecycled()) {
277 | compressBitmap.isRecycled();
278 | System.gc();
279 | }
280 | if (oldScrollStatus) {
281 | startScroll();
282 | }
283 | }
284 |
285 | /**
286 | * 判断是否为竖直滚动
287 | */
288 | private boolean scrollOrientationIsVertical() {
289 | return mScrollOrientation == OUT_SLIDE_TOP || mScrollOrientation == OUT_SLIDE_BOTTOM;
290 | }
291 |
292 | /**
293 | * 设置滚动方向
294 | * @param scrollOrientation
295 | */
296 | public void setScrollOrientation(@ScrollOrientation int scrollOrientation) {
297 | mPanDistance = 0;
298 | mScrollOrientation = scrollOrientation;
299 | if (mSrcBitmap != null) {
300 | if (mDrawable != null && (mDrawable instanceof BitmapDrawable)) {
301 | Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
302 | if (!bitmap.isRecycled()) {
303 | setSrcBitmap(bitmap);
304 | return;
305 | }
306 | }
307 | setSrcBitmap(mSrcBitmap);
308 | }
309 | }
310 |
311 | /**
312 | * 切换滚动方向
313 | */
314 | public void changeScrollOrientation() {
315 | mPanDistance = 0;
316 | if (mScrollOrientation == OUT_SLIDE_RIGHT) {
317 | mScrollOrientation = OUT_SLIDE_TOP;
318 | } else {
319 | mScrollOrientation++;
320 | }
321 | if (mSrcBitmap != null) {
322 | if (mDrawable != null && (mDrawable instanceof BitmapDrawable)) {
323 | Bitmap bitmap = ((BitmapDrawable) mDrawable).getBitmap();
324 | if (!bitmap.isRecycled()) {
325 | setSrcBitmap(bitmap);
326 | return;
327 | }
328 | }
329 | setSrcBitmap(mSrcBitmap);
330 | }
331 | }
332 |
333 | /**
334 | * 缩放Bitmap
335 | */
336 | private Bitmap scaleBitmap(Bitmap originBitmap) {
337 | int width = originBitmap.getWidth();
338 | int height = originBitmap.getHeight();
339 | int newHeight;
340 | int newWidth;
341 | if (scrollOrientationIsVertical()) {
342 | newWidth = getMeasuredWidth();
343 | newHeight = newWidth * height / width;
344 | } else {
345 | newHeight = getMeasuredHeight();
346 | newWidth = newHeight * width / height;
347 | }
348 |
349 | return Bitmap.createScaledBitmap(originBitmap, newWidth, newHeight, true);
350 | }
351 | }
352 |
--------------------------------------------------------------------------------
/library/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/library/src/test/java/com/ziwenl/library/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.ziwenl.library
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name='SrcScrollFrameLayout'
2 | include ':app'
3 | include ':library'
4 |
--------------------------------------------------------------------------------