Android 動畫的透明度漸變、旋轉動畫、縮放動畫、評議動畫
這是我在學習android的時候做的一個小小的東西可以實現圖片的旋轉、平移、縮放、透明度的漸變
首先我們要創建一個android的項目
在自己的drawable-mdpi中添加自己的圖片
然后在res目錄中,創建一個名稱是anim(動畫)的目錄,并且在該目錄中實現圖片的操作
首先是anim_alpha.xml定義一個實現透明漸變的動畫該動畫實現的是完全不透明——>完全透明————>完全不透明
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1"
android:toAlpha="0"
android:fillAfter="true"
android:repeatMode="reverse"
android:repeatCount="1"
android:duration="2000"/>
</set>然后是創建anim_rotate.xml的文件,在該文件中定義一個實現旋轉的動畫,實現從零到720再從360到零的旋轉
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/accelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="720"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000">
</rotate>
<rotate
android:interpolator="@android:anim/accelerate_interpolator"
android:startOffset="2000"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000">
</rotate>
</set>創建名稱是anim_scale.xml的文件定義一個實現縮放的動畫,該動畫首先將原來的圖像放大兩倍再將其縮小到原來的尺寸;
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1"
android:interpolator="@android:anim/decelerate_interpolator"
android:fromYScale="1"
android:toXScale="2.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:repeatCount="1"
android:repeatMode="reverse"
android:duration="2000"/>
</set>創建名稱是anim_translate.xml的文件實現圖片的平移從左側移動到右側然后從右側移動到左側
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:toXDelta="860"
android:fromYDelta="0"
android:toYDelta="0"
android:fillAfter="true"
android:repeatMode="reverse"
android:repeatCount="1"
android:duration="2000">
</translate>
</set>這樣 我們接下來就寫main.XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋轉" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="平移" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="縮放" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度漸變" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50px"
android:src="@drawable/cat" />
</LinearLayout>然后就是修改MainActivity.java
package com.mingrisoft;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation rotate=AnimationUtils.loadAnimation(this, R.anim.anim_rotate); //獲取“旋轉”動畫資源
final Animation translate=AnimationUtils.loadAnimation(this, R.anim.anim_translate); //獲取“平移”動畫資源
final Animation scale=AnimationUtils.loadAnimation(this, R.anim.anim_scale); //獲取“縮放”動畫資源
final Animation alpha=AnimationUtils.loadAnimation(this, R.anim.anim_alpha); //獲取“透明度變化”動畫資源
final ImageView iv=(ImageView)findViewById(R.id.imageView1); //獲取要應用動畫效果的ImageView
Button button1=(Button)findViewById(R.id.button1); //獲取“旋轉”按鈕
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
iv.startAnimation(rotate); //播放“旋轉”動畫
}
});
Button button2=(Button)findViewById(R.id.button2); //獲取“平移”按鈕
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
iv.startAnimation(translate); //播放“平移”動畫
}
});
Button button3=(Button)findViewById(R.id.button3); //獲取“縮放”按鈕
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
iv.startAnimation(scale); //播放“縮放”動畫
}
});
Button button4=(Button)findViewById(R.id.button4); //獲取“透明度漸變”按鈕
button4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
iv.startAnimation(alpha); //播放“透明度漸變”動畫
}
});
}
}這樣我們的小小的動畫制作就完成了
本文由用戶 cg4f 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!