先在在layout放置兩個元件,我這邊的範例是使用ImageView 、Button兩個元件介紹
layout:
<Button
android:id="@+id/Button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<ImageView
android:id="@+id/ImageView_Sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
移動特效
Sample:
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
ImageView mImage_sample;
Button mButton_start;
init()
{
mImage_sample = (ImageView) findViewById(R.id.ImageView_Sample);
mButton_start = (Button) findViewById(R.id.Button_start);
mButton_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//ScaleAnimation(float fromX, float toX, float fromY, float toY)
//(fromX,toX)X軸從fromX的倍率放大/縮小至toX的倍率
//(fromY,toY)X軸從fromY的倍率放大/縮小至toX的倍率
Animation am = new ScaleAnimation(0.0f, 4.0f, 0.0f, 4.0f);
//setDuration (long durationMillis) 設定動畫開始到結束的執行時間
am.setDuration(2000);
//setRepeatCount (int repeatCount) 設定重複次數 -1為無限次數 0
am.setRepeatCount(-1);
//將動畫參數設定到圖片並開始執行動畫
mImage_sample.startAnimation(am);
}
});
}
若要使用XML的方式為在res下建立一個anim的資料夾 res/animSample:animationset.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="2000" android:fromXScale="0" android:fromYScale="0" android:repeatCount="-1" android:toXScale="4" android:toYScale="4" > </scale> </set>Code:
import android.view.animation.AnimationUtils; Animation am = AnimationUtils.loadAnimation(mContext, R.anim.animationset); mImage_sample.startAnimation(am);
其他文章:
Android TranslateAnimation 移動效果 動態特效
Android ScaleAnimation 放大縮小效果 動態特效
Android RotateAnimation 旋轉效果 動態特效
Android AlphaAnimation 淡進淡出效果 動態特效
Android AnimationSet 組合特效 動態特效

沒有留言:
張貼留言