Convert MarginItemDecoration, SimpleSmoothScroller and WindowInsetsAnimationHelper to kotlin
This commit is contained in:
1 parent
ada4158c8e
commit
028405ecce
4 files changed
+112
-117
No files matched your search
@@ -59,7 +59,7 @@ object LayoutManagerUtils {
|
||||
@JvmOverloads
|
||||
fun smoothScrollToPosition(
|
||||
layoutManager: RecyclerView.LayoutManager,
|
||||
context: Context?,
|
||||
context: Context,
|
||||
position: Int,
|
||||
millisecondsPerInch: Int = -1,
|
||||
) {
|
||||
@@ -106,7 +106,7 @@ object LayoutManagerUtils {
|
||||
|
||||
fun scrollToPositionProperly(
|
||||
layoutManager: RecyclerView.LayoutManager,
|
||||
context: Context?,
|
||||
context: Context,
|
||||
position: Int,
|
||||
listener: OnScrollToPositionListener?,
|
||||
) {
|
||||
|
||||
@@ -13,31 +13,37 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.hippo.ehviewer.ui.legacy
|
||||
|
||||
package com.hippo.ehviewer.ui.legacy;
|
||||
import android.graphics.Rect
|
||||
import android.view.View
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
|
||||
|
||||
import android.graphics.Rect;
|
||||
import android.view.View;
|
||||
class MarginItemDecoration(
|
||||
margin: Int,
|
||||
paddingLeft: Int,
|
||||
paddingTop: Int,
|
||||
paddingRight: Int,
|
||||
paddingBottom: Int,
|
||||
) : ItemDecoration() {
|
||||
private var mMargin = 0
|
||||
private var mPaddingLeft = 0
|
||||
private var mPaddingTop = 0
|
||||
private var mPaddingRight = 0
|
||||
private var mPaddingBottom = 0
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
public class MarginItemDecoration extends RecyclerView.ItemDecoration {
|
||||
|
||||
private int mMargin;
|
||||
private int mPaddingLeft;
|
||||
private int mPaddingTop;
|
||||
private int mPaddingRight;
|
||||
private int mPaddingBottom;
|
||||
|
||||
public MarginItemDecoration(int margin, int paddingLeft, int paddingTop,
|
||||
int paddingRight, int paddingBottom) {
|
||||
setMargin(margin, paddingLeft, paddingTop, paddingRight, paddingBottom);
|
||||
init {
|
||||
setMargin(margin, paddingLeft, paddingTop, paddingRight, paddingBottom)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getItemOffsets(Rect outRect, View view,
|
||||
RecyclerView parent, RecyclerView.State state) {
|
||||
outRect.set(mMargin, mMargin, mMargin, mMargin);
|
||||
override fun getItemOffsets(
|
||||
outRect: Rect,
|
||||
view: View,
|
||||
parent: RecyclerView,
|
||||
state: RecyclerView.State,
|
||||
) {
|
||||
outRect[mMargin, mMargin, mMargin] = mMargin
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,17 +53,22 @@ public class MarginItemDecoration extends RecyclerView.ItemDecoration {
|
||||
* @param paddingRight gap between RecyclerView right and right item right
|
||||
* @param paddingBottom gap between RecyclerView bottom and bottom item bottom
|
||||
*/
|
||||
public void setMargin(int margin, int paddingLeft, int paddingTop,
|
||||
int paddingRight, int paddingBottom) {
|
||||
int halfMargin = margin / 2;
|
||||
mMargin = halfMargin;
|
||||
mPaddingLeft = paddingLeft - halfMargin;
|
||||
mPaddingTop = paddingTop - halfMargin;
|
||||
mPaddingRight = paddingRight - halfMargin;
|
||||
mPaddingBottom = paddingBottom - halfMargin;
|
||||
fun setMargin(
|
||||
margin: Int,
|
||||
paddingLeft: Int,
|
||||
paddingTop: Int,
|
||||
paddingRight: Int,
|
||||
paddingBottom: Int,
|
||||
) {
|
||||
val halfMargin = margin / 2
|
||||
mMargin = halfMargin
|
||||
mPaddingLeft = paddingLeft - halfMargin
|
||||
mPaddingTop = paddingTop - halfMargin
|
||||
mPaddingRight = paddingRight - halfMargin
|
||||
mPaddingBottom = paddingBottom - halfMargin
|
||||
}
|
||||
|
||||
public void applyPaddings(View view) {
|
||||
view.setPadding(mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom);
|
||||
fun applyPaddings(view: View) {
|
||||
view.setPadding(mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom)
|
||||
}
|
||||
}
|
||||
@@ -13,28 +13,26 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.hippo.ehviewer.ui.legacy
|
||||
|
||||
package com.hippo.ehviewer.ui.legacy;
|
||||
import android.content.Context
|
||||
import androidx.recyclerview.widget.LinearSmoothScroller
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.ceil
|
||||
|
||||
import android.content.Context;
|
||||
abstract class SimpleSmoothScroller(context: Context, millisecondsPerInch: Float) :
|
||||
LinearSmoothScroller(context) {
|
||||
private val mMillisecondsPerPx: Float
|
||||
|
||||
import androidx.recyclerview.widget.LinearSmoothScroller;
|
||||
|
||||
public abstract class SimpleSmoothScroller extends LinearSmoothScroller {
|
||||
|
||||
private final float mMillisecondsPerPx;
|
||||
|
||||
public SimpleSmoothScroller(Context context, float millisecondsPerInch) {
|
||||
super(context);
|
||||
mMillisecondsPerPx = millisecondsPerInch / context.getResources().getDisplayMetrics().densityDpi;
|
||||
init {
|
||||
mMillisecondsPerPx = millisecondsPerInch / context.resources.displayMetrics.densityDpi
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int calculateTimeForScrolling(int dx) {
|
||||
if (mMillisecondsPerPx <= 0) {
|
||||
return super.calculateTimeForScrolling(dx);
|
||||
override fun calculateTimeForScrolling(dx: Int): Int {
|
||||
return if (mMillisecondsPerPx <= 0) {
|
||||
super.calculateTimeForScrolling(dx)
|
||||
} else {
|
||||
return (int) Math.ceil(Math.abs(dx) * mMillisecondsPerPx);
|
||||
ceil((abs(dx) * mMillisecondsPerPx).toDouble()).toInt()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,95 +15,81 @@
|
||||
* You should have received a copy of the GNU General Public License along with EhViewer.
|
||||
* If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.hippo.ehviewer.ui.legacy
|
||||
|
||||
package com.hippo.ehviewer.ui.legacy;
|
||||
import android.view.View
|
||||
import androidx.core.view.WindowInsetsAnimationCompat
|
||||
import androidx.core.view.WindowInsetsAnimationCompat.BoundsCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.google.android.material.math.MathUtils
|
||||
|
||||
import android.view.View;
|
||||
class WindowInsetsAnimationHelper(dispatchMode: Int, vararg views: View) :
|
||||
WindowInsetsAnimationCompat.Callback(dispatchMode) {
|
||||
private val views: Array<View>
|
||||
private val startPaddings = HashMap<View, Int>()
|
||||
private val endPaddings = HashMap<View, Int>()
|
||||
var animation: WindowInsetsAnimationCompat? = null
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.view.WindowInsetsAnimationCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
|
||||
import com.google.android.material.math.MathUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class WindowInsetsAnimationHelper extends WindowInsetsAnimationCompat.Callback {
|
||||
private final View[] views;
|
||||
private final HashMap<View, Integer> startPaddings = new HashMap<>();
|
||||
private final HashMap<View, Integer> endPaddings = new HashMap<>();
|
||||
WindowInsetsAnimationCompat animation;
|
||||
|
||||
public WindowInsetsAnimationHelper(int dispatchMode, View... views) {
|
||||
super(dispatchMode);
|
||||
this.views = views;
|
||||
init {
|
||||
this.views = arrayOf(*views)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrepare(@NonNull WindowInsetsAnimationCompat animation) {
|
||||
super.onPrepare(animation);
|
||||
this.animation = animation;
|
||||
for (View view : views) {
|
||||
if (view == null) {
|
||||
continue;
|
||||
}
|
||||
startPaddings.put(view, view.getPaddingBottom());
|
||||
override fun onPrepare(animation: WindowInsetsAnimationCompat) {
|
||||
super.onPrepare(animation)
|
||||
this.animation = animation
|
||||
for (view in views) {
|
||||
startPaddings[view] = view.paddingBottom
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public WindowInsetsAnimationCompat.BoundsCompat onStart(@NonNull WindowInsetsAnimationCompat animation, @NonNull WindowInsetsAnimationCompat.BoundsCompat bounds) {
|
||||
this.animation = animation;
|
||||
for (View view : views) {
|
||||
if (view == null) {
|
||||
continue;
|
||||
}
|
||||
endPaddings.put(view, view.getPaddingBottom());
|
||||
int startPadding = startPaddings.containsKey(view) ? startPaddings.get(view) : 0;
|
||||
view.setTranslationY(-(startPadding - view.getPaddingBottom()));
|
||||
override fun onStart(
|
||||
animation: WindowInsetsAnimationCompat,
|
||||
bounds: BoundsCompat,
|
||||
): BoundsCompat {
|
||||
this.animation = animation
|
||||
for (view in views) {
|
||||
endPaddings[view] = view.paddingBottom
|
||||
val startPadding = if (startPaddings.containsKey(view)) startPaddings[view]!! else 0
|
||||
view.translationY = -(startPadding - view.paddingBottom).toFloat()
|
||||
}
|
||||
return bounds;
|
||||
return bounds
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public WindowInsetsCompat onProgress(@NonNull WindowInsetsCompat insets, @NonNull List<WindowInsetsAnimationCompat> runningAnimations) {
|
||||
override fun onProgress(
|
||||
insets: WindowInsetsCompat,
|
||||
runningAnimations: List<WindowInsetsAnimationCompat>,
|
||||
): WindowInsetsCompat {
|
||||
if (animation == null) {
|
||||
return insets;
|
||||
return insets
|
||||
}
|
||||
WindowInsetsAnimationCompat imeAnimation = null;
|
||||
for (WindowInsetsAnimationCompat animation : runningAnimations) {
|
||||
if ((animation.getTypeMask() & WindowInsetsCompat.Type.ime()) != 0) {
|
||||
imeAnimation = animation;
|
||||
break;
|
||||
var imeAnimation: WindowInsetsAnimationCompat? = null
|
||||
for (animation in runningAnimations) {
|
||||
if (animation.typeMask and WindowInsetsCompat.Type.ime() != 0) {
|
||||
imeAnimation = animation
|
||||
break
|
||||
}
|
||||
}
|
||||
if (imeAnimation != null) {
|
||||
for (View view : views) {
|
||||
if (view == null) {
|
||||
continue;
|
||||
}
|
||||
int startPadding = startPaddings.containsKey(view) ? startPaddings.get(view) : 0;
|
||||
int endPadding = endPaddings.containsKey(view) ? endPaddings.get(view) : 0;
|
||||
view.setTranslationY(MathUtils.lerp(endPadding - startPadding, 0, animation.getInterpolatedFraction()));
|
||||
for (view in views) {
|
||||
val startPadding = if (startPaddings.containsKey(view)) startPaddings[view]!! else 0
|
||||
val endPadding = if (endPaddings.containsKey(view)) endPaddings[view]!! else 0
|
||||
view.translationY = MathUtils.lerp(
|
||||
(endPadding - startPadding).toFloat(),
|
||||
0f,
|
||||
animation!!.interpolatedFraction,
|
||||
)
|
||||
}
|
||||
}
|
||||
return insets;
|
||||
return insets
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnd(@NonNull WindowInsetsAnimationCompat animation) {
|
||||
super.onEnd(animation);
|
||||
startPaddings.clear();
|
||||
endPaddings.clear();
|
||||
this.animation = null;
|
||||
for (View view : views) {
|
||||
if (view == null) {
|
||||
continue;
|
||||
}
|
||||
view.setTranslationY(0);
|
||||
override fun onEnd(animation: WindowInsetsAnimationCompat) {
|
||||
super.onEnd(animation)
|
||||
startPaddings.clear()
|
||||
endPaddings.clear()
|
||||
this.animation = null
|
||||
for (view in views) {
|
||||
view.translationY = 0f
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user