简单的animation动画效果的实现过程

  
Animations的分类:
1.TweenedAnimation :提供旋转、移动、伸展、和淡出的效果
      Alpha:淡入淡出效果                           Scale:缩放效果
      Rotate:旋转效果                                 Translate: 移动效果
2.Frame-by-Frame Animation :创建Drawable的序列,可以按照指定的时间间隙一个个显示(1s中播放24张以上的图片就可以变成视频了)也可以做成动态广告的形式

3.res文件加下加入XMl文件,可以使用XML文件实现控制动画,XML文件中添加Set标签,标签中加入rotate、alpha、scale、或者translate 代码中实现AnimationUtil装载xml

   文件并生成,Animation对象需要注意的是

<!--pivotX  pivotY旋转时候 的坐标绝对位置的

50这种方法使用的是绝对位置
50%相对于空间本身定位

"50%p"的意思是相对于空控件的父控件的定位 -->

4.接着咱们看看代码的实现过程

首先看看两种方法共同的布局文件

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/Alpha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="淡入淡出" />
    <Button
        android:id="@+id/scale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="缩放效果" />
    <Button
        android:id="@+id/rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="旋转效果" />
    <Button
        android:id="@+id/translate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="移动效果" />
    </LinearLayout>
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/a" />
    

</LinearLayout>

5.看看第一种方法的活动实现的过程

 

 

package com.wang;

import com.wang.R.id;

import android.R.anim;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
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.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationTestActivity extends Activity {
	private ImageView image = null;
	private Button Alphabutton = null;
	private Button scalebutton = null;
	private Button rotatebutton = null;
	private Button translatebutton = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		image = (ImageView) findViewById(R.id.image);
		Alphabutton = (Button) findViewById(R.id.Alpha);
		scalebutton = (Button) findViewById(R.id.scale);
		rotatebutton = (Button) findViewById(R.id.rotate);
		translatebutton = (Button) findViewById(R.id.translate);
		// 淡入淡出按钮的监听时间
		Alphabutton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// 创建一个AnimationSet对象
				AnimationSet animationSet = new AnimationSet(true);
				// j加速播放
				animationSet.setInterpolator(new AccelerateInterpolator());
				// //创建一个AnimationSet对象淡出旋转二合一
				AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
				RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
						Animation.RELATIVE_TO_PARENT, 1f,// X轴
						Animation.RELATIVE_TO_PARENT, 0f);// y轴

				// 将alphaAnimation对象添加到animationSet中
				animationSet.addAnimation(alphaAnimation);
				animationSet.addAnimation(rotateAnimation);
				// 显示的时间为1s
				alphaAnimation.setDuration(3000);
				// 开始执行动画
				image.startAnimation(animationSet);
				// 设置重复的次数
				animationSet.setRepeatCount(4);

			}

		});
		// 缩放按钮的监听事件
		scalebutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AnimationSet animationSet = new AnimationSet(true);
				// 从1缩放的0.1
				ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
						0.1f, Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);

				animationSet.addAnimation(scaleAnimation);

				// 执行前等待的时间
				animationSet.setStartOffset(1000);

				// dd动画执行之前和之后的状态
				animationSet.setFillAfter(true);
				animationSet.setFillBefore(false);
				animationSet.setDuration(5000);
				image.startAnimation(animationSet);

			}
		});
		// 旋转按钮的单击事件
		rotatebutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				AnimationSet animationSet = new AnimationSet(true);
				// 创建对象 参数 从0度转到360度
				/*********
				 **一个动画控制旋转的一个对象。这个旋转发生int xy平面。 您可以指定点使用的中心旋转,在那里(0,0)是左上角点。
				 * 如果未指定,(0,0)是默认的旋转点
				 **/
				RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
						Animation.RELATIVE_TO_PARENT, 1f,// X轴
						Animation.RELATIVE_TO_PARENT, 0f);// y轴
				rotateAnimation.setDuration(5000);
				animationSet.addAnimation(rotateAnimation);
				image.startAnimation(animationSet);

			}
		});
		// 移动按钮的点击事件
		translatebutton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				AnimationSet animationSet = new AnimationSet(true);
				TranslateAnimation translatebutton = new TranslateAnimation(
						Animation.RELATIVE_TO_SELF, 0f,// //X轴
						Animation.RELATIVE_TO_SELF, 0.5f,// y轴
						Animation.RELATIVE_TO_SELF, 0f,// X轴
						Animation.RELATIVE_TO_SELF, 1.0f);// y轴
				translatebutton.setDuration(5000);
			}
		});
	}
}

6.第二种方式实现的时候要在res的文件之下在建一个文件夹anim。文件夹里面包含的四个文件如下
alpha.xml  淡入淡出效果的文件

 

 

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:startOffset="500"
        android:toAlpha="0.0" />

</set>

 

 

 

 

 

rotate.xml  旋转效果的文件

 

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <rotate
        android:fromDegrees="0"
        android:toDegrees="350"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:duration="3000" />
<!--pivotX  pivotY旋转时候 的坐标绝对位置的
50这种方法使用的是绝对位置
50%相对于空间本身定位
"50%p"的意思是相对于空控件的父控件的定位

 -->
</set>

 

 

 

 

 

sacle.xml   缩放效果的文件  

 

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >


    <scale
        android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0" 
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000"/>


</set>

 

 

 

 

 

translate.xml  移动效果的文件

 

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <translate
        android:fromXDelta="50%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="3000" />

</set>

7.接着看看第二种方法的Activity的实现过程

 

package com.wang;

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 AnimationXMLActivity extends Activity {

	private ImageView image;
	private Button alphabutton;
	private Button rotatebutton;
	private Button saclebutton;
	private Button translatebutton;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		image = (ImageView) findViewById(R.id.image);
		alphabutton = (Button) findViewById(R.id.alpha);
		rotatebutton = (Button) findViewById(R.id.roate);
		saclebutton = (Button) findViewById(R.id.scale);
		translatebutton = (Button) findViewById(R.id.translate);
		// 淡入淡出按钮的监听事件
		alphabutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// s使用AnimationUtils装载动画设置文件
				Animation animation = AnimationUtils.loadAnimation(
						AnimationXMLActivity.this, R.anim.alpha);
				// 启动
				image.startAnimation(animation);
			}
		});
		// 旋转效果的监听事件
		rotatebutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// s使用AnimationUtils装载动画设置文件
				Animation animation = AnimationUtils.loadAnimation(
						AnimationXMLActivity.this, R.anim.rotate);
				image.startAnimation(animation);// TODO Auto-generated method
				// stub

			}
		});
		// 缩放组件的效果监听
		saclebutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// s使用AnimationUtils装载动画设置文件
				Animation animation = AnimationUtils.loadAnimation(
						AnimationXMLActivity.this, R.anim.sacle);
				image.startAnimation(animation); // TODO Auto-generated method
				// stub

			}
		});
		// 移动效果的监听事件
		translatebutton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// s使用AnimationUtils装载动画设置文件
				Animation animation = AnimationUtils.loadAnimation(
						AnimationXMLActivity.this, R.anim.translate);
				image.startAnimation(animation);
			}
		});

	}
}

 

 

8.殊路同归,这两种方法实现的功能都差不多,第一张图片是原图,第二张图片是淡入淡出的效果,第三张是旋转的效果图,第四章是缩放的效果图,第五章是移动的效果图

 

 

由于是动画效果,所以只能截取图片的一部分

运行结果如下

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵。

既然都看到这里,领两个红包在走吧!
以下两个红包每天都可以领取

1.支付宝搜索 522398497,或扫码支付宝红包海报。

支付宝扫一扫,每天领取大红包

2.微信红包,微信扫一扫即可领取红包

 

微信扫一扫,每天领取微信红包

小礼物走一走,来简书关注我

posted @   程序员Android的博客  阅读(79)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示