Convert AddDeleteDrawable, HandlerDrawable and WrapDrawable to kotlin

This commit is contained in:
FooIbar authored and tarsin committed 2023-06-22 10:01:41 +08:00
1 parent 273bed9ba1
commit b602fa1204
5 files changed
+216 -263

No files matched your search

@@ -13,154 +13,136 @@
* 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.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PixelFormat
import android.graphics.drawable.Drawable
import androidx.annotation.Keep
import com.google.android.material.math.MathUtils
import com.hippo.ehviewer.R
import kotlin.math.roundToInt
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import androidx.annotation.Keep;
import com.google.android.material.math.MathUtils;
import com.hippo.ehviewer.R;
public class AddDeleteDrawable extends Drawable {
private final Paint mPaint = new Paint();
private final Path mPath = new Path();
private final int mSize;
private float mProgress;
private boolean mAutoUpdateMirror = false;
private boolean mVerticalMirror = false;
/**
* @param context used to get the configuration for the drawable from
*/
public AddDeleteDrawable(Context context, int color) {
Resources resources = context.getResources();
mSize = resources.getDimensionPixelSize(R.dimen.add_size);
float barThickness = Math.round(resources.getDimension(R.dimen.add_thickness));
mPaint.setColor(color);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.MITER);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setStrokeWidth(barThickness);
float halfSize = mSize / 2;
mPath.moveTo(0f, -halfSize);
mPath.lineTo(0, halfSize);
mPath.moveTo(-halfSize, 0);
mPath.lineTo(halfSize, 0);
/**
* @param context used to get the configuration for the drawable from
*/
class AddDeleteDrawable(context: Context, color: Int) : Drawable() {
private val mPaint = Paint()
private val mPath = Path()
private val mSize: Int
private var mProgress = 0f
private var mAutoUpdateMirror = false
private var mVerticalMirror = false
init {
val resources = context.resources
mSize = resources.getDimensionPixelSize(R.dimen.add_size)
val barThickness = resources.getDimension(R.dimen.add_thickness).roundToInt().toFloat()
mPaint.color = color
mPaint.style = Paint.Style.STROKE
mPaint.strokeJoin = Paint.Join.MITER
mPaint.strokeCap = Paint.Cap.BUTT
mPaint.strokeWidth = barThickness
val halfSize = (mSize / 2).toFloat()
mPath.moveTo(0f, -halfSize)
mPath.lineTo(0f, halfSize)
mPath.moveTo(-halfSize, 0f)
mPath.lineTo(halfSize, 0f)
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
float canvasRotate;
if (mVerticalMirror) {
canvasRotate = MathUtils.lerp(270, 135f, mProgress);
override fun draw(canvas: Canvas) {
val bounds = bounds
val canvasRotate = if (mVerticalMirror) {
MathUtils.lerp(270f, 135f, mProgress)
} else {
canvasRotate = MathUtils.lerp(0f, 135f, mProgress);
MathUtils.lerp(0f, 135f, mProgress)
}
canvas.save();
canvas.translate(bounds.centerX(), bounds.centerY());
canvas.rotate(canvasRotate);
canvas.drawPath(mPath, mPaint);
canvas.restore();
canvas.save()
canvas.translate(bounds.centerX().toFloat(), bounds.centerY().toFloat())
canvas.rotate(canvasRotate)
canvas.drawPath(mPath, mPaint)
canvas.restore()
}
public void setColor(int color) {
mPaint.setColor(color);
invalidateSelf();
fun setColor(color: Int) {
mPaint.color = color
invalidateSelf()
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
override fun setAlpha(alpha: Int) {
mPaint.alpha = alpha
}
@Override
public void setColorFilter(ColorFilter cf) {
mPaint.setColorFilter(cf);
override fun setColorFilter(cf: ColorFilter?) {
mPaint.setColorFilter(cf)
}
@Override
public int getIntrinsicHeight() {
return mSize * 6 / 5;
override fun getIntrinsicHeight(): Int {
return mSize * 6 / 5
}
@Override
public int getIntrinsicWidth() {
return mSize * 6 / 5;
override fun getIntrinsicWidth(): Int {
return mSize * 6 / 5
}
/**
* If set, canvas is flipped when progress reached to end and going back to start.
*/
protected void setVerticalMirror(boolean verticalMirror) {
mVerticalMirror = verticalMirror;
private fun setVerticalMirror(verticalMirror: Boolean) {
mVerticalMirror = verticalMirror
}
public void setAutoUpdateMirror(boolean autoUpdateMirror) {
mAutoUpdateMirror = autoUpdateMirror;
fun setAutoUpdateMirror(autoUpdateMirror: Boolean) {
mAutoUpdateMirror = autoUpdateMirror
}
@Keep
public float getProgress() {
return mProgress;
}
@Keep
public void setProgress(float progress) {
if (mAutoUpdateMirror) {
if (progress == 1f) {
setVerticalMirror(true);
} else if (progress == 0f) {
setVerticalMirror(false);
@get:Keep
@set:Keep
var progress: Float
get() = mProgress
set(progress) {
if (mAutoUpdateMirror) {
if (progress == 1f) {
setVerticalMirror(true)
} else if (progress == 0f) {
setVerticalMirror(false)
}
}
mProgress = progress
invalidateSelf()
}
mProgress = progress;
invalidateSelf();
fun setAdd(duration: Long) {
setShape(false, duration)
}
public void setAdd(long duration) {
setShape(false, duration);
fun setDelete(duration: Long) {
setShape(true, duration)
}
public void setDelete(long duration) {
setShape(true, duration);
}
public void setShape(boolean delete, long duration) {
if (!((!delete && mProgress == 0f) || (delete && mProgress == 1f))) {
float endProgress = delete ? 1f : 0f;
fun setShape(delete: Boolean, duration: Long) {
if (!(!delete && mProgress == 0f || delete && mProgress == 1f)) {
val endProgress = if (delete) 1f else 0f
if (duration <= 0) {
setProgress(endProgress);
progress = endProgress
} else {
ObjectAnimator oa = ObjectAnimator.ofFloat(this, "progress", endProgress);
oa.setDuration(duration);
oa.setAutoCancel(true);
oa.start();
val oa = ObjectAnimator.ofFloat(this, "progress", endProgress)
oa.setDuration(duration)
oa.setAutoCancel(true)
oa.start()
}
}
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
@Deprecated(
"Deprecated in Java",
ReplaceWith("PixelFormat.TRANSLUCENT", "android.graphics.PixelFormat"),
)
override fun getOpacity(): Int {
return PixelFormat.TRANSLUCENT
}
}
@@ -13,74 +13,68 @@
* 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.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
class HandlerDrawable : Drawable() {
private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val mTemp = RectF()
private var mColor = Color.BLACK
import androidx.annotation.NonNull;
public class HandlerDrawable extends Drawable {
private final Paint mPaint;
private final RectF mTemp = new RectF();
private int mColor = Color.BLACK;
public HandlerDrawable() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(mColor);
mPaint.setStyle(Paint.Style.FILL);
init {
mPaint.color = mColor
mPaint.style = Paint.Style.FILL
}
@Override
public void draw(@NonNull Canvas canvas) {
int width = getBounds().width();
int height = getBounds().height();
override fun draw(canvas: Canvas) {
val width = bounds.width()
val height = bounds.height()
if (width > height) {
canvas.drawRect(0, 0, width, height, mPaint);
canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), mPaint)
} else {
mTemp.set(0, 0, width, width);
canvas.drawArc(mTemp, -180, 180, true, mPaint);
mTemp.set(0, height - width, width, height);
canvas.drawArc(mTemp, 0, 180, true, mPaint);
float halfWidth = (float) width / 2.0f;
canvas.drawRect(0, halfWidth, width, height - halfWidth, mPaint);
mTemp[0f, 0f, width.toFloat()] = width.toFloat()
canvas.drawArc(mTemp, -180f, 180f, true, mPaint)
mTemp[0f, (height - width).toFloat(), width.toFloat()] = height.toFloat()
canvas.drawArc(mTemp, 0f, 180f, true, mPaint)
val halfWidth = width.toFloat() / 2.0f
canvas.drawRect(0f, halfWidth, width.toFloat(), height - halfWidth, mPaint)
}
}
public void setColor(int color) {
fun setColor(color: Int) {
if (mColor != color) {
mColor = color;
mPaint.setColor(color);
mColor = color
mPaint.color = color
}
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
override fun setAlpha(alpha: Int) {
mPaint.alpha = alpha
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
mPaint.setColorFilter(colorFilter);
override fun setColorFilter(colorFilter: ColorFilter?) {
mPaint.setColorFilter(colorFilter)
}
@Override
public int getOpacity() {
int alpha = Color.alpha(mColor);
if (alpha == 0xff) {
return PixelFormat.OPAQUE;
} else if (alpha == 0x00) {
return PixelFormat.TRANSPARENT;
} else {
return PixelFormat.TRANSLUCENT;
@Deprecated("Deprecated in Java")
override fun getOpacity(): Int {
return when (Color.alpha(mColor)) {
0xff -> {
PixelFormat.OPAQUE
}
0x00 -> {
PixelFormat.TRANSPARENT
}
else -> {
PixelFormat.TRANSLUCENT
}
}
}
}
@@ -51,21 +51,23 @@ class UnikeryDrawable(private val mTextView: ObservedTextView, url: String) :
drawable = null
}
override fun setDrawable(drawable: Drawable?) {
// Remove old callback
val oldDrawable = getDrawable()
if (oldDrawable != null) {
oldDrawable.callback = null
override var drawable: Drawable?
get() = super.drawable
set(drawable) {
// Remove old callback
val oldDrawable = drawable
if (oldDrawable != null) {
oldDrawable.callback = null
}
super.drawable = drawable
if (drawable != null) {
drawable.callback = mTextView
}
updateBounds()
if (drawable != null) {
invalidateSelf()
}
}
super.setDrawable(drawable)
if (drawable != null) {
drawable.callback = mTextView
}
updateBounds()
if (drawable != null) {
invalidateSelf()
}
}
override fun invalidateSelf() {
val cs = mTextView.text
@@ -74,7 +76,7 @@ class UnikeryDrawable(private val mTextView: ObservedTextView, url: String) :
private fun onGetValue(drawable: Drawable) {
clearDrawable()
setDrawable(drawable)
this.drawable = drawable
if (drawable is AnimatedImageDrawable) {
drawable.start()
}
@@ -13,131 +13,107 @@
* 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.Canvas
import android.graphics.ColorFilter
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
public class WrapDrawable extends Drawable {
private Drawable mDrawable;
public Drawable getDrawable() {
return mDrawable;
open class WrapDrawable : Drawable() {
open var drawable: Drawable? = null
fun updateBounds() {
setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}
public void setDrawable(Drawable drawable) {
mDrawable = drawable;
}
public void updateBounds() {
setBounds(0, 0, getIntrinsicWidth(), getIntrinsicHeight());
}
@Override
public void draw(Canvas canvas) {
if (mDrawable != null) {
mDrawable.draw(canvas);
override fun draw(canvas: Canvas) {
if (drawable != null) {
drawable!!.draw(canvas)
}
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
if (mDrawable != null) {
mDrawable.setBounds(left, top, right, bottom);
override fun setBounds(left: Int, top: Int, right: Int, bottom: Int) {
super.setBounds(left, top, right, bottom)
if (drawable != null) {
drawable!!.setBounds(left, top, right, bottom)
}
}
@Override
public void setBounds(Rect bounds) {
super.setBounds(bounds);
if (mDrawable != null) {
mDrawable.setBounds(bounds);
override fun setBounds(bounds: Rect) {
super.setBounds(bounds)
if (drawable != null) {
drawable!!.bounds = bounds
}
}
@Override
public int getChangingConfigurations() {
if (mDrawable != null) {
return mDrawable.getChangingConfigurations();
override fun getChangingConfigurations(): Int {
return if (drawable != null) {
drawable!!.changingConfigurations
} else {
return super.getChangingConfigurations();
super.getChangingConfigurations()
}
}
@Override
public void setChangingConfigurations(int configs) {
super.setChangingConfigurations(configs);
if (mDrawable != null) {
mDrawable.setChangingConfigurations(configs);
override fun setChangingConfigurations(configs: Int) {
super.setChangingConfigurations(configs)
if (drawable != null) {
drawable!!.changingConfigurations = configs
}
}
@Override
public void setDither(boolean dither) {
super.setDither(dither);
if (mDrawable != null) {
mDrawable.setDither(dither);
@Deprecated("Deprecated in Java")
override fun setDither(dither: Boolean) {
super.setDither(dither)
if (drawable != null) {
drawable!!.setDither(dither)
}
}
@Override
public void setFilterBitmap(boolean filter) {
super.setFilterBitmap(filter);
if (mDrawable != null) {
mDrawable.setFilterBitmap(filter);
override fun setFilterBitmap(filter: Boolean) {
super.setFilterBitmap(filter)
if (drawable != null) {
drawable!!.isFilterBitmap = filter
}
}
@Override
public void setAlpha(int alpha) {
if (mDrawable != null) {
mDrawable.setAlpha(alpha);
override fun setAlpha(alpha: Int) {
if (drawable != null) {
drawable!!.alpha = alpha
}
}
@Override
public void setColorFilter(ColorFilter cf) {
if (mDrawable != null) {
mDrawable.setColorFilter(cf);
override fun setColorFilter(cf: ColorFilter?) {
if (drawable != null) {
drawable!!.colorFilter = cf
}
}
@Override
public int getOpacity() {
if (mDrawable != null) {
return mDrawable.getOpacity();
@Deprecated(
"Deprecated in Java",
ReplaceWith("PixelFormat.TRANSLUCENT", "android.graphics.PixelFormat"),
)
override fun getOpacity(): Int {
return if (drawable != null) {
drawable!!.opacity
} else {
return PixelFormat.UNKNOWN;
PixelFormat.UNKNOWN
}
}
@Override
public int getIntrinsicWidth() {
if (mDrawable != null) {
return mDrawable.getIntrinsicWidth();
override fun getIntrinsicWidth(): Int {
return if (drawable != null) {
drawable!!.intrinsicWidth
} else {
return super.getIntrinsicWidth();
super.getIntrinsicWidth()
}
}
@Override
public int getIntrinsicHeight() {
if (mDrawable != null) {
return mDrawable.getIntrinsicHeight();
override fun getIntrinsicHeight(): Int {
return if (drawable != null) {
drawable!!.intrinsicHeight
} else {
return super.getIntrinsicHeight();
super.getIntrinsicHeight()
}
}
}
@@ -394,8 +394,7 @@ class GalleryListScene : SearchBarScene(), OnDragHandlerListener, SearchLayout.H
binding.fabLayout.addOnExpandListener(this)
addAboveSnackView(binding.fabLayout)
val colorID = theme.resolveColor(com.google.android.material.R.attr.colorOnSurface)
mActionFabDrawable =
AddDeleteDrawable(context, colorID)
mActionFabDrawable = AddDeleteDrawable(requireContext(), colorID)
binding.fabLayout.primaryFab!!.setImageDrawable(mActionFabDrawable)
mSearchFab.setOnClickListener { onApplySearch() }