diff --git a/app/src/main/java/com/hippo/ehviewer/ui/legacy/AboveSnackerLayout.kt b/app/src/main/java/com/hippo/ehviewer/ui/legacy/AboveSnackerLayout.kt index 8e3fd1e00..588a52c86 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/legacy/AboveSnackerLayout.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/legacy/AboveSnackerLayout.kt @@ -17,7 +17,7 @@ import androidx.drawerlayout.widget.DrawerLayout.DrawerListener import androidx.interpolator.view.animation.FastOutSlowInInterpolator import com.drakeet.drawer.FullDraggableHelper import com.google.android.material.snackbar.Snackbar.SnackbarLayout -import com.hippo.ehviewer.yorozuya.LayoutUtils.dp2pix +import com.hippo.ehviewer.yorozuya.dp2px class AboveSnackerLayout : FrameLayout, AttachedBehavior, FullDraggableHelper.Callback { private val helper: FullDraggableHelper @@ -195,7 +195,7 @@ class AboveSnackerLayout : FrameLayout, AttachedBehavior, FullDraggableHelper.Ca val view = child.getAboveSnackViewAt(i) if (view != null) { val translationY = - (dependency.translationY - dependency.height - dp2pix(view.context, 8f)) + (dependency.translationY - dependency.height - dp2px(view.context, 8f)) .coerceAtMost(0f) ViewCompat.animate(view).setInterpolator(FastOutSlowInInterpolator()) .translationY(translationY).setDuration(150).start() diff --git a/app/src/main/java/com/hippo/ehviewer/ui/legacy/ContentLayout.kt b/app/src/main/java/com/hippo/ehviewer/ui/legacy/ContentLayout.kt index af747f3be..6cb4f06b4 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/legacy/ContentLayout.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/legacy/ContentLayout.kt @@ -35,8 +35,8 @@ import com.hippo.ehviewer.ui.legacy.ViewTransition.OnShowViewListener import com.hippo.ehviewer.util.ExceptionUtils import com.hippo.ehviewer.util.getParcelableCompat import com.hippo.ehviewer.yorozuya.IntIdGenerator -import com.hippo.ehviewer.yorozuya.LayoutUtils import com.hippo.ehviewer.yorozuya.collect.IntList +import com.hippo.ehviewer.yorozuya.dp2px import rikka.core.res.resolveColor class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout(context, attrs) { @@ -82,7 +82,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout binding.refreshLayout.setProgressViewOffset( true, 0, - fitPaddingTop + LayoutUtils.dp2pix(context, 32f), + fitPaddingTop + dp2px(context, 32f), ) // TODO } @@ -112,7 +112,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout binding.fastScroller.paddingRight, mFastScrollerOriginBottom + fitPaddingBottom, ) - if (fitPaddingBottom > LayoutUtils.dp2pix(context, 16f)) { + if (fitPaddingBottom > dp2px(context, 16f)) { binding.bottomProgress.setPadding(0, 0, 0, fitPaddingBottom) } else { binding.bottomProgress.setPadding(0, 0, 0, 0) @@ -235,7 +235,7 @@ class ContentLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout } fun init(contentLayout: ContentLayout) { binding = contentLayout.binding - mNextPageScrollSize = LayoutUtils.dp2pix(contentLayout.context, 48f) + mNextPageScrollSize = dp2px(contentLayout.context, 48f) val drawable = ContextCompat.getDrawable(context, R.drawable.big_sad_pandroid)!! drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight) binding.tip.setCompoundDrawables(null, drawable, null, null) diff --git a/app/src/main/java/com/hippo/ehviewer/ui/legacy/FastScroller.kt b/app/src/main/java/com/hippo/ehviewer/ui/legacy/FastScroller.kt index 938a9949a..347f738ac 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/legacy/FastScroller.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/legacy/FastScroller.kt @@ -30,9 +30,9 @@ import androidx.recyclerview.widget.RecyclerView.AdapterDataObserver import com.google.android.material.math.MathUtils.dist import com.hippo.ehviewer.R import com.hippo.ehviewer.yorozuya.AnimationUtils -import com.hippo.ehviewer.yorozuya.LayoutUtils.dp2pix import com.hippo.ehviewer.yorozuya.SimpleAnimatorListener import com.hippo.ehviewer.yorozuya.SimpleHandler +import com.hippo.ehviewer.yorozuya.dp2px import kotlin.math.abs class FastScroller : View { @@ -79,7 +79,7 @@ class FastScroller : View { a.recycle() alpha = 0.0f visibility = INVISIBLE - mMinHandlerHeight = dp2pix(context, MIN_HANDLER_HEIGHT_DP.toFloat()) + mMinHandlerHeight = dp2px(context, MIN_HANDLER_HEIGHT_DP.toFloat()) mTouchSlop = ViewConfiguration.get(context).scaledTouchSlop mShowAnimator = ObjectAnimator.ofFloat(this, "alpha", 1.0f).apply { interpolator = AnimationUtils.FAST_SLOW_INTERPOLATOR diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.kt index 27abd4848..5d532a9f3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.kt @@ -32,7 +32,7 @@ import com.hippo.ehviewer.ui.legacy.AutoStaggeredGridLayoutManager import com.hippo.ehviewer.ui.legacy.MarginItemDecoration import com.hippo.ehviewer.ui.legacy.STRATEGY_MIN_SIZE import com.hippo.ehviewer.ui.legacy.STRATEGY_SUITABLE_SIZE -import com.hippo.ehviewer.yorozuya.LayoutUtils.dp2pix +import com.hippo.ehviewer.yorozuya.dp2px import splitties.init.appCtx abstract class GalleryAdapter( @@ -100,7 +100,7 @@ abstract class GalleryAdapter( } TYPE_GRID -> { - val columnWidth = dp2pix(appCtx, thumbSizeDp.toFloat()) + val columnWidth = dp2px(appCtx, thumbSizeDp.toFloat()) mLayoutManager.setColumnSize(columnWidth) mLayoutManager.setStrategy(STRATEGY_SUITABLE_SIZE) if (null != mListDecoration) { diff --git a/app/src/main/java/com/hippo/ehviewer/yorozuya/LayoutUtils.java b/app/src/main/java/com/hippo/ehviewer/yorozuya/LayoutUtils.java deleted file mode 100644 index 2b3babce9..000000000 --- a/app/src/main/java/com/hippo/ehviewer/yorozuya/LayoutUtils.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2015 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.ehviewer.yorozuya; - -import android.content.Context; - -public final class LayoutUtils { - private LayoutUtils() { - } - - /** - * dp conversion to pix - * - * @param context The context - * @param dp The value you want to conversion - * @return value in pix - */ - public static int dp2pix(Context context, float dp) { - return (int) (dp * context.getResources().getDisplayMetrics().density + 0.5f); - } - - /** - * pix conversion to dp - * - * @param context The context - * @param pix The value you want to conversion - * @return value in dp - */ - public static float pix2dp(Context context, int pix) { - return pix / context.getResources().getDisplayMetrics().density; - } - - /** - * sp conversion to pix - * - * @param sp The value you want to conversion - * @return value in pix - */ - public static int sp2pix(Context context, float sp) { - return (int) (sp * context.getResources().getDisplayMetrics().scaledDensity + 0.5f); - } - - /** - * pix conversion to sp - * - * @param pix The value you want to conversion - * @return value in sp - */ - public static float pix2sp(Context context, float pix) { - return pix / context.getResources().getDisplayMetrics().scaledDensity; - } -} diff --git a/app/src/main/java/com/hippo/ehviewer/yorozuya/LayoutUtils.kt b/app/src/main/java/com/hippo/ehviewer/yorozuya/LayoutUtils.kt new file mode 100644 index 000000000..7fa1f89aa --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/yorozuya/LayoutUtils.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2015 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.hippo.ehviewer.yorozuya + +import android.content.Context +import android.util.TypedValue.COMPLEX_UNIT_DIP +import android.util.TypedValue.applyDimension +import kotlin.math.roundToInt + +fun dp2px(context: Context, dp: Float): Int { + return applyDimension(COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics).roundToInt() +}