Prechádzať zdrojové kódy

搞定动画View的宽高

詹子聪 5 rokov pred
rodič
commit
af29f8b2ca

+ 2 - 1
app/src/main/java/com/miekir/ocr/widget/CropView.java

@@ -119,7 +119,8 @@ public class CropView extends View {
                 // 邮箱
                 coverWidth = (int) (getWidth()*0.815);
                 // 最小高度不能小于图片的高度
-                coverHeight = Math.max(minHeight, (int)(getWidth()*0.3));
+//                coverHeight = Math.max(minHeight, (int)(getWidth()*0.3));
+                coverHeight = SizeUtils.dp2px(50);
                 sX = (getWidth()*1.0f-coverWidth)/2;
                 sY = getWidth()*0.177f;
                 mFrameRectF = new RectF(sX, sY, sX + coverWidth, sY + coverHeight);

+ 35 - 39
app/src/main/java/com/miekir/ocr/widget/ScalableImageView.java

@@ -5,7 +5,6 @@ import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.util.AttributeSet;
-import android.util.Log;
 import android.view.animation.Animation;
 import android.view.animation.TranslateAnimation;
 import android.widget.FrameLayout;
@@ -38,8 +37,6 @@ public class ScalableImageView extends AppCompatImageView implements IScalable {
     private int mHorizontalRawHeight;
     private Bitmap mHorizontalBitmap;
 
-    private int parentWidth;
-    private int parentHeight;
 
     public ScalableImageView(@NonNull Context context) {
         this(context, null);
@@ -51,7 +48,6 @@ public class ScalableImageView extends AppCompatImageView implements IScalable {
 
     public ScalableImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
-
         post(() -> {
             BitmapFactory.Options verticalOptions = new BitmapFactory.Options();
             // verticalOptions.inJustDecodeBounds = true;
@@ -65,10 +61,6 @@ public class ScalableImageView extends AppCompatImageView implements IScalable {
             mHorizontalRawWidth =  horizontalOptions.outWidth * 2;
             mHorizontalRawHeight = horizontalOptions.outHeight * 2;
 
-            FrameLayout frameLayout = (FrameLayout)getParent();
-            parentWidth = frameLayout.getWidth();
-            parentHeight = frameLayout.getHeight();
-
             setImageBitmap(mVerticalBitmap);
             AnimateManager.getInstance().register(this);
         });
@@ -88,26 +80,29 @@ public class ScalableImageView extends AppCompatImageView implements IScalable {
             return;
         }
 
-        setImageBitmap(mHorizontalBitmap);
-
-        this.height = bottom-top;
-        width = (height * mHorizontalRawWidth)/ mHorizontalRawHeight;
+        // 需要在post里执行,否则setMinimumWidth或者setLayoutParams都不起作用
+        post(new Runnable() {
+            @Override
+            public void run() {
+                height = bottom-top;
+                width = (height * mHorizontalRawWidth)/ mHorizontalRawHeight;
 
+                //int minWidth = width < mHorizontalRawWidth ? mHorizontalRawWidth : width;
+                setMinimumWidth(width);
+                setMaxWidth(width);
 
-        int minWidth = width < mHorizontalRawWidth ? mHorizontalRawWidth : width;
-        setMinimumWidth(minWidth);
-        setMaxWidth(minWidth);
+                //int minHeight = height < mHorizontalRawHeight ? mHorizontalRawHeight : height;
+                setMinimumHeight(height);
+                setMaxHeight(height);
 
-        int minHeight = height < mHorizontalRawHeight ? mHorizontalRawHeight : height;
-        setMinimumHeight(minHeight);
-        setMaxHeight(minHeight);
-        Log.e(TAG, "min_width:" + minWidth);
-        Log.e(TAG, "min_height:" + minHeight);
-//        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
-//        params.width = width;
-//        params.height = h;
+                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
+                params.width = width;
+                params.height = height;
+                setImageBitmap(mHorizontalBitmap);
 
-        startHorizontalAnimation(left, right-width, top);
+                startHorizontalAnimation(left, right-width, top);
+            }
+        });
     }
 
     @Override
@@ -116,25 +111,26 @@ public class ScalableImageView extends AppCompatImageView implements IScalable {
             return;
         }
 
-        setImageBitmap(mVerticalBitmap);
+        post(new Runnable() {
+            @Override
+            public void run() {
+                width = right-left;
+                height = (width * mVerticalRawHeight)/ mVerticalRawWidth;
 
-        this.width = right-left;
-        height = (width * mVerticalRawHeight)/ mVerticalRawWidth;
+                setMinimumWidth(width);
+                setMaxWidth(width);
 
-        int minWidth = width < mVerticalRawWidth ? mVerticalRawWidth : width;
-        setMinimumWidth(minWidth);
-        setMaxWidth(minWidth);
+                setMinimumHeight(height);
+                setMaxHeight(height);
 
-        int minHeight = height < mVerticalRawHeight ? mVerticalRawHeight : height;
-        setMinimumHeight(minHeight);
-        setMaxHeight(minHeight);
-        Log.e(TAG, "min_width:" + minWidth);
-        Log.e(TAG, "min_height:" + minHeight);
-//        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
-//        params.width = w;
-//        params.height = height;
+                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) getLayoutParams();
+                params.width = width;
+                params.height = height;
 
-        startVerticalAnimation(top, bottom-height, left);
+                setImageBitmap(mVerticalBitmap);
+                startVerticalAnimation(top, bottom-height, left);
+            }
+        });
     }
 
     private Animation animation;