Skip to content

Commit

Permalink
[skp] opt: HolidayViews
Browse files Browse the repository at this point in the history
  • Loading branch information
lingqiqi5211 committed Feb 3, 2025
1 parent 1cf695e commit 4d6b6b5
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.Surface;
import android.view.WindowManager;

import com.sevtinge.hyperceiler.R;
import com.sevtinge.hyperceiler.ui.app.holiday.weather.confetto.Confetto;
Expand All @@ -19,24 +18,28 @@ public class FlowerParticle extends Confetto {
private final ConfettoInfo confettoInfo;
private final Bitmap petal;
private float petalScale;
private final int[] petals = new int[] { R.drawable.confetti1, R.drawable.confetti1, R.drawable.confetti2, R.drawable.confetti2, R.drawable.confetti3, R.drawable.confetti3, R.drawable.petal };

FlowerParticle(Context context, ConfettoInfo confettoInfo) {
super();
this.confettoInfo = confettoInfo;
petalScale = 0.6f - (float)Math.random() * 0.15f;
petal = BitmapFactory.decodeResource(context.getResources(), petals[new Random().nextInt(petals.length)]);
this.petalScale = 0.6f - (float)Math.random() * 0.15f;
int[] petals = {R.drawable.confetti1, R.drawable.confetti1, R.drawable.confetti2, R.drawable.confetti2, R.drawable.confetti3, R.drawable.confetti3, R.drawable.petal};
this.petal = BitmapFactory.decodeResource(context.getResources(), petals[new Random().nextInt(petals.length)]);

int rotation = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) petalScale *= 1.5;
int rotation = context.getDisplay().getRotation();
if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
this.petalScale *= 1.5f;
}
}

@Override
public int getHeight() {
return 0;
return petal.getHeight();
}

@Override
public int getWidth() {
return 0;
return petal.getWidth();
}

public void reset() {
Expand All @@ -49,6 +52,7 @@ public void configurePaint(Paint paint) {
paint.setAntiAlias(true);
}

@Override
protected void drawInternal(Canvas canvas, Matrix matrix, Paint paint, float x, float y, float rotation, float percentageAnimated) {
switch (confettoInfo.getPrecipType()) {
case CLEAR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,31 @@ public void onSensorChanged(SensorEvent event) {
if (this.magneticValues == null || this.accelerometerValues == null) return;

float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrix(rotationMatrix, null, this.accelerometerValues, this.magneticValues);
float[] remappedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, remappedRotationMatrix);
float[] orientationAngles = new float[3];

SensorManager.getRotationMatrix(rotationMatrix, null, this.accelerometerValues, this.magneticValues);
SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, remappedRotationMatrix);
SensorManager.getOrientation(remappedRotationMatrix, orientationAngles);
//double pitch = Math.toDegrees((double)orientationAngles[1]);

double roll = Math.toDegrees(orientationAngles[2]) + Math.random() * 20 - 10;
if (this.orientation == Surface.ROTATION_90) roll += 90;
else if (this.orientation == Surface.ROTATION_270) roll -= 90;
else if (this.orientation == Surface.ROTATION_180) roll += roll > 0 ? 180 : -180;
if (roll > 90) roll -= 180; else if (roll < -90) roll += 180;
this.weatherView.setAngle((int)roll);
this.weatherView.setSpeed(this.speed + (int)Math.round(Math.random() * 20 - 10));
switch (this.orientation) {
case Surface.ROTATION_90:
roll += 90;
break;
case Surface.ROTATION_270:
roll -= 90;
break;
case Surface.ROTATION_180:
roll += roll > 0 ? 180 : -180;
break;
}

if (roll > 90) roll -= 180;
else if (roll < -90) roll += 180;

this.weatherView.setAngle((int) roll);
this.weatherView.setSpeed(this.speed + (int) Math.round(Math.random() * 20 - 10));
}

private void registerListener() {
Expand All @@ -71,31 +83,31 @@ private void unregisterListener() {
this.sensorManager.unregisterListener(this);
}

public final void start() {
public void start() {
this.started = true;
this.registerListener();
}

public final void stop() {
public void stop() {
this.started = false;
this.unregisterListener();
}

public final void onResume() {
public void onResume() {
if (this.started) {
this.registerListener();
}
}

public final void onPause() {
public void onPause() {
this.unregisterListener();
}

public final Context getContext() {
public Context getContext() {
return this.context;
}

public final WeatherView getWeatherView() {
public WeatherView getWeatherView() {
return this.weatherView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.Objects;

import fan.navigator.app.NavigatorActivity;

Expand All @@ -36,8 +37,8 @@ public class HolidayHelper {

public HolidayHelper(Activity activity) {
mContext = activity;
mRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
mContentView = activity.findViewById(android.R.id.content);
mRotation = Objects.requireNonNull(mContext.getDisplay()).getRotation();
mContentView = activity.findViewById(android.R.id.content);
mHolidayView = LayoutInflater.from(mContext).inflate(R.layout.layout_holiday, mContentView, false);
initialize(activity instanceof NavigatorActivity);
}
Expand Down Expand Up @@ -68,68 +69,58 @@ private void initView() {

private void initHoliday() {
mWeatherView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

weatherView = new WeakReference<>(mWeatherView);
GravitySensor listener = null;
if (opt == 1) {
mWeatherView.setPrecipType(PrecipType.SNOW);
mWeatherView.setSpeed(50);
mWeatherView.setEmissionRate(mRotation == Surface.ROTATION_90 || mRotation == Surface.ROTATION_270 ? 8 : 4);
mWeatherView.setFadeOutPercent(0.75f);
mWeatherView.setAngle(0);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)mWeatherView.getLayoutParams();
lp.height = mContext.getResources().getDisplayMetrics().heightPixels / (mRotation == Surface.ROTATION_90 || mRotation == Surface.ROTATION_270 ? 2 : 3);
mWeatherView.setLayoutParams(lp);
setWeatherGenerator(new SnowGenerator(mContext));
mWeatherView.resetWeather();
mWeatherView.setVisibility(View.VISIBLE);
mWeatherView.getConfettiManager().setRotationalVelocity(0, 45);

listener = new GravitySensor(mContext, mWeatherView);
listener.setOrientation(mRotation);
listener.setSpeed(50);
listener.start();

mHeaderView.setImageResource(R.drawable.newyear_header);
mHeaderView.setVisibility(View.VISIBLE);
} else if (opt == 2) {
mWeatherView.setPrecipType(PrecipType.SNOW);
mWeatherView.setSpeed(35);
mWeatherView.setEmissionRate(mRotation == Surface.ROTATION_90 || mRotation == Surface.ROTATION_270 ? 4 : 2);
mWeatherView.setFadeOutPercent(0.75f);
mWeatherView.setAngle(0);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)mWeatherView.getLayoutParams();
lp.height = mContext.getResources().getDisplayMetrics().heightPixels / (mRotation == Surface.ROTATION_90 || mRotation == Surface.ROTATION_270 ? 3 : 4);
mWeatherView.setLayoutParams(lp);
setWeatherGenerator(new FlowerGenerator(mContext));
mWeatherView.resetWeather();
mWeatherView.setVisibility(View.VISIBLE);
mWeatherView.getConfettiManager().setRotationalVelocity(0, 45);

listener = new GravitySensor(mContext, mWeatherView);
listener.setOrientation(mRotation);
listener.setSpeed(35);
listener.start();

mHeaderView.setImageResource(R.drawable.lunar_newyear_header);
mHeaderView.setVisibility(View.VISIBLE);
} else if (opt == 3) {
mWeatherView.setPrecipType(PrecipType.CLEAR);
mWeatherView.setSpeed(0);
mWeatherView.setEmissionRate(0.6f);
mWeatherView.setFadeOutPercent(1.0f);
mWeatherView.setAngle(0);
setWeatherGenerator(new CoinGenerator(mContext));
mWeatherView.resetWeather();
mWeatherView.setVisibility(View.VISIBLE);
mWeatherView.getConfettiManager().setRotationalVelocity(0, 15).setTTL(30000);

mHeaderView.setImageResource(R.drawable.crypto_header);
mHeaderView.setVisibility(View.VISIBLE);

mWeatherView.setPrecipType(opt == 3 ? PrecipType.CLEAR : PrecipType.SNOW);
mWeatherView.setSpeed(opt == 1 ? 50 : (opt == 2 ? 35 : 0));
mWeatherView.setEmissionRate(mRotation == Surface.ROTATION_90 || mRotation == Surface.ROTATION_270 ? (opt == 1 ? 8 : 4) : (opt == 1 ? 4 : 2));
mWeatherView.setFadeOutPercent(opt == 3 ? 1.0f : 0.75f);
mWeatherView.setAngle(0);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mWeatherView.getLayoutParams();
lp.height = mContext.getResources().getDisplayMetrics().heightPixels / (mRotation == Surface.ROTATION_90 || mRotation == Surface.ROTATION_270 ? (opt == 1 ? 2 : 3) : (opt == 1 ? 3 : 4));
mWeatherView.setLayoutParams(lp);
setWeatherGenerator(opt == 1 ? new SnowGenerator(mContext) : (opt == 2 ? new FlowerGenerator(mContext) : new CoinGenerator(mContext)));
mWeatherView.resetWeather();
mWeatherView.setVisibility(View.VISIBLE);
mWeatherView.getConfettiManager().setRotationalVelocity(0, opt == 3 ? 15 : 45);
if (opt == 3) {
mWeatherView.getConfettiManager().setTTL(30000);
}
GravitySensor listener = new GravitySensor(mContext, mWeatherView);
listener.setOrientation(mRotation);
listener.setSpeed(opt == 1 ? 50 : 35);
listener.start();

setupHeaderView(opt);
angleListener = new WeakReference<>(listener);
}

public static void pauseAnimation() {
if (angleListener != null) {
GravitySensor listener = angleListener.get();
if (listener != null) {
listener.stop();
}
angleListener.clear();
}
}

public static void resumeAnimation() {
if (angleListener != null) {
GravitySensor listener = angleListener.get();
if (listener != null) {
listener.start();
}
angleListener.clear();
}
}

private void setupHeaderView(int opt) {
int headerResId = opt == 1 ? R.drawable.newyear_header : (opt == 2 ? R.drawable.lunar_newyear_header : R.drawable.crypto_header);
mHeaderView.setImageResource(headerResId);
mHeaderView.setVisibility(View.VISIBLE);
}

private void setWeatherGenerator(ConfettoGenerator generator) {
try {
ConfettiManager manager = weatherView.get().getConfettiManager();
Expand All @@ -140,4 +131,4 @@ private void setWeatherGenerator(ConfettoGenerator generator) {
t.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,29 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
}
}

@Override
public void onResume() {
super.onResume();
if (isLunarNewYearThemeView) {
HolidayHelper.resumeAnimation();
}
}

@Override
protected void onPause() {
super.onPause();
if (isLunarNewYearThemeView) {
HolidayHelper.pauseAnimation();
}
}

@Override
public void onDestroy() {
super.onDestroy();
ShellInit.destroy();
ThreadPoolManager.shutdown();
PreferenceHeader.mUninstallApp.clear();
PreferenceHeader.mDisableOrHiddenApp.clear();
super.onDestroy();
}

// 权限申请
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import com.sevtinge.hyperceiler.ui.base.SettingsPreferenceFragment;
import com.sevtinge.hyperceiler.utils.prefs.PrefsUtils;

import fan.pickerwidget.color.HSLColor;
import fan.preference.ColorPickerPreference;

public class CustomBackgroundSettings extends SettingsPreferenceFragment implements Preference.OnPreferenceChangeListener {

private String mKey = "";
private String mCustomBackgroundEnabledKey;
private String mColorKey;
private String mCornerRadiusKey;
Expand All @@ -59,7 +59,7 @@ public void initPrefs() {
Bundle args = getArguments();

if (args != null) {
mKey = args.getString("key");
String mKey = args.getString("key");

mCustomBackgroundEnabledKey = mKey + "_custom_enable";

Expand Down Expand Up @@ -125,7 +125,7 @@ public boolean onPreferenceChange(@NonNull Preference preference, Object o) {
if (preference == mCustomEnabledPreference) {
setCustomEnable((Boolean) o);
} else if (preference == mColorPickerPreference) {
setBackgroundColor((int) o);
setBackgroundColor((HSLColor) o);
} else if (preference == mCornerRadiusPreference) {
setBackgroundCornerRadius((int) o);
} else if (preference == mBlurEnabledPreference) {
Expand All @@ -141,9 +141,9 @@ private void setCustomEnable(boolean isCustomEnabled) {
PrefsUtils.mSharedPreferences.edit().putBoolean(mCustomBackgroundEnabledKey, isCustomEnabled).apply();
}

private void setBackgroundColor(int value) {
mColorPickerPreference.setColor(value);
PrefsUtils.mSharedPreferences.edit().putInt(mColorKey, value).apply();
private void setBackgroundColor(HSLColor value) {
mColorPickerPreference.setColor(value.color);
PrefsUtils.mSharedPreferences.edit().putInt(mColorKey, value.color).apply();
}

private void setBackgroundCornerRadius(int value) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/layout_holiday.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0.1dp"
android:paddingRight="0.1dp"
android:paddingEnd="0.1dp"
android:animateLayoutChanges="true"
android:fitsSystemWindows="false">

Expand Down

0 comments on commit 4d6b6b5

Please sign in to comment.