Android開發(fā)之圖形圖像與動(dòng)畫(二)Animation實(shí)現(xiàn)圖像的漸變/縮放/位移/旋轉(zhuǎn)
更新時(shí)間:2013年01月28日 11:23:29 作者:
Android 平臺(tái)提供了兩類動(dòng)畫,一類是Tween動(dòng)畫,就是對(duì)場(chǎng)景里的對(duì)象不斷的進(jìn)行圖像變化來產(chǎn)生動(dòng)畫效果;旋轉(zhuǎn)、平移、放縮和漸變等等,感興趣的朋友可以了解下啊,希望本文對(duì)你有所幫助
Android 平臺(tái)提供了兩類動(dòng)畫。 一類是Tween動(dòng)畫,就是對(duì)場(chǎng)景里的對(duì)象不斷的進(jìn)行圖像變化來產(chǎn)生動(dòng)畫效果(旋轉(zhuǎn)、平移、放縮和漸變)。
下面就講一下Tweene Animations。
主要類:
Animation 動(dòng)畫
AlphaAnimation 漸變透明度
RotateAnimation 畫面旋轉(zhuǎn)
ScaleAnimation 漸變尺寸縮放
TranslateAnimation 位置移動(dòng)
AnimationSet 動(dòng)畫集
一.AlphaAnimation
其中AlphaAnimation類第一個(gè)參數(shù)fromAlpha表示動(dòng)畫起始時(shí)的透明度, 第二個(gè)參數(shù)toAlpha表示動(dòng)畫結(jié)束時(shí)的透明度。
setDuration用來設(shè)置動(dòng)畫持續(xù)時(shí)間。
二.RotateAnimation
其中RotateAnimation類第一個(gè)參數(shù)fromDegrees表示動(dòng)畫起始時(shí)的角度, 第二個(gè)參數(shù)toDegrees表示動(dòng)畫結(jié)束時(shí)的角度。
另外還可以設(shè)置伸縮模式pivotXType、pivotYType, 伸縮動(dòng)畫相對(duì)于x,y 坐標(biāo)的開始位置pivotXValue、pivotYValue等。
三.ScaleAnimation
ScaleAnimation類中
第一個(gè)參數(shù)fromX ,第二個(gè)參數(shù)toX:分別是動(dòng)畫起始、結(jié)束時(shí)X坐標(biāo)上的伸縮尺寸。
第三個(gè)參數(shù)fromY ,第四個(gè)參數(shù)toY:分別是動(dòng)畫起始、結(jié)束時(shí)Y坐標(biāo)上的伸縮尺寸。
另外還可以設(shè)置伸縮模式pivotXType、pivotYType, 伸縮動(dòng)畫相對(duì)于x,y 坐標(biāo)的開始位置pivotXValue、pivotYValue等。
四.TranslateAnimation
第一個(gè)參數(shù)fromXDelta ,第二個(gè)參數(shù)toXDelta:分別是動(dòng)畫起始、結(jié)束時(shí)X坐標(biāo)。
第三個(gè)參數(shù)fromYDelta ,第四個(gè)參數(shù)toYDelta:分別是動(dòng)畫起始、結(jié)束時(shí)Y坐標(biāo)。
下面我實(shí)現(xiàn)的這個(gè)例子是使得圖片有上述四個(gè)動(dòng)畫效果,且其中第五實(shí)現(xiàn)的是兩個(gè)效果的重疊,具體的實(shí)現(xiàn)截圖如下:
點(diǎn)擊各個(gè)按鈕會(huì)做出相應(yīng)的反應(yīng)。
本實(shí)例用到的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scale"
android:layout_x="5dp"
android:layout_y="383dp" />
<Button
android:id="@+id/button_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rotate"
android:layout_x="158dp"
android:layout_y="383dp" />
<Button
android:id="@+id/button_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alpha"
android:layout_x="5dp"
android:layout_y="331dp" />
<Button
android:id="@+id/button_translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="translate"
android:layout_x="160dp"
android:layout_y="329dp" />
<Button
android:id="@+id/button_alpha_translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alpha_translate"
android:layout_x="84dp"
android:layout_y="265dp" />
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="105dp"
android:layout_y="133dp"
android:src="@drawable/ic_launcher"
/>
</AbsoluteLayout>
實(shí)現(xiàn)本實(shí)例的源代碼如下:
public class Animations_Activity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animations_);
button1=(Button)findViewById(R.id.button_alpha);
button2=(Button)findViewById(R.id.button_rotate);
button3=(Button)findViewById(R.id.button_scale);
button4=(Button)findViewById(R.id.button_translate);
button5=(Button)findViewById(R.id.button_alpha_translate);
imageView=(ImageView)findViewById(R.id.imageview);
button1.setOnClickListener(new MyButton());
button2.setOnClickListener(new MyButton());
button3.setOnClickListener(new MyButton());
button4.setOnClickListener(new MyButton());
button5.setOnClickListener(new MyButton());
}
class MyButton implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button_alpha:
Alpha();
break;
case R.id.button_rotate:
Rotata();
break;
case R.id.button_scale:
Scale();
break;
case R.id.button_translate:
Translate();
break;
case R.id.button_alpha_translate:
Alpha_Translate();
break;
default:
break;
}
}
}
/*
* 1.創(chuàng)建一個(gè)AnimationSet對(duì)象,該對(duì)象存儲(chǔ)的是動(dòng)畫的集合
* 2.根據(jù)需要?jiǎng)?chuàng)建相應(yīng)的Animation對(duì)象
* 3.根據(jù)動(dòng)畫的需求,為Animation對(duì)象設(shè)置相應(yīng)的數(shù)據(jù)(即執(zhí)行效果)
* 4.獎(jiǎng)Animation對(duì)象添加到AnimationSet對(duì)象當(dāng)中
* 5.使用控件對(duì)象開始執(zhí)行AnimationSet
*/
public void Alpha() {
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
public void Rotata(){
AnimationSet animationSet=new AnimationSet(true);
//后面的四個(gè)參數(shù)定義的是旋轉(zhuǎn)的圓心位置
RotateAnimation rotateAnimation=new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
public void Scale() {
AnimationSet animationSet=new AnimationSet(true);
ScaleAnimation scaleAnimation=new ScaleAnimation(
1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(scaleAnimation);
}
public void Translate() {
AnimationSet animationSet=new AnimationSet(true);
TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, //X軸的開始位置
Animation.RELATIVE_TO_SELF, 0.5f, //X軸的結(jié)束位置
Animation.RELATIVE_TO_SELF, 0f, //Y軸的開始位置
Animation.RELATIVE_TO_SELF, 1.0f); //Y軸的結(jié)束位置
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
/*
* 第一行的設(shè)置如果為true,則動(dòng)畫執(zhí)行完之后效果定格在執(zhí)行完之后的狀態(tài)
* 第二行的設(shè)置如果為false,則動(dòng)畫執(zhí)行完之后效果定格在執(zhí)行完之后的狀態(tài)
* 第三行設(shè)置的是一個(gè)long類型的值,是指動(dòng)畫延遲多少毫秒之后執(zhí)行
* 第四行定義的是動(dòng)畫重復(fù)幾次執(zhí)行
*/
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setStartOffset(2000);
animationSet.setRepeatCount(3);
imageView.startAnimation(animationSet);
}
public void Alpha_Translate() {
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, //X軸的開始位置
Animation.RELATIVE_TO_SELF, 0.5f, //X軸的結(jié)束位置
Animation.RELATIVE_TO_SELF, 0f, //Y軸的開始位置
Animation.RELATIVE_TO_SELF, 1.0f); //Y軸的結(jié)束位置
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_animations_, menu);
return true;
}
}
下面就講一下Tweene Animations。
主要類:
Animation 動(dòng)畫
AlphaAnimation 漸變透明度
RotateAnimation 畫面旋轉(zhuǎn)
ScaleAnimation 漸變尺寸縮放
TranslateAnimation 位置移動(dòng)
AnimationSet 動(dòng)畫集
一.AlphaAnimation
其中AlphaAnimation類第一個(gè)參數(shù)fromAlpha表示動(dòng)畫起始時(shí)的透明度, 第二個(gè)參數(shù)toAlpha表示動(dòng)畫結(jié)束時(shí)的透明度。
setDuration用來設(shè)置動(dòng)畫持續(xù)時(shí)間。
二.RotateAnimation
其中RotateAnimation類第一個(gè)參數(shù)fromDegrees表示動(dòng)畫起始時(shí)的角度, 第二個(gè)參數(shù)toDegrees表示動(dòng)畫結(jié)束時(shí)的角度。
另外還可以設(shè)置伸縮模式pivotXType、pivotYType, 伸縮動(dòng)畫相對(duì)于x,y 坐標(biāo)的開始位置pivotXValue、pivotYValue等。
三.ScaleAnimation
ScaleAnimation類中
第一個(gè)參數(shù)fromX ,第二個(gè)參數(shù)toX:分別是動(dòng)畫起始、結(jié)束時(shí)X坐標(biāo)上的伸縮尺寸。
第三個(gè)參數(shù)fromY ,第四個(gè)參數(shù)toY:分別是動(dòng)畫起始、結(jié)束時(shí)Y坐標(biāo)上的伸縮尺寸。
另外還可以設(shè)置伸縮模式pivotXType、pivotYType, 伸縮動(dòng)畫相對(duì)于x,y 坐標(biāo)的開始位置pivotXValue、pivotYValue等。
四.TranslateAnimation
第一個(gè)參數(shù)fromXDelta ,第二個(gè)參數(shù)toXDelta:分別是動(dòng)畫起始、結(jié)束時(shí)X坐標(biāo)。
第三個(gè)參數(shù)fromYDelta ,第四個(gè)參數(shù)toYDelta:分別是動(dòng)畫起始、結(jié)束時(shí)Y坐標(biāo)。
下面我實(shí)現(xiàn)的這個(gè)例子是使得圖片有上述四個(gè)動(dòng)畫效果,且其中第五實(shí)現(xiàn)的是兩個(gè)效果的重疊,具體的實(shí)現(xiàn)截圖如下:
點(diǎn)擊各個(gè)按鈕會(huì)做出相應(yīng)的反應(yīng)。
本實(shí)例用到的布局文件如下:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scale"
android:layout_x="5dp"
android:layout_y="383dp" />
<Button
android:id="@+id/button_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rotate"
android:layout_x="158dp"
android:layout_y="383dp" />
<Button
android:id="@+id/button_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alpha"
android:layout_x="5dp"
android:layout_y="331dp" />
<Button
android:id="@+id/button_translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="translate"
android:layout_x="160dp"
android:layout_y="329dp" />
<Button
android:id="@+id/button_alpha_translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alpha_translate"
android:layout_x="84dp"
android:layout_y="265dp" />
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="105dp"
android:layout_y="133dp"
android:src="@drawable/ic_launcher"
/>
</AbsoluteLayout>
實(shí)現(xiàn)本實(shí)例的源代碼如下:
復(fù)制代碼 代碼如下:
public class Animations_Activity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animations_);
button1=(Button)findViewById(R.id.button_alpha);
button2=(Button)findViewById(R.id.button_rotate);
button3=(Button)findViewById(R.id.button_scale);
button4=(Button)findViewById(R.id.button_translate);
button5=(Button)findViewById(R.id.button_alpha_translate);
imageView=(ImageView)findViewById(R.id.imageview);
button1.setOnClickListener(new MyButton());
button2.setOnClickListener(new MyButton());
button3.setOnClickListener(new MyButton());
button4.setOnClickListener(new MyButton());
button5.setOnClickListener(new MyButton());
}
class MyButton implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button_alpha:
Alpha();
break;
case R.id.button_rotate:
Rotata();
break;
case R.id.button_scale:
Scale();
break;
case R.id.button_translate:
Translate();
break;
case R.id.button_alpha_translate:
Alpha_Translate();
break;
default:
break;
}
}
}
/*
* 1.創(chuàng)建一個(gè)AnimationSet對(duì)象,該對(duì)象存儲(chǔ)的是動(dòng)畫的集合
* 2.根據(jù)需要?jiǎng)?chuàng)建相應(yīng)的Animation對(duì)象
* 3.根據(jù)動(dòng)畫的需求,為Animation對(duì)象設(shè)置相應(yīng)的數(shù)據(jù)(即執(zhí)行效果)
* 4.獎(jiǎng)Animation對(duì)象添加到AnimationSet對(duì)象當(dāng)中
* 5.使用控件對(duì)象開始執(zhí)行AnimationSet
*/
public void Alpha() {
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
public void Rotata(){
AnimationSet animationSet=new AnimationSet(true);
//后面的四個(gè)參數(shù)定義的是旋轉(zhuǎn)的圓心位置
RotateAnimation rotateAnimation=new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
public void Scale() {
AnimationSet animationSet=new AnimationSet(true);
ScaleAnimation scaleAnimation=new ScaleAnimation(
1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(scaleAnimation);
}
public void Translate() {
AnimationSet animationSet=new AnimationSet(true);
TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, //X軸的開始位置
Animation.RELATIVE_TO_SELF, 0.5f, //X軸的結(jié)束位置
Animation.RELATIVE_TO_SELF, 0f, //Y軸的開始位置
Animation.RELATIVE_TO_SELF, 1.0f); //Y軸的結(jié)束位置
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
/*
* 第一行的設(shè)置如果為true,則動(dòng)畫執(zhí)行完之后效果定格在執(zhí)行完之后的狀態(tài)
* 第二行的設(shè)置如果為false,則動(dòng)畫執(zhí)行完之后效果定格在執(zhí)行完之后的狀態(tài)
* 第三行設(shè)置的是一個(gè)long類型的值,是指動(dòng)畫延遲多少毫秒之后執(zhí)行
* 第四行定義的是動(dòng)畫重復(fù)幾次執(zhí)行
*/
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setStartOffset(2000);
animationSet.setRepeatCount(3);
imageView.startAnimation(animationSet);
}
public void Alpha_Translate() {
AnimationSet animationSet=new AnimationSet(true);
AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, //X軸的開始位置
Animation.RELATIVE_TO_SELF, 0.5f, //X軸的結(jié)束位置
Animation.RELATIVE_TO_SELF, 0f, //Y軸的開始位置
Animation.RELATIVE_TO_SELF, 1.0f); //Y軸的結(jié)束位置
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_animations_, menu);
return true;
}
}
相關(guān)文章
如何自己實(shí)現(xiàn)Android View Touch事件分發(fā)流程
這篇文章主要介紹了如何自己實(shí)現(xiàn)Android View Touch事件分發(fā)流程,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android自定義View葉子旋轉(zhuǎn)完整版(六)
這篇文章主要為大家詳細(xì)介紹了Android自定義View葉子旋轉(zhuǎn)完整版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
android設(shè)置adb自帶screenrecord錄屏命令
這篇文章主要介紹了android設(shè)置adb自帶screenrecord錄屏命令,需要的朋友可以參考下2018-11-11
Android Service服務(wù)詳細(xì)介紹及使用總結(jié)
這篇文章主要介紹了Android Service 服務(wù)的詳細(xì)資料,網(wǎng)上關(guān)于Android Service 服務(wù)的文章比較多,但是不是很全面,不夠細(xì)致,畢竟是Android 四大組件之一,重要性不用說,這里總結(jié)下,需要的朋友可以參考下2016-12-12
Android TextView漸變顏色和方向及動(dòng)畫效果的設(shè)置詳解
TextView的在安卓中可以理解為一個(gè)文本視圖控件,Android的視圖控件的基類是View類,可以理解的TextView是View的子類。我們通常在.XML布局文件中會(huì)為文本視圖控件指定各種屬性來設(shè)置它的樣式,今天我們要講的當(dāng)然不是傳統(tǒng)常見的那種,將會(huì)帶有漸變顏色和方向及動(dòng)畫效果2021-11-11
Android圖片的Base64編碼與解碼及解碼Base64圖片方法
Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個(gè)可打印字符來表示二進(jìn)制數(shù)據(jù)的方法。接下來通過本文給大家分享Android圖片的Base64編碼與解碼及解碼Base64圖片,需要的朋友參考下吧2017-12-12
Android NDK開發(fā)(C語言--動(dòng)態(tài)內(nèi)存分配)
這篇文章主要介紹了Android NDK開發(fā) C語言--動(dòng)態(tài)內(nèi)存分配2021-12-12
ActivityManagerService之Service啟動(dòng)過程解析
這篇文章主要為大家介紹了ActivityManagerService之Service啟動(dòng)過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

