1.首先先加入一個imagebutton,我們找一張並命名為fba.png
2.要加入二個xml檔,一個負責放大(to_large.xml),另一個負責縮小(to_small.xml)
res/anim/to_large.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:fromXScale="1.0" android:toXScale="2.0" android:fromYScale="1.0" android:toYScale="2.0" android:pivotX="50%" android:pivotY="50%" android:duration="500" /> <!-- android:fromXScale = "1.0" //fromXScale表示從什麼大小開始放大,1.0表示從原本圖的大小開始放大, 0.5則是初始值是原圖縮小一倍 --> <!-- android:toXScale="2.0" //toXScale表示放大到多大,2.0表示放大到原圖的2倍 -->
res/anim/to_small.xml
<scale xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="2.0" android:toXScale="1.0" android:fromYScale="2.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="500" />
MainActivity.java
import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.view.animation.Animation.AnimationListener; import android.widget.ImageButton; public class MainActivity extends Activity implements AnimationListener { private Animation toLargeAnimation; private Animation toSmallAnimation; private ImageButton imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageButton) findViewById(R.id.imageView); toLargeAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.to_large); toSmallAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.to_small); toLargeAnimation.setAnimationListener(MainActivity.this); toSmallAnimation.setAnimationListener(MainActivity.this); imageView.startAnimation(toSmallAnimation); addListenerOnButton(); } public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } public void onAnimationEnd(Animation animation) { if (animation.hashCode() == toLargeAnimation.hashCode()) imageView.startAnimation(toSmallAnimation); else imageView.startAnimation(toLargeAnimation); } public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } public void addListenerOnButton() { imageView.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // 按下按鈕要做的事,就寫在這裡 } }); } }
res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageButton android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginBottom="20dp" android:background = "#ffffff" android:src="@drawable/fba" /> </RelativeLayout>
參考連結
http://www.360doc.com/content/11/1208/16/8157240_170676944.shtml
http://www.pocketdigi.com/20110511/277.html
沒有留言:
張貼留言