From af2a1900bd85c961ddebba9872f9d32d9e95a2e3 Mon Sep 17 00:00:00 2001 From: Albert Grobas Date: Fri, 29 May 2015 17:57:15 +0200 Subject: [PATCH] fixes a bug on update image --- README.md | 6 +++--- .../java/net/grobas/view/MovingImageView.java | 16 +++++++--------- .../movingimageview/sample/SampleActivity.java | 3 ++- sample/src/main/res/layout/activity_sample.xml | 4 ++-- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 214f47a..c83e1c6 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ MovingImageView =============== +[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-MovingImageView-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/1850) Create a custom ImageView for moving image around the screen. - ![Demo Screenshot 1][1] ![Demo Screenshot 2][2] @@ -17,10 +17,10 @@ To use MovingImageView, add the module into your project and start to build xml diff --git a/movingimageview/src/main/java/net/grobas/view/MovingImageView.java b/movingimageview/src/main/java/net/grobas/view/MovingImageView.java index f0e4caf..c9aa087 100644 --- a/movingimageview/src/main/java/net/grobas/view/MovingImageView.java +++ b/movingimageview/src/main/java/net/grobas/view/MovingImageView.java @@ -111,7 +111,7 @@ private void updateAll() { private void updateImageSize() { imageWidth = getDrawable().getIntrinsicWidth(); - imageHeight = getDrawable().getMinimumHeight(); + imageHeight = getDrawable().getIntrinsicHeight(); } /** @@ -154,8 +154,8 @@ private void updateAnimator() { * @return image scale. */ private float calculateTypeAndScale() { - float scale = 1f; movementType = MovingViewAnimator.AUTO_MOVE; + float scale = 1f; float scaleByImage = Math.max(imageWidth / canvasWidth, imageHeight / canvasHeight); Matrix m = new Matrix(); @@ -168,16 +168,16 @@ private float calculateTypeAndScale() { scale = Math.min(sW, maxRelativeSize); m.setTranslate((canvasWidth - imageWidth * scale) / 2f, 0); movementType = MovingViewAnimator.VERTICAL_MOVE; + } else if (sW < sH) { scale = Math.min(sH, maxRelativeSize); m.setTranslate(0, (canvasHeight - imageHeight * scale) / 2f); movementType = MovingViewAnimator.HORIZONTAL_MOVE; + } else { scale = Math.max(sW, maxRelativeSize); - if (scale == sW) - movementType = MovingViewAnimator.NONE_MOVE; - else - movementType = MovingViewAnimator.DIAGONAL_MOVE; + movementType = (scale == sW) ? MovingViewAnimator.NONE_MOVE : + MovingViewAnimator.DIAGONAL_MOVE; } //Width too small to perform any horizontal animation, scale to width @@ -193,9 +193,7 @@ private float calculateTypeAndScale() { //Enough size but too big, resize down } else if (scaleByImage > maxRelativeSize) { scale = maxRelativeSize / scaleByImage; - float newW = imageWidth * scale; - float newH = imageHeight * scale; - if(newW < canvasWidth || newH < canvasHeight) { + if(imageWidth * scale < canvasWidth || imageHeight * scale < canvasHeight) { scale = Math.max(canvasWidth / imageWidth, canvasHeight / imageHeight); } } diff --git a/sample/src/main/java/net/grobas/movingimageview/sample/SampleActivity.java b/sample/src/main/java/net/grobas/movingimageview/sample/SampleActivity.java index 04e7412..9443186 100644 --- a/sample/src/main/java/net/grobas/movingimageview/sample/SampleActivity.java +++ b/sample/src/main/java/net/grobas/movingimageview/sample/SampleActivity.java @@ -14,13 +14,14 @@ public class SampleActivity extends AppCompatActivity { MovingImageView image; boolean toggleState = true; boolean toggleCustomMovement = true; - int[] imageList = {R.drawable.skyline, R.drawable.futurecity, R.drawable.spacecargo, R.drawable.city}; + int[] imageList = {R.drawable.anotherworld, R.drawable.futurecity, R.drawable.spacecargo, R.drawable.city}; int pos = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sample); + image = (MovingImageView) findViewById(R.id.image); image.getMovingAnimator().addListener(new Animator.AnimatorListener() { @Override diff --git a/sample/src/main/res/layout/activity_sample.xml b/sample/src/main/res/layout/activity_sample.xml index dfc3918..8203639 100644 --- a/sample/src/main/res/layout/activity_sample.xml +++ b/sample/src/main/res/layout/activity_sample.xml @@ -15,7 +15,7 @@ android:layout_height="250dp" android:clickable="true" android:onClick="clickImage" - android:src="@drawable/skyline" + android:src="@drawable/anotherworld" app:miv_load_on_create="true" app:miv_max_relative_size="3.0" app:miv_min_relative_offset="0.2" @@ -43,7 +43,7 @@ android:layout_below="@id/image" android:clickable="true" android:onClick="clickText" - android:padding="15dp" + android:padding="16dp" android:text="@string/text" />