这是我在学习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>
<?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>
<?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><?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>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); //播放“透明度渐变”动画
}
});
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
android动画的透明度渐变、旋转动画、缩放动画、评议动画
原文地址:http://blog.csdn.net/bluezhangfun/article/details/46835239