|
|
@@ -0,0 +1,151 @@
|
|
|
+package com.miekir.ocr.widget;
|
|
|
+
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.graphics.BitmapFactory;
|
|
|
+import android.util.AttributeSet;
|
|
|
+import android.view.animation.Animation;
|
|
|
+import android.view.animation.TranslateAnimation;
|
|
|
+import android.widget.FrameLayout;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.appcompat.widget.AppCompatImageView;
|
|
|
+
|
|
|
+import com.miekir.ocr.R;
|
|
|
+import com.miekir.ocr.base.IScalable;
|
|
|
+import com.miekir.ocr.tool.AnimateManager;
|
|
|
+
|
|
|
+import static android.view.animation.Animation.INFINITE;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Copyright (C), 2019-2020, Miekir
|
|
|
+ *
|
|
|
+ * @author Miekir
|
|
|
+ * @date 2020/10/14 9:33
|
|
|
+ * Description: 按比例缩放的ImageView
|
|
|
+ */
|
|
|
+public class ScalableImageView extends AppCompatImageView implements IScalable {
|
|
|
+ private int mVerticalRawWidth;
|
|
|
+ private int mVerticalRawHeight;
|
|
|
+ private Bitmap mVerticalBitmap;
|
|
|
+
|
|
|
+ private int mHorizontalRawWidth;
|
|
|
+ private int mHorizontalRawHeight;
|
|
|
+ private Bitmap mHorizontalBitmap;
|
|
|
+
|
|
|
+ public ScalableImageView(@NonNull Context context) {
|
|
|
+ this(context, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ScalableImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
|
|
+ this(context, attrs, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ScalableImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
|
+ super(context, attrs, defStyleAttr);
|
|
|
+
|
|
|
+ post(() -> {
|
|
|
+ BitmapFactory.Options verticalOptions = new BitmapFactory.Options();
|
|
|
+ // verticalOptions.inJustDecodeBounds = true;
|
|
|
+ mVerticalBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.move_vertical, verticalOptions);
|
|
|
+ mVerticalRawWidth = verticalOptions.outWidth;
|
|
|
+ mVerticalRawHeight = verticalOptions.outHeight;
|
|
|
+
|
|
|
+ BitmapFactory.Options horizontalOptions = new BitmapFactory.Options();
|
|
|
+ // verticalOptions.inJustDecodeBounds = true;
|
|
|
+ mHorizontalBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.move_horizontal, horizontalOptions);
|
|
|
+ mHorizontalRawWidth = horizontalOptions.outWidth;
|
|
|
+ mHorizontalRawHeight = horizontalOptions.outHeight;
|
|
|
+
|
|
|
+ setImageBitmap(mVerticalBitmap);
|
|
|
+ AnimateManager.getInstance().register(this);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDetachedFromWindow() {
|
|
|
+ super.onDetachedFromWindow();
|
|
|
+ AnimateManager.getInstance().unRegister();
|
|
|
+ }
|
|
|
+
|
|
|
+ private int width;
|
|
|
+ private int height;
|
|
|
+ @Override
|
|
|
+ public void setAnimateWidth(int w, int h, int left, int top, int right, int bottom) {
|
|
|
+ if (mVerticalRawWidth == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ setImageBitmap(mHorizontalBitmap);
|
|
|
+
|
|
|
+ width = (h * mHorizontalRawWidth)/ mHorizontalRawHeight;
|
|
|
+ this.height = h;
|
|
|
+ setMinimumWidth(width);
|
|
|
+ setMaxWidth(width);
|
|
|
+ setMinimumHeight(height);
|
|
|
+ setMaxHeight(height);
|
|
|
+// FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
|
|
|
+// params.width = width;
|
|
|
+// params.height = h;
|
|
|
+
|
|
|
+ startHorizontalAnimation(left, right-width, top);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setAnimateHeight(int w, int h, int left, int top, int right, int bottom) {
|
|
|
+ if (mVerticalRawWidth == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ setImageBitmap(mVerticalBitmap);
|
|
|
+
|
|
|
+ this.width = w;
|
|
|
+ height = (w * mVerticalRawHeight)/ mVerticalRawWidth;
|
|
|
+ setMinimumWidth(width);
|
|
|
+ setMaxWidth(width);
|
|
|
+ setMinimumHeight(height);
|
|
|
+ setMaxHeight(height);
|
|
|
+// FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
|
|
|
+// params.width = w;
|
|
|
+// params.height = height;
|
|
|
+
|
|
|
+ startVerticalAnimation(top, bottom-height, left);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Animation animation;
|
|
|
+ private void startHorizontalAnimation(int startX, int endX, int y) {
|
|
|
+ setVisibility(VISIBLE);
|
|
|
+
|
|
|
+ if (animation != null) {
|
|
|
+ animation.cancel();
|
|
|
+ }
|
|
|
+ animation = new TranslateAnimation(startX, endX, y, y);
|
|
|
+ animation.setRepeatMode(INFINITE);
|
|
|
+ animation.setRepeatCount(INFINITE);
|
|
|
+ animation.setDuration(2000);
|
|
|
+ startAnimation(animation);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startVerticalAnimation(int startY, int endY, int x) {
|
|
|
+ setVisibility(VISIBLE);
|
|
|
+
|
|
|
+ if (animation != null) {
|
|
|
+ animation.cancel();
|
|
|
+ }
|
|
|
+ animation = new TranslateAnimation(x, x, startY, endY);
|
|
|
+ animation.setRepeatMode(INFINITE);
|
|
|
+ animation.setRepeatCount(INFINITE);
|
|
|
+ animation.setDuration(2000);
|
|
|
+ startAnimation(animation);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void stopTranslateAnimation() {
|
|
|
+ setVisibility(INVISIBLE);
|
|
|
+ if (animation != null) {
|
|
|
+ animation.cancel();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|