android開發之Animations的使用(一)
編輯:關於Android編程
android開發之Animations的使用(一)
本博文主要講述的是android開發中的動畫效果(Animations)API的使用,主要講述動畫的四種效果的實現:
第一,圖片縮放效果
第二,圖片移動效果
第三,圖片翻轉效果
第四,圖片的漸進效果
下面我們來看代碼:
首先我們來看MainActivity.java:
package com.example.animationtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView = null;
private Button scaleButton = null;
private Button translateButton = null;
private Button rotateButton = null;
private Button alphaButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.myImage);
scaleButton = (Button)findViewById(R.id.scaleButton);
translateButton = (Button)findViewById(R.id.translateButton);
rotateButton = (Button)findViewById(R.id.rotateButton);
alphaButton = (Button)findViewById(R.id.alphaButton);
scaleButton.setOnClickListener(new setScaleListener());
translateButton.setOnClickListener(new setTranslateListener());
rotateButton.setOnClickListener(new setRotateListener());
alphaButton.setOnClickListener(new setAlphaListener());
}
//動畫縮放效果監聽器
class setScaleListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創建一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//創建一個Animation對象 (1,1) --> (0.5,0.5)
Animation animation = new ScaleAnimation(1f, 0.5f, 1f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//設置動畫過程的時間
animation.setDuration(1000);
//設置動畫開始時間偏移量
animation.setStartOffset(1000);
//設置重復播放的次數
animation.setRepeatCount(0);
//結束時的狀態
animation.setFillAfter(true);
//將Animation對象添加到AnimationSet對象中
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
//動畫移動效果監聽器
class setTranslateListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創建一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//(0,0) --> (1,1)
Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
//旋轉動畫效果監聽器
class setRotateListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創建一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//360旋轉,RELATIVE_TO_PARENT 以父控件的(0,1)為中心
//RELATIVE_TO_SELF 自身
Animation animation = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
//漸入漸出動畫效果監聽器
class setAlphaListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創建一個AnimationSet對象
AnimationSet animationSet = new AnimationSet(true);
//1:表示完全可見
//0:表示完全不可見
Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
@Override
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;
}
}
主布局文件main.xml如下:
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" >
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
android:id="@+id/imgLayout"
android:layout_below="@id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="55dp"
>
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
android:id="@+id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imgLayout"
android:text="@string/scale"
/>
android:id="@+id/translateButton"
android:layout_below="@id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/translate"
/>
android:id="@+id/rotateButton"
android:layout_below="@id/translateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/rotate"
/>
android:id="@+id/alphaButton"
android:layout_below="@id/rotateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/alpha"
/>
實現效果如下:
分別點擊四個按鈕就會看到圖片會展示對應的四種效果
上一頁:AndroidClipSquare安卓實現方形頭像裁剪 下一頁:android AsyncTask介紹