Scale Animation on ImageView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Scale Animation on ImageView

Postby nanye » Wed Jun 25, 2008 3:58 am

I want to apply scale animation on an ImageView when a button is clicked. The animation should scale the image of ImageView to the double size (scalce 200%) of its original value and image should not be clipped by bound of its parent (a LinaerLayout). When the animation is over, the image should return the original size (scalce 100%).

I set the attribute "android:clipChildren" of LinearLayout to "false" and call setKeepAnimations(false) too. But it seems these are not affective. The animation is clipped with the bound of LinearLayout and the image is not return to the original size.

So, could anyone can help me? :(

The main java code: AnimationTest.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.opensource.view;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.view.View;
  11.  
  12. import android.view.View.OnClickListener;
  13.  
  14. import android.view.animation.Animation;
  15.  
  16. import android.view.animation.AnimationUtils;
  17.  
  18. import android.widget.Button;
  19.  
  20. import android.widget.ImageView;
  21.  
  22. import android.widget.LinearLayout;
  23.  
  24.  
  25.  
  26. public class AnimationTest extends Activity {
  27.  
  28.  
  29.  
  30.         private Animation mAnim = null;
  31.  
  32.         private ImageView mImageView = null;
  33.  
  34.        
  35.  
  36.         @Override
  37.  
  38.     public void onCreate(Bundle icicle) {
  39.  
  40.         super.onCreate(icicle);
  41.  
  42.         setContentView(R.layout.animation_test);
  43.  
  44.        
  45.  
  46.         mAnim = AnimationUtils.loadAnimation(this, R.anim.anim);
  47.  
  48.         mAnim.setRepeatMode(Animation.NO_REPEAT);
  49.  
  50.        
  51.  
  52.         mImageView = (ImageView) findViewById(R.id.image);
  53.  
  54.        
  55.  
  56.         Button btn = (Button) findViewById(R.id.btn_ani);
  57.  
  58.         btn.setOnClickListener(new OnClickListener() {
  59.  
  60.                  public void onClick(View v) {
  61.  
  62.                          mImageView.startAnimation(mAnim);
  63.  
  64.                          
  65.  
  66.                  }
  67.  
  68.         });
  69.  
  70.        
  71.  
  72.         LinearLayout layout = (LinearLayout) findViewById(R.id.anim_layout);
  73.  
  74.         layout.setKeepAnimations(false);
  75.  
  76.         }
  77.  
  78. }
  79.  
  80.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The main layout XML: animation_test.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <RelativeLayout
  5.  
  6.         xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent">
  11.  
  12.     <Button  
  13.  
  14.                 android:id="@+id/btn_ani"
  15.  
  16.                 android:layout_alignParentTop="true"
  17.  
  18.             android:layout_width="fill_parent"
  19.  
  20.             android:layout_height="wrap_content"
  21.  
  22.             android:text="Click to start animation"/>
  23.  
  24.         <LinearLayout
  25.  
  26.                 android:id="@+id/anim_layout"
  27.  
  28.                 android:layout_below="@id/btn_ani"
  29.  
  30.                 android:layout_alignParentBottom="false"
  31.  
  32.             android:layout_width="fill_parent"
  33.  
  34.             android:layout_height="wrap_content"
  35.  
  36.             android:gravity="center"
  37.  
  38.             android:clipChildren="false">
  39.  
  40.                 <ImageView
  41.  
  42.                         android:id="@+id/image"
  43.  
  44.                         android:layout_below="@id/btn_ani"
  45.  
  46.                         android:layout_alignParentBottom="true"
  47.  
  48.                     android:layout_width="wrap_content"
  49.  
  50.                     android:layout_height="wrap_content"
  51.  
  52.                     android:src="@drawable/sample_icon_00"
  53.  
  54.                     android:scaleType="center"/>
  55.  
  56.         </LinearLayout>
  57.  
  58. </RelativeLayout>
  59.  
  60.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


The animation XML: anim/anim.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <scale xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:fromXScale="1.0"
  7.  
  8.    android:toXScale="2.0"
  9.  
  10.    android:fromYScale="1.0"
  11.  
  12.    android:toYScale="2.0"
  13.  
  14.    android:pivotX="50%"
  15.  
  16.    android:pivotY="50%"
  17.  
  18.    android:startOffset="0"
  19.  
  20.    android:duration="400"
  21.  
  22.    android:fillBefore="true" />
  23.  
  24.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
nanye
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Apr 18, 2008 3:55 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests