通过AnimationSet设置动画
在代码中可以通过set来设置多个动画属性,这里分开来设置不同的属性。
首先先贴上布局文件,里面的imageview是用来做动画的控件

<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"
tools:context="${relativePackage}.${activityClass}" > <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:orientation="vertical" > <Button
android:id="@+id/alpha_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener"
android:text="透明度改变" /> <Button
android:id="@+id/rotate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener"
android:text="旋转动画" /> <Button
android:id="@+id/scale_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener"
android:text="缩放动画" /> <Button
android:id="@+id/translate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener"
android:text="移动动画" />
</LinearLayout> <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:src="@drawable/ic_launcher" /> </RelativeLayout>
在activity中的listener中写上不同的动画效果
主要是
AnimationSet set = new AnimationSet(true);
set.addAnimation(alpha);
package com.kale.anim; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
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.TranslateAnimation;
import android.widget.ImageView; public class MainActivity extends Activity { ImageView iV; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iV = (ImageView)findViewById(R.id.imageView);
} public void buttonListener(View v) {
AnimationSet set = new AnimationSet(true);
switch (v.getId()) {
case R.id.alpha_button:
//设置渐变从不透明->透明,1表示不透明,0表示透明
AlphaAnimation alpha = new AlphaAnimation(1f, 0f);
//设置执行的时间
alpha.setDuration(1000);
set.addAnimation(alpha);
break;
case R.id.rotate_button:
//RotateAnimation rotate = new RotateAnimation(0, 180, 0, 0);//从0度旋转到180度,以左上角(0,0)为圆心
//从相对于自身(圆心在图片的中心)旋转360度,
//x轴坐标相对于父控件宽的一半,y轴相对于自身高的一半,于是确定一个圆心
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f, //0.5 = 1/2的自己父控件的长度
Animation.RELATIVE_TO_SELF, 0.5f);//0.5 = 1/2的自己的长度
rotate.setDuration(5000);
set.addAnimation(rotate);
break;
case R.id.scale_button:
//缩放动画,x坐标从1f->2f,y坐标从1f->2f。缩放的轴是相对于自己的一半,等于是自己的中心
ScaleAnimation scale = new ScaleAnimation(1f, 2f, 1f, 2f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(1000);
set.addAnimation(scale);
break;
case R.id.translate_button:
//移动动画.x:从相对于自己x轴为0的位置移动到相对于自己x轴为1的位置。等于自己向右边移动一个身位
//y:从相对于自己y轴为0的位置移动到相对于自己y轴为1的位置。等于自己向下移动了两个身位
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 2f);
translate.setDuration(1000);
set.addAnimation(translate);
break;
default:
break;
}
//设置开始动画
iV.startAnimation(set);
}
}
补充:
set.setStartOffset(1000);//一秒后再执行动画 = 等待1秒后执行动画
set.setFillAfter(true);//设置动画执行后保持最后状态
set.setFillBefore(false);//设置动画执行后不回到原来状态
set.setRepeatCount(3);//设置动画重复执行的次数
通过AnimationSet设置动画的更多相关文章
- android 后台代码设置动画
1.设置旋转动画 final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF, 0. ...
- 使用ObjectAnimator设置动画
ObjectAnimator是ValueAnimator的子类,他本身就已经包含了时间引擎和值计算,所以它拥有为对象的某个属性设置动画的功能.这使得为任何对象设置动画更加的容易.你不再需要实现 Val ...
- UI设计篇·入门篇·简单动画的实现,为布局设置动画,用XML布置布局动画
不仅仅控件可以设置动画,一个布局也可以设置动画, 当给一个布局设置了动画的时候,这个布局里所包含的控件都会依赖执行这些动画. 为布局设置动画的实现步骤: 1.新建一个动画,设置需要实现的形式 2.新建 ...
- XamarinAndroid组件教程RecylerView适配器设置动画示例
XamarinAndroid组件教程RecylerView适配器设置动画示例 [示例1-3]下面将在RecylerView的子元素进行滚动时,使用适配器动画.具体的操作步骤如下: (1)创建一个名为R ...
- XamarinAndroid组件教程RecylerView适配器设置动画
XamarinAndroid组件教程RecylerView适配器设置动画 本小节将讲解动画相关设置,如动画的时长.插值器以及复合动画等. 1.设置动画时长 设置动画持续的时间可以使用Animation ...
- XamarinAndroid组件教程设置动画的设置插值器
XamarinAndroid组件教程设置动画的设置插值器 为动画设置插值器,可以使用BaseItemAnimator抽象类中的SetInterpolator()方法,其语法形式如下: public v ...
- XamarinAndroid组件教程设置动画的时长参数
XamarinAndroid组件教程设置动画的时长参数 在添加动画的时候,开发者还可以动画参数进行设置,如动画持续的时长.插值器等.下面依次讲解动画参数的设置方法. 1.设置动画时长 设置动画持续的时 ...
- VUE - 路由跳转时设置动画效果
/* 为对应的路由跳转时设置动画效果 */ <transition name="fade"> <router-view /> & ...
- AndroidUI 视图动画-混合动画效果 (AnimationSet)/动画效果监听
在前面介绍了几种动画效果:透明动画效果(AlphsAnimation).移动动画效果(TranslateAnimation).旋转动画效果(RotateAnimation).缩放动画效果(ScaleA ...
随机推荐
- SpringMVC介绍及参数绑定
本节内容: SpringMVC介绍 入门程序 SpringMVC架构 SpringMVC整合MyBatis 参数绑定 SpringMVC和Struts2的区别 一.SpringMVC介绍 1. 什么是 ...
- .NetCore Linux中安装Grafana界面及配置InfluxDB相关设置
前面的文章已经安装好了InfluxDB 安装 wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.1. ...
- Git(二)使用git管理文件版本(TortoiseGit )
一.创建版本库 什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都 ...
- Git(一)Git的简介安装
一.git历史 同生活中的许多伟大事件一样,Git 诞生于一个极富纷争大举创新的年代.Linux 内核开源项目有着为数众广的参与者.绝大多数的 Linux 内核维护工作都花在了提交补丁和保存归档的繁琐 ...
- 【AtCoder】AGC029(A-E)
A - Irreversible operation 题解 把每个B后面的W个数累加起来即可 代码 #include <bits/stdc++.h> #define fi first #d ...
- 关于spark ui中executor显示的内存量与设置的内存量不符的问题
executor显示的内存量是实际执行程序使用的内存量,也就是排除bspark.storage.memoryFraction设置的比例外,然后使用的内存量. 默认是0.6,所以executory和dr ...
- thinkphp中table方法
table方法也属于模型类的连贯操作方法之一,主要用于指定操作的数据表. 用法 一般情况下,操作模型的时候系统能够自动识别当前对应的数据表,所以,使用table方法的情况通常是为了:切换操作的数据表: ...
- CentOS和Windows下配置MySQL远程访问的教程
CentOS和Windows下配置MySQL远程访问的教程 一.前言 由于实验在云服务器上跑的结果不是很理想.所以,现在切换到局域网服务器.因此,需要重新配置 Windows 服务器和 CentO ...
- 纯css滚动视差
1.何为滚动视差 视差滚动(Parallax Scrolling)是指让多层背景以不同的速度移动,形成立体的运动效果,带来非常出色的视觉体验. 作为网页设计的热点趋势,越来越多的网站应用了这项技术.效 ...
- 使用 jquery jroll2 开发仿qq聊天列表侧滑功能
由于开发需求,需要做一个类似qq的聊天界面,侧滑弹出单条item右侧菜单,菜单可点击,效果如下图(包括点击事件+长按事件): 1.项目主体dom和css 页面结构比较简单,顶部header做了fixe ...