Skip to content

Commit

Permalink
fixes a bug on update image
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertGrobas committed May 29, 2015
1 parent 7d262de commit af2a190
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
6 changes: 3 additions & 3 deletions 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]

Expand All @@ -17,10 +17,10 @@ To use MovingImageView, add the module into your project and start to build xml
<net.grobas.view.MovingImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@drawable/beach"
android:src="@drawable/image"
app:miv_load_on_create="true"
app:miv_max_relative_size="3"
app:miv_min_relative_offset="0.1"
app:miv_min_relative_offset="0.2"
app:miv_start_delay="1000"
app:miv_repetitions="-1"
app:miv_speed="100" />
Expand Down
Expand Up @@ -111,7 +111,7 @@ private void updateAll() {

private void updateImageSize() {
imageWidth = getDrawable().getIntrinsicWidth();
imageHeight = getDrawable().getMinimumHeight();
imageHeight = getDrawable().getIntrinsicHeight();
}

/**
Expand Down Expand Up @@ -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();

Expand All @@ -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
Expand All @@ -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);
}
}
Expand Down
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/res/layout/activity_sample.xml
Expand Up @@ -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"
Expand Down Expand Up @@ -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" />


Expand Down

0 comments on commit af2a190

Please sign in to comment.