AlphaAnimation 透明效果实现:

activity_main.xml中仅仅是一个简单的图片,下面的例子都会使用这个xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <!-- 用于动画的图片 --> <ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="70dp"
android:layout_marginTop="138dp"
android:src="@drawable/jhs_button1_h" /> </RelativeLayout>

透明效果的java代码:

package com.example.test.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1);
//图片点击的时候,启动动画效果
imageView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Animation anim = getAlphaAnimation();
v.startAnimation(anim);
}
}); } /**
* 透明效果
* @return
*/
public Animation getAlphaAnimation() {
//实例化 AlphaAnimation 主要是改变透明度
//透明度 从 1-不透明 0-完全透明
Animation animation = new AlphaAnimation(1.0f, 0.5f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(1000);
return animation;
} }

ScaleAnimation 缩放效果实现:

package com.example.test.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1);
//图片点击的时候,启动动画效果
imageView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Animation scalegetAnim = getScaleAnimation();
v.startAnimation(scalegetAnim);
}
}); } /**
* 缩放动画
* @return
*/
public Animation getScaleAnimation() {
//实例化 ScaleAnimation 主要是缩放效果
//参数:fromX-动画开始前,x坐标 toX-动画结束后x坐标
//fromY-动画开始前,Y坐标 toY-动画结束后Y坐标
//pivotXType - 为动画相对于物件的X坐标的参照物 pivotXValue - 值
//pivotYType - 为动画相对于物件的Y坐标的参照物 pivotYValue - 值
Animation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(1000);
return animation;
} }

RotateAnimation 旋转效果实现:

package com.example.test.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1);
//图片点击的时候,启动动画效果
imageView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Animation rotateAnim = getRotateAnimation();
v.startAnimation(rotateAnim);
}
}); } /**
* 旋转
* @return
*/
public Animation getRotateAnimation() {
//实例化RotateAnimation
//以自身中心为圆心,旋转360度 正值为顺时针旋转,负值为逆时针旋转
RotateAnimation animation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(1000);
return animation;
} }

TranslateAnimation 移动效果实现:

package com.example.test.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1);
//图片点击的时候,启动动画效果
imageView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Animation translateAnim = getTranslateAnimation();
v.startAnimation(translateAnim);
}
}); } /**
* 移动
* @return
*/
public Animation getTranslateAnimation() {
//实例化TranslateAnimation
//以自身为坐标系和长度单位,从(0,0)移动到(1,1)
TranslateAnimation animation = new
TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(1000);
return animation;
} }

AnimationSet 动画集合实现和使用:

package com.example.test.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1);
//图片点击的时候,启动动画效果
imageView.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//AnimationSet类是一个Animation集合,里面可以许多Animation,
//且在AnimationSet中设置的属性适用于里面的所有Animation。
//参数true 则共享@Interpolator
AnimationSet set = new AnimationSet(true); //透明
Animation alphaAnim = getAlphaAnimation();
set.addAnimation(alphaAnim); //缩放
Animation scalegetAnim = getScaleAnimation();
set.addAnimation(scalegetAnim); //旋转
Animation rotateAnim = getRotateAnimation();
set.addAnimation(rotateAnim); //移动 上面三个动画是同时进行的,我现在需要让移动这个动画在上面的动画之后执行
//需要使用setStartOffset 设置动画开始的时间
Animation translateAnim = getTranslateAnimation();
translateAnim.setStartOffset(500);
set.addAnimation(translateAnim); v.startAnimation(set);
}
}); } /**
* 透明效果
* @return
*/
public Animation getAlphaAnimation() {
//实例化 AlphaAnimation 主要是改变透明度
//透明度 从 1-不透明 0-完全透明
Animation animation = new AlphaAnimation(1.0f, 0.8f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(1000);
return animation;
} /**
* 缩放动画
* @return
*/
public Animation getScaleAnimation() {
//实例化 ScaleAnimation 主要是缩放效果
//参数:fromX-动画开始前,x坐标 toX-动画结束后x坐标
//fromY-动画开始前,Y坐标 toY-动画结束后Y坐标
//pivotXType - 为动画相对于物件的X坐标的参照物 pivotXValue - 值
//pivotYType - 为动画相对于物件的Y坐标的参照物 pivotYValue - 值
Animation animation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(1000);
return animation;
} /**
* 旋转
* @return
*/
public Animation getRotateAnimation() {
//实例化RotateAnimation
//以自身中心为圆心,旋转360度 正值为顺时针旋转,负值为逆时针旋转
RotateAnimation animation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(1000);
return animation;
} /**
* 移动
* @return
*/
public Animation getTranslateAnimation() {
//实例化TranslateAnimation
//以自身为坐标系和长度单位,从(0,0)移动到(1,1)
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(1000);
return animation;
} }

Interpolator 描述动画的速率:

安卓默认的Interpolator:

AccelerateInterpolator:动画开始时比较慢,然后逐渐加速。

DecelerateInterpolator:动画开始时比较快,然后逐渐减速。

AccelerateDecelerateInterpolator:动画开始时和结束时比较慢,中间过程加速。

LinearInterpolator:动画匀速进行。

CycleInterpolator:动画循环播放指定次数,速率沿着正弦曲线改变。

DecelerateInterpolator代码:主要实现getInterpolation ,也可以自定义

/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package android.view.animation; import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet; /**
* An interpolator where the rate of change starts out quickly and
* and then decelerates.
*
*/
public class DecelerateInterpolator implements Interpolator {
public DecelerateInterpolator() {
} /**
* Constructor
*
* @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces
* an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the
* ease-out effect (i.e., it starts even faster and ends evens slower)
*/
public DecelerateInterpolator(float factor) {
mFactor = factor;
} public DecelerateInterpolator(Context context, AttributeSet attrs) {
TypedArray a =
context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.DecelerateInterpolator); mFactor = a.getFloat(com.android.internal.R.styleable.DecelerateInterpolator_factor, 1.0f); a.recycle();
} public float getInterpolation(float input) {
float result;
if (mFactor == 1.0f) {
result = (float)(1.0f - (1.0f - input) * (1.0f - input));
} else {
result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));
}
return result;
} private float mFactor = 1.0f;
}

使用:

        //设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());

AnimationListener 监听器:

可以监听动画前,动画结束,动画repeat的时候的动作,对上面代码中的移动效果进行动画监听:

 /**
* 移动
* @return
*/
public Animation getTranslateAnimation() {
//实例化TranslateAnimation
//以自身为坐标系和长度单位,从(0,0)移动到(1,1)
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置重复动画
animation.setRepeatCount(2);
//设置动画执行时间
animation.setDuration(1000);
//设置监听器
animation.setAnimationListener(new AnimationListener() { @Override
public void onAnimationStart(Animation animation) {
//动画开始前
Toast.makeText(getBaseContext(), "Strart!", Toast.LENGTH_SHORT).show();
} @Override
public void onAnimationRepeat(Animation animation) {
//重复动画的时候,
Toast.makeText(getBaseContext(), "Repeat!", Toast.LENGTH_SHORT).show();
} @Override
public void onAnimationEnd(Animation animation) {
// 结束动画的时候
Toast.makeText(getBaseContext(), "End!", Toast.LENGTH_SHORT).show(); }
});
return animation;
}

安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果的更多相关文章

  1. Unity3d动画脚本 Animation Scripting(深入了解游戏引擎中的动画处理原理)

    也许这一篇文章的内容有点枯燥,但我要说的是如果你想深入的了解游戏引擎是如何处理动画片断或者素材并 让玩家操控的角色动起来栩栩如生,那么这真是一篇好文章(当然我仅仅是翻译了一下)   动画脚本 Anim ...

  2. 程序员带你学习安卓开发,十天快速入-对比C#学习java语法

    关注今日头条-做全栈攻城狮,学代码也要读书,爱全栈,更爱生活.提供程序员技术及生活指导干货. 如果你真想学习,请评论学过的每篇文章,记录学习的痕迹. 请把所有教程文章中所提及的代码,最少敲写三遍,达到 ...

  3. 十大经典排序算法详细总结(含JAVA代码实现)

    原文出处:http://www.cnblogs.com/guoyaohua/p/8600214.html 0.排序算法说明 0.1 排序的定义 对一序列对象根据某个关键字进行排序. 0.2 术语说明 ...

  4. .net mvc 站点自带简易SSL加密传输 Word报告自动生成(例如 导出数据库结构) 微信小程序:动画(Animation) SignalR 设计理念(一) ASP.NET -- WebForm -- ViewState ASP.NET -- 一般处理程序ashx 常用到的一些js方法,记录一下 CryptoJS与C#AES加解密互转

    .net mvc 站点自带简易SSL加密传输   因项目需要,传输数据需要加密,因此有了一些经验,现简易抽出来分享! 请求:前端cryptojs用rsa/aes 或 rsa/des加密,后端.net ...

  5. 微信小程序:动画(Animation)

    简单总结一下微信动画的实现及执行步骤. 一.实现方式 官方文档是这样说的:①创建一个动画实例 animation.②调用实例的方法来描述动画.③最后通过动画实例的 export 方法导出动画数据传递给 ...

  6. 帧动画的创建方式 - 纯Java代码方式

    废话不多说,先看东西 帧动画的创建方式主要以下2种: * 用xml创建动画: * 纯Java代码创建动画:   本文内容主要关注 纯java代码创建帧动画 的方式: 用xml创建帧动画:http:// ...

  7. 【Android UI设计和开发】动画(Animation)详细说明(一)

    Android开发之动画效果浅析 请尊重他人的劳动成果.转载请注明出处:Android开发之动画效果浅析 程序执行效果图: Android动画主要包括补间动画(Tween)View Animation ...

  8. 安卓开发_浅谈Android动画(四)

    Property动画 概念:属性动画,即通过改变对象属性的动画. 特点:属性动画真正改变了一个UI控件,包括其事件触发焦点的位置 一.重要的动画类及属性值: 1.  ValueAnimator 基本属 ...

  9. 安卓开发_浅谈Android动画(二)

    在学习了四个基本动画之后,现在要学习一些更有用的效果 先给出所有的动画xml <?xml version="1.0" encoding="utf-8"?& ...

随机推荐

  1. BZOJ 1729: [Usaco2005 dec]Cow Patterns 牛的模式匹配

    Description 约翰的N(1≤N≤100000)只奶牛中出现了K(1≤K≤25000)只爱惹麻烦的坏蛋.奶牛们按一定的顺序排队的时候,这些坏蛋总会站在一起.为了找出这些坏蛋,约翰让他的奶牛排好 ...

  2. php访问类静态属性

    在类的外部,如果要使用到类的静态变量,则可以使用 :: 操作符. <?php class A { static $x = 10; function test() { echo self::$x; ...

  3. easyui源码翻译1.32--SearchBox(搜索框)

    前言 使用$.fn.searchbox.defaults重写默认值对象.下载该插件翻译源码 搜索框提示用户需要输入搜索的值.它可以结合一个菜单,允许用户选择不同的搜索类别.在用户按下回车键或点击组件右 ...

  4. 轻松使用Nginx搭建web服务器

    如果读者以前做过web开发的话,就应该知道如何去搭建一个web服务器来跑你的web站点,在windows下你可能会选择去用IIS,十分的快捷,在linux下,你可能首先会想到apache,“一哥”( ...

  5. asp.net 框架初接触

    1. 在web层的Default.aspx里只有最基本的UI代码 2. 在web层的Default.aspx.cs里第一行创建一个用户业务对象UserBO (注意添加引用,下同) protected ...

  6. USB CDC类

    现代嵌入式系统中,异步串行通信接口往往作为标准外设出现在单片机和嵌入式系统中.但是随着个人计算机通用外围设备越来越少地使用串口,串口正在逐渐从个人计算机特别是便携式电脑上消失.于是嵌入式开发人员常常发 ...

  7. Unicode编码的熟悉与研究过程(内附全部汉字编码列表)

    我有一个问题是:是不是会有个别汉字无法在Unicode下表示,这种情况下就不能完全显示了? 各种编码查询表:http://bm.kdd.cc/ ---------------------------- ...

  8. Android 滑动菜单SlidingMenu

    首先我们看下面视图: 这种效果大家都不陌生,网上好多都说是仿人人网的,估计人家牛逼出来的早吧,我也参考了一一些例子,实现起来有三种方法,我下面简单介绍下: 方法一:其实就是对GestureDetect ...

  9. Android:MD5加密

    /** * @author gongchaobin * * MD5加密 * * @version 2013-8-22 */ public class MD5Util { // 用来将字节转换成 16 ...

  10. -_-#【Cookie】缩小 Cookie

    Reduce Cookie Size Cookie 是个很有趣的话题.根据 RFC 2109 的描述,每个客户端最多保持 300 个 Cookie,针对每个域名最多 20 个 Cookie (实际上多 ...