user5524159
user5524159

Reputation: 553

Zoom in zoom out animation

I am testing a zoom in/zoom out animation when an image is clicked. But I didn't get the results I wanted. The image completely zooms in but does not completely zoom out. For better understanding of my problem, see this video.

Here is my code:

zoom_in.XML

<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" >
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="3"
    android:toYScale="3" >
</scale>
</set>

zoom_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" >
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.5"
    android:toYScale="0.5" >
</scale>

mainactivity

public class MainActivity extends Activity 
 {
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageButton pic = (ImageButton) findViewById(R.id.levelimg);
    pic.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.dialogpic);
            dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
            dialog.show();
        }
    });
}
  }

my dailog animation style

<style name="DialogAnimation">
    <item name="android:windowEnterAnimation">@anim/zoom_in</item>
    <item name="android:windowExitAnimation">@anim/zoom_out</item>
    </style>

Upvotes: 2

Views: 4216

Answers (3)

Aditya Vyas-Lakhan
Aditya Vyas-Lakhan

Reputation: 13555

Try this

public class MainActivity extends Activity {
   private ImageView iv;
   private Matrix matrix = new Matrix();
   private float scale = 1f;
   private ScaleGestureDetector SGD;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      iv=(ImageView)findViewById(R.id.imageView);
      SGD = new ScaleGestureDetector(this,new ScaleListener());
   }

   public boolean onTouchEvent(MotionEvent ev) {
      SGD.onTouchEvent(ev);
      return true;
   }

   private class ScaleListener extends ScaleGestureDetector.

   SimpleOnScaleGestureListener {
      @Override
      public boolean onScale(ScaleGestureDetector detector) {
         scale *= detector.getScaleFactor();
         scale = Math.max(0.1f, Math.min(scale, 5.0f));

         matrix.setScale(scale, scale);
         iv.setImageMatrix(matrix);
         return true;
      }
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.

      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

Upvotes: 0

Aks4125
Aks4125

Reputation: 5720

change your code with this.

zoom_in.xml

<scale
    android:duration="@android:integer/config_shortAnimTime"
    android:fromXScale="0.3"
    android:fromYScale="0.3"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

zoom_out.xml

<scale
    android:duration="@android:integer/config_shortAnimTime"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0"
    android:toYScale="0" />

This will do the trick for you. :)

Upvotes: 5

BalajiG
BalajiG

Reputation: 529

Change your zoom out code as below and check once,Let me if it is not working.

   <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" >

        <scale
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="

1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0"
        android:toYScale="0" >
    </scale>

Upvotes: 1

Related Questions