Skip to content

Commit d4242b8

Browse files
committed
修复关于页面颜色播放不连贯的问题
1 parent 32d68b2 commit d4242b8

3 files changed

Lines changed: 49 additions & 7 deletions

File tree

app/src/main/java/com/sevtinge/hyperceiler/about/AboutSettingsFragment.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.view.LayoutInflater;
2222
import android.view.View;
2323
import android.view.ViewGroup;
24+
import android.view.ViewParent;
2425
import android.view.WindowInsets;
2526
import android.widget.FrameLayout;
2627
import android.widget.TextView;
@@ -266,6 +267,12 @@ public void onStart() {
266267
//setShadowEffect();
267268
}
268269

270+
@Override
271+
public void onStop() {
272+
stopRuntimeShader();
273+
super.onStop();
274+
}
275+
269276
@Override
270277
public void onResume() {
271278
super.onResume();
@@ -468,6 +475,12 @@ private void startRuntimeShader() {
468475
}
469476
}
470477

478+
private void stopRuntimeShader() {
479+
if (mBgEffectController != null) {
480+
mBgEffectController.stop();
481+
}
482+
}
483+
471484
private void checkNetwork() {
472485
Context context = getSafeContext();
473486
if (context == null || executor.isShutdown()) {
@@ -546,11 +559,21 @@ private void checkNetworkDebounced() {
546559

547560
@Override
548561
public void onDestroyView() {
562+
stopRuntimeShader();
549563
super.onDestroyView();
550564
mHandler.removeCallbacksAndMessages(null);
551565
if (mRootView != null) {
552566
unregisterCoordinateScrollView(mRootView);
553567
}
568+
if (mBgEffectView != null) {
569+
ViewParent parent = mBgEffectView.getParent();
570+
if (parent instanceof ViewGroup viewGroup) {
571+
viewGroup.removeView(mBgEffectView);
572+
}
573+
}
574+
mBgEffectView = null;
575+
mBgEffectController = null;
576+
mContentView = null;
554577
mRootView = null;
555578
unregisterNetworkCallback();
556579
executor.shutdownNow();

app/src/main/java/com/sevtinge/hyperceiler/about/controller/BgEffectController.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ public void start() {
2929
mBgEffectPainter = new BgEffectPainter(mTarget.getContext());
3030
mLastGlobalTime = System.nanoTime();
3131
resetTime();
32-
mTarget.post(this);
32+
mTarget.postOnAnimation(this);
3333
}
3434
}
3535

3636
@Override
3737
public void run() {
3838
if (mBgEffectPainter != null) {
3939
tickPingPong();
40-
mBgEffectPainter.setResolution(mTarget.getWidth(), mTarget.getHeight());
41-
mBgEffectPainter.updateMaterials(mDeltaTime * mTimeDirection);
42-
mTarget.setRenderEffect(mBgEffectPainter.getRenderEffect());
43-
mTarget.postDelayed(this, 16L);
40+
if (mTarget.getWidth() > 0 && mTarget.getHeight() > 0) {
41+
mBgEffectPainter.setResolution(mTarget.getWidth(), mTarget.getHeight());
42+
mBgEffectPainter.updateMaterials(mDeltaTime * mTimeDirection);
43+
mTarget.setRenderEffect(mBgEffectPainter.getRenderEffect());
44+
mTarget.invalidate();
45+
}
46+
mTarget.postOnAnimation(this);
4447
}
4548
}
4649

@@ -70,6 +73,7 @@ public void stop() {
7073
mBgEffectPainter.stop();
7174
mBgEffectPainter = null;
7275
mTarget.setRenderEffect(null);
76+
mTarget.invalidate();
7377
}
7478
}
7579

app/src/main/java/com/sevtinge/hyperceiler/about/controller/BgEffectPainter.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.sevtinge.hyperceiler.R;
1313
import com.sevtinge.hyperceiler.common.log.AndroidLog;
1414

15+
import org.intellij.lang.annotations.Language;
16+
1517
import java.io.InputStream;
1618
import java.util.Scanner;
1719

@@ -38,10 +40,11 @@ public class BgEffectPainter {
3840
private float[] uBgBound = {0.0f, 0.4489f, 1.0f, 0.5511f};
3941
private final float[] uColors = {0.57f, 0.76f, 0.98f, 1.0f, 0.98f, 0.85f, 0.68f, 1.0f, 0.98f, 0.75f, 0.93f, 1.0f, 0.73f, 0.7f, 0.98f, 1.0f};
4042
private float prevT = 0.0f;
41-
private final float colorInterpT = 0.0f;
42-
private final float gradientSpeed = 1.0f;
43+
private float colorInterpT = 0.0f;
44+
private float gradientSpeed = 1.0f;
4345

4446
public BgEffectPainter(Context context) {
47+
@Language("AGSL")
4548
String loadShader = loadShader(context.getResources(), R.raw.bg_frag);
4649
loadShader.getClass();
4750
mBgRuntimeShader = new RuntimeShader(loadShader);
@@ -147,6 +150,7 @@ private void computeGradientColor() {
147150

148151
private void executeAnim() {
149152
if (stateStyle != null) {
153+
mHandler.removeCallbacksAndMessages(null);
150154
stateStyle.setTo("colorInterpT", Float.valueOf(0.0f));
151155
stateStyle.to("colorInterpT", Float.valueOf(1.0f), animConfig1);
152156
stateStyle.to("gradientSpeed", Float.valueOf(mBgEffectData.gradientSpeedChange), animConfig1);
@@ -167,10 +171,21 @@ public void setType(BgEffectDataManager.DeviceType deviceType, BgEffectDataManag
167171
mBgRuntimeShader.setFloatUniform("uBound", uBound);
168172
mBgEffectData = mBgEffectDataManager.getData(deviceType, themeMode);
169173
uAnimTime = 0.0f;
174+
cycleCount = 0.0f;
175+
prevT = 0.0f;
176+
colorInterpT = 0.0f;
177+
gradientSpeed = mBgEffectData.gradientSpeedRest;
170178
startColorValue = mBgEffectData.gradientColors2;
171179
endColorValue = mBgEffectData.gradientColors2;
180+
linearInterpolate(uColors, startColorValue, endColorValue, colorInterpT);
181+
if (stateStyle != null) {
182+
stateStyle.cancel();
183+
stateStyle.setTo("colorInterpT", colorInterpT);
184+
stateStyle.setTo("gradientSpeed", gradientSpeed);
185+
}
172186
mBgRuntimeShader.setFloatUniform("uTranslateY", mBgEffectData.uTranslateY);
173187
mBgRuntimeShader.setFloatUniform("uPoints", mBgEffectData.uPoints);
188+
mBgRuntimeShader.setFloatUniform("uColors", uColors);
174189
mBgRuntimeShader.setFloatUniform("uNoiseScale", mBgEffectData.uNoiseScale);
175190
mBgRuntimeShader.setFloatUniform("uPointOffset", mBgEffectData.uPointOffset);
176191
mBgRuntimeShader.setFloatUniform("uPointRadiusMulti", mBgEffectData.uPointRadiusMulti);

0 commit comments

Comments
 (0)