├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.xml │ │ │ ├── drawable │ │ │ │ └── sky.png │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── content_scrolling.xml │ │ ├── java │ │ │ └── pathdemo │ │ │ │ └── li │ │ │ │ └── com │ │ │ │ └── pathdemo │ │ │ │ ├── MainActivity.java │ │ │ │ ├── XScrollView.java │ │ │ │ └── PathView.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── pathdemo │ │ │ └── li │ │ │ └── com │ │ │ └── pathdemo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── pathdemo │ │ └── li │ │ └── com │ │ └── pathdemo │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── images ├── 1.png ├── 2.png ├── 3.png ├── 4.png └── pathview.gif ├── .idea ├── copyright │ └── profiles_settings.xml ├── markdown-navigator │ └── profiles_settings.xml ├── encodings.xml ├── vcs.xml ├── inspectionProfiles │ ├── profiles_settings.xml │ └── Project_Default.xml ├── modules.xml ├── runConfigurations.xml ├── gradle.xml ├── compiler.xml ├── misc.xml └── markdown-navigator.xml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /images/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/images/1.png -------------------------------------------------------------------------------- /images/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/images/2.png -------------------------------------------------------------------------------- /images/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/images/3.png -------------------------------------------------------------------------------- /images/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/images/4.png -------------------------------------------------------------------------------- /images/pathview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/images/pathview.gif -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | PathDemo 3 | 4 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/sky.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/app/src/main/res/drawable/sky.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /.idea/markdown-navigator/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/415192022/PathDemo/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #60FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 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-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/java/pathdemo/li/com/pathdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package pathdemo.li.com.pathdemo; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | public class MainActivity extends AppCompatActivity { 7 | 8 | @Override 9 | protected void onCreate(Bundle savedInstanceState) { 10 | super.onCreate(savedInstanceState); 11 | setContentView(R.layout.content_scrolling); 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/test/java/pathdemo/li/com/pathdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package pathdemo.li.com.pathdemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Android\AndroidStudio\android_sdk\android_sdk\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1024m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/pathdemo/li/com/pathdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package pathdemo.li.com.pathdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("pathdemo.li.com.pathdemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "pathdemo.li.com.pathdemo" 8 | minSdkVersion 9 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.1.0' 28 | testCompile 'junit:junit:4.12' 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #ScrollView滑动到顶部后的阴影反馈效果 2 | ##### >当ScrollView滑动到顶部后,根据手指在屏幕上继续下拉出现的阴影效果的简单实现方式,仅供学习参考,在实际项目中引用还需慎重!!! 3 | #####技术点: 4 | #####1、利用Path中的moveTo() 、quadTo()方法实现的二阶贝塞尔曲线生成阴影 5 | >moveTo()方法就不做过多的介绍了,可以简单的理解为一个起始点。而quadTo()方法是android为开发者提供的一个生成二阶贝塞尔曲线方法,一段曲线用二阶足以!当然,还有三阶贝塞尔曲线生成方法:cubicTo(),然而android也只为我们提供到三阶,因为更高阶的贝塞尔曲线可以通过结合二阶和一阶来实现,其实贝塞尔曲线的核心就在于那个控制点,比如二阶曲线的控制点数量为1,三阶曲线的控制点数量为2... n阶曲线的控制点数量为n-1,以此类推。cubicTo()方法需要四个参数,这四个参数是干什么用的,或者说是怎么去传呢?前两个就是我们所说的核心:控制点坐标x,y,而后两个参数也是需要一个坐标,就是与moveTo()方法之对应的终点坐标,就好比moveTo()方法和lineTo方法成对使用是一个道理,要画一条直线肯定至少需要两个点:moveTo()是起点lineTo()是终点,而画一个二阶贝塞尔曲线也要依赖两个点moveTo()是起点,quadTo()的后两个参数为终点。好了,曲线出来了,那么怎么让曲线以上的部分变成阴影呢?实现也简单,设置好画笔就可以了,利用Paint的setStyle()方法传递Paint.Style.FILL参数即可,最后传入带有透明度的颜色即可,效果如下图 6 | ##### 7 | #####2、如何根据手势滑动来控制这段曲线? 8 | >这个问题也是实现此效果的核心,当然也是很简单的,简单的说下思路。首先我们要重写已经玩烂的onTouchEvent()方法,在 ACTION_DOWN中记录下手指按下的Y坐标,在ACTION_MOVE中算出实时的距离:就是当前手指的位置 - 初始按下的位置 ,就像这样 9 | ##### 10 | >然后我们将上面介绍过的贝塞尔曲线画法封装一下就像这样: 11 | ##### 12 | >因为需求确定,起始点就定在了屏幕的左上角,终点就定在屏幕的右上角,只需传入控制点即可但是我们如果无限的向屏幕下方滑动那么贝塞尔曲线的弧度也就越大,是不是感觉弧度太大也不是很美观,所以就要对他进行约束。这个约束条件就是当我们滑动Y的距离超过屏幕的1/10时就让这个距离固定在屏幕高度的1/10,如果小于1/10那么就根据我们手指移动的距离来与用户交互。 13 | >最后我们在onDraw()方法中调用它就行了,需要强调的是最后一定要调用canvas中的drawPath()方法,这个Path参数就是我们生成的贝塞尔曲线。 14 | #####3、当手松开时,这段曲线如何平滑过渡到初始位置? 15 | >此处的实现方式应该有很多种,这里我使用的是子线程进行多次计算然后通知主线程刷新UI的方式实现的。当我们在onTouchEvent方法中监听到ACTION_UP时我们就要让这段曲线复原,并且是平滑过渡回初始的位置,先上代码为敬: 16 | ##### 17 | >这段是执行在子线程中的,可能你发现使用了synchronized关键字,为什么要用它?不用不行吗? 当然,不用也可以,效果没有没么好而已。当我们遇到手指多次去滑动屏幕时我们的onTouchEvent方法就会不断的响应我们,当我们划一次就会开一条子线程去执行release()方法,那么假设我现在划过第一次后线程还没执行完毕,我又划了第二次此时就会有两条线程在运行,而我们的阴影还没有复位又开始了下一次的计算并且更新UI那不乱套才怪呢,用户体验就很差。所以我们要对线程加个锁,当我这条线程没计算完毕时新的线程就休想执行,使用synchronized简单的实现了我们这个复杂的业务需求;剩下的就很简单了就是如何平滑的滚回去?当松开手时计算好的距离又一次派上用场了,因为这段曲线就是从这里平滑的滚回原位,从这个距离开始递减一直到0为止。 18 | 19 | #####>最后附上简书地址:http://www.jianshu.com/p/a01ae4b42121 20 | >喜欢的顺便star一下,给我一些支持,谢谢大家! 21 | -------------------------------------------------------------------------------- /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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_scrolling.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 17 | 22 | 27 | 32 | 37 | 42 | 47 | 52 | 57 | 62 | 67 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 33 | 34 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /app/src/main/java/pathdemo/li/com/pathdemo/XScrollView.java: -------------------------------------------------------------------------------- 1 | package pathdemo.li.com.pathdemo; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Matrix; 6 | import android.graphics.Paint; 7 | import android.graphics.Path; 8 | import android.graphics.PointF; 9 | import android.graphics.RectF; 10 | import android.os.Handler; 11 | import android.os.Message; 12 | import android.util.AttributeSet; 13 | import android.view.MotionEvent; 14 | import android.widget.ScrollView; 15 | 16 | /** 17 | * Created by Mingwei Li on 2017/1/24 0024. 18 | */ 19 | 20 | public class XScrollView extends ScrollView { 21 | public XScrollView(Context context) { 22 | super(context); 23 | init(); 24 | 25 | } 26 | 27 | public XScrollView(Context context, AttributeSet attrs) { 28 | super(context, attrs); 29 | init(); 30 | } 31 | 32 | public XScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 33 | super(context, attrs, defStyleAttr); 34 | init(); 35 | } 36 | 37 | public XScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 38 | super(context, attrs, defStyleAttr, defStyleRes); 39 | init(); 40 | } 41 | 42 | private Paint paint; 43 | 44 | private void init() { 45 | pointF = new PointF(); 46 | paint = new Paint(); 47 | paint.setAntiAlias(true); 48 | paint.setColor(getResources().getColor(R.color.colorAccent)); 49 | paint.setStyle(Paint.Style.FILL); 50 | paint.setStrokeWidth(10); 51 | paint.setTextSize(50); 52 | } 53 | 54 | private Path drawBezierShadow(float cx, float cy) { 55 | Path path = new Path(); 56 | path.moveTo(0, 0); 57 | path.quadTo(cx, cy, getWidth(), 0); 58 | return path; 59 | } 60 | 61 | private Path drawTest(float cx, float cy, float[] floats) { 62 | RectF rectF = new RectF(cx, cy, 200, 200); 63 | Path path = new Path(); 64 | path.addOval(rectF, Path.Direction.CCW); 65 | Matrix matrix = new Matrix(); 66 | matrix.setValues(floats); 67 | path.transform(matrix); 68 | return path; 69 | } 70 | 71 | @Override 72 | protected void onDraw(Canvas canvas) { 73 | super.onDraw(canvas); 74 | if (pointF.y > getHeight() / 8) { 75 | pointF.y = getHeight() / 8; 76 | } 77 | canvas.drawPath(drawBezierShadow(pointF.x, pointF.y), paint); 78 | } 79 | 80 | /** 81 | * 同步方法,如果不用同步会出现闪烁 82 | */ 83 | private synchronized void release() { 84 | new Thread(new Runnable() { 85 | @Override 86 | public void run() { 87 | isDrawShadow = false; 88 | if (pointF.y > getHeight() / 10) { 89 | pointF.y = getHeight() / 10; 90 | } 91 | for (float i = pointF.y; i > 0; i--) { 92 | pointF.y = i; 93 | try { 94 | Thread.sleep(1); 95 | } catch (InterruptedException e) { 96 | e.printStackTrace(); 97 | } 98 | myHandler.sendEmptyMessage(0); 99 | } 100 | isDrawShadow = true; 101 | } 102 | }).start(); 103 | } 104 | 105 | private PointF pointF; 106 | private float dy; 107 | private volatile boolean isDrawShadow = true; 108 | 109 | @Override 110 | public boolean onTouchEvent(MotionEvent event) { 111 | System.out.println("top" + top); 112 | if (isDrawShadow) { 113 | switch (event.getAction()) { 114 | case MotionEvent.ACTION_UP: 115 | release(); 116 | break; 117 | case MotionEvent.ACTION_MOVE: 118 | pointF.x = event.getX(); 119 | float pd = event.getY() - dy; 120 | if (pd > getHeight() / 10) { 121 | pd = getHeight() / 10; 122 | } 123 | pointF.y = pd; 124 | break; 125 | case MotionEvent.ACTION_DOWN: 126 | dy = event.getY(); 127 | break; 128 | } 129 | } 130 | invalidate(); 131 | if ((pointF.y - dy) < 0) { 132 | return super.onTouchEvent(event); 133 | } else { 134 | return true; 135 | } 136 | } 137 | 138 | private int top = 100; 139 | 140 | @Override 141 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { 142 | super.onScrollChanged(l, t, oldl, oldt); 143 | top = t; 144 | System.out.println("------- top: " + t + "\n-------oldtop:" + oldt); 145 | } 146 | 147 | 148 | private MyHandler myHandler = new MyHandler(); 149 | 150 | class MyHandler extends Handler { 151 | @Override 152 | public void handleMessage(Message msg) { 153 | super.handleMessage(msg); 154 | invalidate(); 155 | } 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /app/src/main/java/pathdemo/li/com/pathdemo/PathView.java: -------------------------------------------------------------------------------- 1 | package pathdemo.li.com.pathdemo; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.graphics.Color; 7 | import android.graphics.Paint; 8 | import android.graphics.Path; 9 | import android.graphics.PointF; 10 | import android.graphics.PorterDuff; 11 | import android.graphics.PorterDuffXfermode; 12 | import android.graphics.Rect; 13 | import android.util.AttributeSet; 14 | import android.view.MotionEvent; 15 | import android.view.View; 16 | 17 | /** 18 | * Created by Mingwei Li on 2017/1/20 0020. 19 | */ 20 | 21 | public class PathView extends View { 22 | 23 | public PathView(Context context) { 24 | super(context); 25 | init(); 26 | } 27 | 28 | public PathView(Context context, AttributeSet attrs) { 29 | super(context, attrs); 30 | init(); 31 | } 32 | 33 | public PathView(Context context, AttributeSet attrs, int defStyleAttr) { 34 | super(context, attrs, defStyleAttr); 35 | init(); 36 | } 37 | 38 | public PathView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 39 | super(context, attrs, defStyleAttr, defStyleRes); 40 | init(); 41 | } 42 | 43 | private Paint paint; 44 | private PointF control; 45 | 46 | private void init() { 47 | paint = new Paint(); 48 | paint.setStrokeCap(Paint.Cap.ROUND); 49 | paint.setAntiAlias(true); 50 | paint.setStrokeWidth(10); 51 | paint.setColor(Color.CYAN); 52 | paint.setStyle(Paint.Style.STROKE); 53 | paint.setStrokeCap(Paint.Cap.ROUND); 54 | paint.setTextSize(100); 55 | 56 | control = new PointF(0, 0); 57 | } 58 | 59 | private void drawBezier(Canvas canvas) { 60 | // 绘制贝塞尔曲线 61 | Path path = new Path(); 62 | path.moveTo(0, getHeight() / 2); 63 | path.quadTo(control.x, control.y, getWidth(), getHeight() / 2); 64 | 65 | path.moveTo(0, getHeight() / 2); 66 | path.lineTo(control.x, control.y); 67 | 68 | path.moveTo(getWidth(), getHeight() / 2); 69 | path.lineTo(control.x, control.y); 70 | canvas.drawPath(path, paint); 71 | invalidate(); 72 | } 73 | 74 | private void drawCircle(Canvas canvas, float x, float y) { 75 | paint.setStyle(Paint.Style.FILL); 76 | canvas.drawCircle(x, y, 100, paint); 77 | } 78 | 79 | 80 | private void drawBM(Canvas canvas, Bitmap bitmap, int x, int y) { 81 | canvas.translate(getWidth() / 2, getHeight() / 2); 82 | Rect size = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 83 | Rect position = new Rect(x + bitmap.getWidth() / 2, y + bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight()); 84 | canvas.drawBitmap(bitmap, size, position, paint); 85 | } 86 | 87 | /** 88 | * 获取裁剪后的圆形图片 89 | */ 90 | public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) { 91 | Bitmap scaledSrcBmp; 92 | int diameter = radius * 2; 93 | // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片 94 | int bmpWidth = bmp.getWidth(); 95 | int bmpHeight = bmp.getHeight(); 96 | int squareWidth = 0, squareHeight = 0; 97 | int x = 0, y = 0; 98 | Bitmap squareBitmap; 99 | if (bmpHeight > bmpWidth) {// 高大于宽 100 | squareWidth = squareHeight = bmpWidth; 101 | x = 0; 102 | y = (bmpHeight - bmpWidth) / 2; 103 | // 截取正方形图片 104 | squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight); 105 | } else if (bmpHeight < bmpWidth) {// 宽大于高 106 | squareWidth = squareHeight = bmpHeight; 107 | x = (bmpWidth - bmpHeight) / 2; 108 | y = 0; 109 | squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight); 110 | } else { 111 | squareBitmap = bmp; 112 | } 113 | if (squareBitmap.getWidth() != diameter || squareBitmap.getHeight() != diameter) { 114 | scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, diameter, true); 115 | } else { 116 | scaledSrcBmp = squareBitmap; 117 | } 118 | Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(), 119 | scaledSrcBmp.getHeight(), 120 | Bitmap.Config.ARGB_8888); 121 | Canvas canvas = new Canvas(output); 122 | 123 | Paint paint = new Paint(); 124 | Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight()); 125 | 126 | paint.setAntiAlias(true); 127 | paint.setFilterBitmap(true); 128 | paint.setDither(true); 129 | canvas.drawARGB(0, 0, 0, 0); 130 | canvas.drawCircle(scaledSrcBmp.getWidth() / 2, 131 | scaledSrcBmp.getHeight() / 2, 132 | scaledSrcBmp.getWidth() / 2, 133 | paint); 134 | paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 135 | canvas.drawBitmap(scaledSrcBmp, rect, rect, paint); 136 | bmp = null; 137 | squareBitmap = null; 138 | scaledSrcBmp = null; 139 | return output; 140 | } 141 | 142 | private VPoint p2, p4; 143 | private HPoint p1, p3; 144 | 145 | class VPoint { 146 | public float x; 147 | public float y; 148 | public PointF top = new PointF(); 149 | public PointF bottom = new PointF(); 150 | 151 | public void setX(float x) { 152 | this.x = x; 153 | top.x = x; 154 | bottom.x = x; 155 | } 156 | 157 | public void adjustY(float offset) { 158 | top.y -= offset; 159 | bottom.y += offset; 160 | } 161 | 162 | public void adjustAllX(float offset) { 163 | this.x += offset; 164 | top.x += offset; 165 | bottom.x += offset; 166 | } 167 | } 168 | 169 | class HPoint { 170 | public float x; 171 | public float y; 172 | public PointF left = new PointF(); 173 | public PointF right = new PointF(); 174 | 175 | public void setY(float y) { 176 | this.y = y; 177 | left.y = y; 178 | right.y = y; 179 | } 180 | 181 | public void adjustAllX(float offset) { 182 | this.x += offset; 183 | left.x += offset; 184 | right.x += offset; 185 | } 186 | } 187 | 188 | private float radius = 50; 189 | private float blackMagic = 0.551915024494f * radius; 190 | private float distance = blackMagic * 0.45f; 191 | private float c = 0.551915024494f; 192 | private float maxLength = getWidth() - radius - radius; 193 | 194 | private void drawCicleBezierSquare3(Canvas canvas) { 195 | canvas.translate(getWidth() / 2, getHeight() / 2); 196 | Path path = new Path(); 197 | path.moveTo(0, -100); 198 | path.cubicTo(blackMagic, 0, 0, blackMagic, 100, 0); 199 | 200 | // path.moveTo(100, 100); 201 | // path.cubicTo(c, -c, c, -10, 100, -100); 202 | // path.moveTo(100, -100); 203 | // path.cubicTo(-c, -10, -10, -c, -100, -100); 204 | // path.moveTo(-100, -100); 205 | // path.cubicTo(-10, c, -c, 10, -100, 100); 206 | canvas.drawPath(path, paint); 207 | 208 | paint.setColor(Color.GREEN); 209 | path.moveTo(-100, 100); 210 | path.lineTo(100, 100); 211 | path.moveTo(100, 100); 212 | path.lineTo(100, -100); 213 | // path.moveTo(100, -100); 214 | // path.lineTo(-100, -100); 215 | // path.moveTo(-100, -100); 216 | // path.lineTo(-100, 100); 217 | canvas.drawPath(path, paint); 218 | 219 | } 220 | 221 | 222 | @Override 223 | protected void onDraw(Canvas canvas) { 224 | super.onDraw(canvas); 225 | drawBezier(canvas); 226 | 227 | // drawCircle(canvas, control.x, control.y); 228 | 229 | // drawBM(canvas, getCroppedRoundBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.sky), 350), (int) control.x, (int) control.y); 230 | // drawCicleBezierSquare3(canvas); 231 | } 232 | 233 | @Override 234 | public boolean onTouchEvent(MotionEvent event) { 235 | // 根据触摸位置更新控制点,并提示重绘 236 | control.x = event.getX(); 237 | control.y = event.getY(); 238 | invalidate(); 239 | return true; 240 | } 241 | 242 | 243 | } 244 | --------------------------------------------------------------------------------