android中为我们定义了几种常见的动画:
AlphaAnimation:透明度变化动画
RotateAnimation:旋转动画
ScaleAnimation: 缩放动画
TranslateAnimation:位置变化动画
但这是不够的,很多时候我们需要实现自己的动画,查看任何一个类的源码,比如AlphaAnimation,可以发现他们都是继承自Animation基类,并且都重写了initialize,applyTransformation方法。于是我们也按部就班的做,一般来说applyTransformation方法是必须重写的。
一个最简单控制透明度变化的动画:
class AlphaAnimation extends Animation {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
t.setAlpha(interpolatedTime);
}
}
一个左右抖动的动画,当然也可以使用android自带的那个抖动。
/**
* 左右抖动
* @author reyo
*
*/
class ShakeAnimation extends Animation {
private int shakeTimes=7;//摇摆次数
private int shakeRange=50;//摇摆幅度
public ShakeAnimation(){
}
public ShakeAnimation(int shakeTimes,int shakeRange){
this.shakeTimes = shakeTimes;
this.shakeRange = shakeRange;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime,Transformation t) {
int dx=(int)(shakeRange*Math.sin(interpolatedTime*Math.PI*shakeTimes));
int dy=0;
t.getMatrix().setTranslate(dx,dy);
}
}
控制小球做圆周运动的动画,可以实现类似火星,金星,地球围绕太阳转的效果。
/**
* 圆周运动
* @author reyo
*
*/
class CircleAnimation extends Animation {
private int r=100;//旋转半径
private int angle=360;//旋转角度
public CircleAnimation(){
}
public CircleAnimation(int r,int angle){
this.r = r;
this.angle = angle;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
protected void applyTransformation(float interpolatedTime,Transformation t) {
int dx=(int)(r*Math.cos(interpolatedTime*Math.PI*angle/180));
int dy=(int)(r*Math.sin(interpolatedTime*Math.PI*angle/180));
t.getMatrix().setTranslate(dx,dy);
}
}
如果你数学学的足够好,那么可以实现更复杂的动画,我这里只是运用了几个简单的sin,cos罢了,点到为止!
分享到:
相关推荐
//自定义一个类继承Animation(android.view.animation.Animation)抽象类 public class MyAnimation extends Animation { // 先定义成员变量 //X轴的中心坐标 int center_X; //Y轴的中心坐标 int center_Y; // ...
本示例通过继承Animation自定义Rotate3D,实现3D翻页效果。效果图如下: 1、Rotate3D(Animation) 首先,自定义Animation的3D动画类Rotate3D public class Rotate3D extends Animation { private float f
注意,本控件非继承自ViewGroup,而是纯自定义View,实现的仿饿了么加入购物车控件,自带闪转腾挪动画的按钮。 图1 项目中使用的效果,考虑到了View的回收复用, 并且可以看到在RecyclerView中使用,切换...
继承此类, 实现一些自定义动画 所有动画共享的功能 2、功能作用 循环操作 setLoopCount(count):设置循环次数 currentLoop():当前循环 currentLoopTime():当前循环时间 时间操作 duration():单次时长 ...
自定义控件,主要继承ViewGroup类,自定义属性,添加4种动画,可实现点击事件,代码简单,逻辑清晰,注释详细,适合android初学者使用。
ESTabBarController是一个高度可定制的TabBarController组件,该组件继承自UITabBarController。为什么? 在实际开发中,我们可能会遇到自定义UITabBar的情况。 例如:更改字体样式,添加动画,使用较大的项目。 ...
在上文,酷炫Path动画已经预告了,今天给大家带来的是利用 纯自定义View,实现的仿饿了么加入购物车控件,自带闪转腾挪动画的按钮。 效果图如下: 图1 项目中使用的效果,考虑到了View的回收复用, 并且可以看到...
BackgroundColorAnimationSwift 这是一个简单的背景颜色更改动画,... 在您希望执行背景动画的视图控制器中,继承Viewcontroller中的backgroundColorAnimationDelegate并将dobackgroundanimation函数添加到控制器实现中
3.自定义动画回弹。 先看效果吧: activity-main.xml布局如下: <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android xmlns:tools=http://schemas.android.com/tools android:layout_...
1)View切换的控件—...该方法有两个重载方法,即可以直接传入Animation对象,也可以传入定义的Animation文件的resourceID。 setOutAnimation:设置View退出屏幕时候使用的动画。使用方法和setInAnimation方法一
9.7 自定义View 9.8 控件扩展(表格) 9.8.1 设计实体类 9.8.2 基于ListView的实现 9.9 本章小结 第10章 数据存储 10.1 File(文件) 10.1.1 java.io包的方法 10.1.2 openFileInput和openFileOutput 10.1.3 从...
DOTween是一种快速,高效,完全类型安全的...回调,循环,轻松(包括AnimationCurves和自定义缓动功能),SpeedBased和许多其他补间选项。此外,更新类型的选择:常规,固定,延迟,加上选项,使其与timeScale无关。
简单,优雅的UI菜单,具有圆形布局和材质设计动画 我们专门从事针对移动应用程序和网站的自定义UI的设计和编码。 请随时关注最新更新: 要求 iOS 9.0以上 Xcode 9.0.1 安装 只需将CircleMenuLib文件夹添加到您的项目...
受折叠纸卡材料设计启发,通过动画扩展内容单元。 我们专门从事针对移动应用程序和网站的自定义UI的设计和编码。 请随时关注最新更新: 要求 iOS 8.0以上 Xcode 10.2 安装 只需将FoldingCell.swift文件添加到您的...
以动画方式切换setBaseOrientation(int orientation)Set the orientation of the panels with the values LinearLayout.VERTICAL or LinearLayout.HORIZONTAL, default is LinearLayout.HORIZONTAL.设置排列方式,...
//注意此处的类继承:SilverlightLoader.ISilverlightLoader public partial class MainPage : UserControl, SilverlightLoader.ISilverlightLoader { public MainPage() { InitializeComponent(); } #...
77. 如何屏蔽子类对其父类的自动继承 293 78. 如何让表单设计器使用自定义表单类 296 79. 如何设计能正确显示和存储下拉列表中不存在选项的组合框类 297 80. 如何利用Systray类将图标添加到系统托盘中 304 81. ...
周围的气泡布局,因为布局RatioLayout是继承自ViewGroup,所以布局layout就可以根据自己的需求来布局其子view,view.layout(int l,int t,int r,int b);用于布局子view在父ViewGroup中的位置(相对于父容器),所以在...
16.15 利用接口继承和指派模拟实现继承 522 16.16 Object Browser 527 16.17 事件和类 528 小结 530 自测练习 532 自测练习答案 532 练习 532 第17章 ActiveX 536 17.1 简介 536 17.2 部件、COM和DCOM 536 17.3 ...