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" ><!-- 用于动画的图片 --><ImageViewandroid: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 {@Overrideprotected 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() {@Overridepublic 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 {@Overrideprotected 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() {@Overridepublic 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 {@Overrideprotected 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() {@Overridepublic 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 {@Overrideprotected 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() {@Overridepublic 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 {@Overrideprotected 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() {@Overridepublic void onClick(View v) {//AnimationSet类是一个Animation集合,里面可以许多Animation,//且在AnimationSet中设置的属性适用于里面的所有Animation。//参数true 则共享@InterpolatorAnimationSet 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() {@Overridepublic void onAnimationStart(Animation animation) {//动画开始前Toast.makeText(getBaseContext(), "Strart!", Toast.LENGTH_SHORT).show();  }@Overridepublic void onAnimationRepeat(Animation animation) {//重复动画的时候,Toast.makeText(getBaseContext(), "Repeat!", Toast.LENGTH_SHORT).show();  }@Overridepublic void onAnimationEnd(Animation animation) {// 结束动画的时候Toast.makeText(getBaseContext(), "End!", Toast.LENGTH_SHORT).show();  }});return animation;}

转载于:https://www.cnblogs.com/snake-hand/p/3184473.html

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

  1. java运用ascii实现动画效果_安卓开发20:动画之Animation 详细使用-主要通过java代码实现动画效果...

    AlphaAnimation 透明效果实现: activity_main.xml中仅仅是一个简单的图片,下面的例子都会使用这个xml: xmlns:tools="http://schemas ...

  2. T4M插件放入unity后怎么找不到_Unity动画系统详解4:如何用代码控制动画?

    摘要:通过上一篇咱们知道了播放动画需要使用Animator,那么如何用代码控制动画呢? 洪流学堂,让你快人几步.你好,我是跟着大智学Unity的萌新,我叫小新,这几周一起来复(yu)习(xi)动画系统 ...

  3. Unity动画系统详解4:如何用代码控制动画?

    摘要:通过上一篇咱们知道了播放动画需要使用Animator,那么如何用代码控制动画呢? 洪流学堂,让你快人几步.你好,我是跟着大智学Unity的萌新,我叫小新,这几周一起来复(yu)习(xi)动画系统 ...

  4. android自定义模态框,安卓开发自定义弹出框的简单方式(纯代码布局)

    弹出框在安卓开发中是经常会用到的,如果单纯的只是用安卓自带的对话框模式肯定是十分单调的. 再来我看来安卓一定有一个功能让我们像Layout添加Layout一样把Layout添加到提示框里面. 所以忘了 ...

  5. Unity3d动画脚本 Animation Scripting(深入了解游戏引擎中的动画处理原理--旧的动画系统)

    (来自:http://blog.sina.com.cn/s/blog_409cc4b00100qmgz.html) 也许这一篇文章的内容有点枯燥,但我要说的是如果你想深入的了解游戏引擎是如何处理动画片 ...

  6. 微信H5分享 代码和详细配置步骤 js + Java 代码

    正常分享状态显示: 分享异常状态显示: 一切变得那么的...无助!!! 开始以为在H5页面上添加一些东西即可,后来发现,完全不是自己想象的那个样子. 这个东西,对于一个从未用过微信JS的码农来说,或许 ...

  7. 安卓开发之数据库——SQLite的详细介绍,详细到连Alt+回车都有说

    安卓原生数据库SQLite的小白级使用方法.自己刚开始学的时候踩过的坑,给大家标出来,供大家参考,也算是记录我的学习过程,我将以用户注册验证登录信息为例. 刚开始学的时候,数据库的查询方法在网上看了很 ...

  8. Android开发获取当前经纬度和详细位置信息(原生代码实现)简单案例

    文章目录 Android定位(经纬度+当前位置信息) 申请权限 LocationManage位置管理器 完整代码 Android定位(经纬度+当前位置信息) 我相信大家在Android开发中应该都有遇 ...

  9. 【安卓开发】天气预报app的UI界面(包含全部代码)

    全部代码https://gitee.com/Cutele/weather-forecast-v1 增加了发送短信等功能 b站视频链接 主页面写的闪退了,就不记录了. 主要是借助适配器显示自己想要显示的 ...

最新文章

  1. CSS进阶(7)—— 内联元素的掌管者line-height和vertical-align(上)
  2. 消息中间件学习总结(4)——RocketMQ之RocketMQ 迈入50万TPS消息俱乐部
  3. Using neural network to combine measures of word semantic similarity for image annotation
  4. 2021大学计算机一级考试试题题库,2021年全国计算机等级考试一级试题及答案.pdf...
  5. 基于聚类算法与随机森林算法的手机终端换机推荐
  6. k3系统 中间层服务器,金蝶k3中间层服务器如何设置
  7. 以企业入侵检测日志分析为场景漫谈大数据安全
  8. matplotlib绘图配色colormap问题
  9. C++ 设计模式 面向对象设计原则 相关概念介绍(结合UnrealEngine4)
  10. Matlab主成分分析法
  11. 求神州战神Z7-SL7D3的2018BIOS
  12. OpenCv 在Win7台式机上的摄像头无法拍照
  13. 一劳永逸解决PPT中声音视频的路径难题
  14. pyinstaller打包使用
  15. 博客园博客Wiz测试
  16. 三级管功放电路设计及参数选择
  17. 【数据库】浅谈WITH CHECK OPTION的作用
  18. 用OpenCv中Mat进行水平投影与垂直投影并实现字符切分
  19. MySQL数据库的约束
  20. M302H-YS-Hi3798MV300H/MV310-当贝纯净桌面卡刷固件包

热门文章

  1. matlab在线性系统理论中的应用,线性系统理论相关的matlab应用.ppt
  2. java 网络爬虫 正则表达式_【干货】Java网络爬虫基础知识
  3. 论文排版之公式居中、编号右对齐
  4. 3.QML布局和输入元素
  5. error: ‘::main‘ must return ‘int‘
  6. ViSP安装与配置VS工程(常见方式-VS2019属性页方法)
  7. 第03课:Spring Boot 启动原理
  8. EOS账户和钱包综合指南
  9. 简单动态启停图 js jquery css3
  10. 乔春洋:网上品牌战略