先在在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.AlphaAnimation;
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) {
//AlphaAnimation(float fromAlpha, float toAlpha)
//fromAlpha 起始透明度(Alpha)值
//toAlpha 最後透明度(Alpha)值
// 1.0f~0.0f
Animation am = new AlphaAnimation(1.0f, 0.0f);
//setDuration (long durationMillis) 設定動畫開始到結束的執行時間
am.setDuration(2000);
//setRepeatCount (int repeatCount) 設定重複次數 -1為無限次數 0
am.setRepeatCount(-1);
//將動畫參數設定到圖片並開始執行動畫
mImage_sample.startAnimation(am);
}
});
}若要使用XML的方式為在res下建立一個anim的資料夾 res/anim
Sample:animationset.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="2000" android:fromAlpha="1" android:repeatCount="-1" android:toAlpha="0" > </alpha> </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 組合特效 動態特效

沒有留言:
張貼留言