Skip to content

Commit

Permalink
实现基于lanmda的插值器函数
Browse files Browse the repository at this point in the history
  • Loading branch information
caofengbin committed Jun 20, 2023
1 parent 6cacc32 commit f072829
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.Test;

import java.util.function.BiFunction;

public class AndroidTouchHandlerTest {

private int width = 720;
Expand All @@ -27,4 +29,39 @@ public void test() {
System.out.println(re[0]);
System.out.println(re[1]);
}

@Test
public void test_interpolator() {
int[] re1 = {200, 1200};
int[] re2 = {200, 300};
// 过渡总时间
int duration = 500;
// 开始时间
long startTime = System.currentTimeMillis();
while (true) {
// 当前时间
long currentTime = System.currentTimeMillis();
float timeProgress = (currentTime - startTime) / (float) duration;
if (timeProgress >= 1.0f) {
// 已经过渡到结束值,停止过渡
System.out.println("[" + re2[0] + "," + re2[1] + "]");
break;
}
BiFunction<Integer, Integer, Integer> transitionX = (start, end) ->
(int) (start + (end - start) * timeProgress);
BiFunction<Integer, Integer, Integer> transitionY = (start, end) ->
(int) (start + (end - start) * timeProgress); // Y 坐标过渡函数

int currentX = transitionX.apply(re1[0], re2[0]); // 当前 X 坐标
int currentY = transitionY.apply(re1[1], re2[1]); // 当前 Y 坐标
// 使用当前坐标进行操作
// ...
System.out.println("[" + currentX + "," + currentY + "]");
try {
Thread.sleep(10);
} catch (InterruptedException e) {

}
}
}
}

0 comments on commit f072829

Please sign in to comment.