简体   繁体   English

Android中的画笔绘画动画效果

[英]Brush Painting Animation Effect in Android

I want to create the brush painting effect below. 我要在下面创建画笔绘画效果。 I have no idea how to do this or where to start. 我不知道该怎么做或从哪里开始。 Searching for solutions on the web yielded no useable results. 在网络上搜索解决方案没有得到可用的结果。 Any help would be appreciated! 任何帮助,将不胜感激!

在此处输入图片说明

There are many ways to do this. 有很多方法可以做到这一点。 The best possible way would probably be to render the animation in a third party tool and then just play it back on the device, but there are also a few simpler ways - for example just using a ValueAnimator and some drawables or an AnimationDrawable - to create something like this. 最好的可能方法是在第三方工具中渲染动画,然后在设备上播放它,但是还有一些更简单的方法-例如,仅使用ValueAnimator和一些drawables或AnimationDrawable创建这样的事情。

I'm going to demonstrate the ValueAnimator version in this answer. 我将在此答案中演示ValueAnimator版本。 The result should look something like this: 结果应如下所示:

在此处输入图片说明

There are two parts to this animation: 此动画分为两部分:

  1. The brush 刷子
  2. The paint the brush leaves behind 油漆刷掉了

For the brush I use a transparent png image of a brush. 对于画笔,我使用画笔的透明png图像。 The paint the brush leaves behind is just a FrameLayout with a black background color. 画笔留下的油漆只是具有黑色背景色的FrameLayout

Using a FrameLayout as container I place them in my layout like this: 使用FrameLayout作为容器,我将它们放置在我的布局中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <FrameLayout
            android:id="@+id/paint"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical"
            android:background="@android:color/black"/>

        <ImageView
            android:id="@+id/brush"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/paint_brush"/>

    </FrameLayout>

</FrameLayout>

The code to perform the animation is quite simple: 执行动画的代码非常简单:

final View brush = findViewById(R.id.brush);
final View paint = findViewById(R.id.paint);

final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setDuration(6000L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        final float progress = (float) valueAnimator.getAnimatedValue();
        paint.setTranslationX(-paint.getWidth() * (1.0f - progress));
        brush.setTranslationX(paint.getWidth() * progress);
    }
});
animator.start();

The ValueAnimator is set to repeat infinitely in this example. 在此示例中, ValueAnimator设置为无限重复。 In the AnimatorUpdateListener I update the translationX property to move the paint and the brush together across the screen. AnimatorUpdateListener我更新了translationX属性,以在屏幕上一起移动绘画和画笔。 The paint is offset by the width of the screen so it starts of all the way off screen and moves in behind the brush to create the illusion of the brush painting on the screen. 绘画的偏移量是屏幕的宽度,因此它从屏幕开始一直延伸到画笔的后面,从而在屏幕上产生画笔绘画的错觉。

If you have any further questions feel free to ask! 如果您还有其他问题,请随时提出!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM