在xml中實作animation
一、在res檔案夾下建立一個名為anim的檔案夾
二、建立xml檔案,首先加入set标簽
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
>
三、在set标簽中加入rotate、translate、alpha、scale标簽,每一個動畫效果占一個xml檔案
點選(此處)折疊或打開
(1)alpha.xml:
?xml version="1.0" encoding="utf-8"?>
set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
alpha ?透明效果?>
android:fromAlpha="0.1" ?開始透明度10%?>
android:toAlpha="1.0" ?結束透明度?>
android:duration="3000" ?設定動畫執行時間?>
android:startOffset="500" ?設定動畫執行前的等待時間?>
/>
/set>
(2)rotate.xml:
?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
rotate ?旋轉效果?>
android:fromDegrees="0" ?起始角度?>
android:toDegrees="+350" ?結束角度?>
android:pivotX="50%" ?旋轉點的x坐标 “50”代表絕對位置,“50%”相對于控件本身,“50%p”相對于父控件?>
android:pivotY="50%" ?旋轉點的y坐标?>
android:duration="3000" ?動畫執行的時間?>
/>
(3)scale.xml
scale ?縮放效果?>
android:fromXScale="1.0" ?起始x坐标?>
android:toXScale="0.1" ?結束x坐标?>
android:fromYScale="1.0" ?起始y坐标?>
android:toYScale="0.1" ?結束y坐标?>
android:pivotX="50%" ?縮放原點x坐标?>
android:pivotY="50%" ?縮放原點y坐标?>
android:duration="3000" ?動畫持續時間?>
/>
四、在代碼中使用AnimationUtils裝載xml檔案,并生成Animation對象
Animation animation_alpha = AnimationUtils.loadAnimation(MainActivity.this , R.anim.alpha);
imageView.startAnimation(animation_alpha);
五、Animation的實作
public class MainActivity extends Activity
{
private Button alphaButton = null;
private Button scaleButton = null;
private Button rotateButton = null;
private Button translateButton = null;
private ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
alphaButton = (Button)findViewById(R.id.alphaBtn);
scaleButton = (Button)findViewById(R.id.scaleBtn);
rotateButton = (Button)findViewById(R.id.rotateBtn);
translateButton= (Button)findViewById(R.id.translateBtn);
imageView = (ImageView)findViewById(R.id.imageViewId);
//為控件添加事件
alphaButton.setOnClickListener(new btnListener());
scaleButton.setOnClickListener(new btnListener());
rotateButton.setOnClickListener(new btnListener());
translateButton.setOnClickListener(new btnListener());
}
class btnListener implements OnClickListener
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.alphaBtn:
//透明效果
Animation animation_alpha = AnimationUtils.loadAnimation(MainActivity.this , R.anim.alpha);
imageView.startAnimation(animation_alpha);
break;
case R.id.scaleBtn:
//縮放效果
Animation animation_scale = AnimationUtils.loadAnimation(MainActivity.this , R.anim.scale);
imageView.startAnimation(animation_scale);
case R.id.rotateBtn:
//旋轉效果
Animation animation_rotate = AnimationUtils.loadAnimation(MainActivity.this , R.anim.rotate);
imageView.startAnimation(animation_rotate);
case R.id.translateBtn:
//移動效果
Animation animation_translate = AnimationUtils.loadAnimation(MainActivity.this , R.anim.translate);
imageView.startAnimation(animation_translate);
default:
break;
}
}
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
xml檔案
1、alpha.xml
?xml version="1.0" encoding="utf-8"?>
set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="3000"
android:startOffset="500"
/>
/set>
2、scalse.xml
scale
android:fromXScale="1.0"
android:toXScale="0.1"
android:fromYScale="1.0"
android:toYScale="0.1"
android:pivotX="50%"
android:pivotY="50%"
/>
3、rotate.xml
rotate
android:fromDegrees="0"
android:toDegrees="+350"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
4、translate.xml
android:interpolator="@android:anim/accelerate_interpolator"
>
translate
/>
布局檔案
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
Button
android:id="@+id/alphaBtn"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:text="alpha動畫"
android:id="@+id/scaleBtn"
android:layout_above="@id/alphaBtn"
android:text="scale動畫"
android:id="@+id/rotateBtn"
android:layout_above="@id/scaleBtn"
android:text="rotate動畫"
android:id="@+id/translateBtn"
android:layout_above="@id/rotateBtn"
android:text="translate動畫"
ImageView
android:id="@+id/imageViewId"
android:layout_centerInParent="true"
android:layout_marginTop="100dip"
android:src="@drawable/ic_launcher"
/RelativeLayout>