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);	//播放“透明度渐变”动画
				
			}
		});			
	}
}

这样我们的小小的动画制作就完成了 



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值