使用两种方式完成四种弹性动画

前言

  最近这段时间对弹性动画比较感兴趣,空闲就做了一下弹性动画的实现。网上对弹性动画的实现其实是有3种,属性动画设置spring插值器、facebook出的rebound以及google出的SpringAnimation。考虑到android的google背景以及想重温一下属性动画的使用,本博实现了第一种和第三种。

最终实现效果

  注意上述动态图界面的title,第一个界面是属性动画差值器的实现,第二个界面是SpringAnimation的实现。每一种实现都做了四种常见的动画操作:缩放、平移、旋转、淡入淡出。

思路及代码

  所谓“弹性动画”,其实就是控件的某个属性值在到达某个值之后在该值的左右来回变化(变得比改之大,或变得比该值小),最终稳定在该值的效果。这种变化的动画很像弹簧,所以就叫做“弹性动画”。这种变化作用在view的缩放参数(scaleX、scaleY)、平移参数(transactionX、transactionY)、旋转参数(rotation)、透明度参数(alpha)上会有物理运动的那种平滑过渡的效果,比直接到达该值的那种生硬好很多很多,这也是“弹性动画”的意义了。

  插值器实现

  属性动画的弹性效果实现,是利用插值器。而选择合适的插值器函数至关重要,网上的一篇文章直接给出了函数:pow(2, -10 * x) * sin((x - factor / 4) * (2 * PI) / factor) + 1,我们就可以利用这个函数创建自己的插值器实现此效果:

public class SpringInterpolator implements Interpolator {
   

    private float factor;

    public SpringInterpolator(float factor) {
        this.factor = factor;
    }

    @Override
    public float getInterpolati
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; /** An applet that displays a simple animation */ public class BouncingCircle extends Applet implements Runnable { int x = 150, y = 50, r = 50; // Position and radius of the circle int dx = 11, dy = 7; // Trajectory of circle Thread animator; // The thread that performs the animation volatile boolean pleaseStop; // A flag to ask the thread to stop /** This method simply draws the circle at its current position */ public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(x - r, y - r, r * 2, r * 2); } /** * This method moves (and bounces) the circle and then requests a redraw. * The animator thread calls this method periodically. */ public void animate() { // Bounce if we've hit an edge. Rectangle bounds = getBounds(); if ((x - r + dx < 0) || (x + r + dx > bounds.width)) dx = -dx; if ((y - r + dy < 0) || (y + r + dy > bounds.height)) dy = -dy; // Move the circle. x += dx; y += dy; // Ask the browser to call our paint() method to draw the circle // at its new position. repaint(); } /** * This method is from the Runnable interface. It is the body of the thread * that performs the animation. The thread itself is created and started in * the start() method. */ public void run() { while (!pleaseStop) { // Loop until we're asked to stop animate(); // Update and request redraw try { Thread.sleep(100); } // Wait 100 milliseconds catch (InterruptedException e) { } // Ignore interruptions } } /** Start animating when the browser starts the applet */ public void start() { animator = new Thread(this); // Create a thread pleaseStop = false; // Don't ask it to stop now animator.start(); // Start the thread. // The thread that called start now returns to its caller. // Meanwhile, the new animator thread has called the run() method } /** Stop animating when the browser stops the applet */ public void stop() { // Set the flag that causes the run() method to end pleaseStop = true; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值