【问题标题】:Android Custom View Fade/Delay AnimationAndroid 自定义视图淡入淡出/延迟动画
【发布时间】:2017-01-21 19:43:00
【问题描述】:

我有一个很简单的画圆圈的课。我给出参数,视图计算其余部分。所有我想在绘制到画布期间为每个人提供一些延迟和淡入淡出效果。我查看了一些关于动画师和处理程序的文章,但我无法弄清楚。请给我看一些实现。谢谢。

    @Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);
    int w = getWidth();
    int pl = getPaddingLeft();
    int pr = getPaddingRight();
    int totalWidth = w - (pl + pr);
    major = totalWidth / circleCount;
    radius = major / 2;
    startPoint = totalWidth / (circleCount * 2);
    for (int i = 0; i < circleCount; i++) {
        canvas.drawCircle(startPoint + major * i, radius, radius, paint);
    }
}

【问题讨论】:

    标签: android canvas android-animation android-custom-view


    【解决方案1】:

    这是一个简单的按钮视图的 alpha 动画[它使按钮闪烁](它并不难;O)):

    import android.view.animation.AlphaAnimation;
    import android.view.animation.Animation;
    import android.view.animation.LinearInterpolator;
    
    final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
    animation.setDuration(500); // duration - half a second
    animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
    animation.setRepeatCount(Animation.INFINITE);        // Repeat animation infinitely
    animation.setRepeatMode(Animation.REVERSE);          // Reverse animation at the end so the button will fade back in
    
    final Button btn = (Button) findViewById(R.id.but4);//replace this with your view
    btn.startAnimation(animation);
    

    【讨论】:

    • 我正在尝试自定义视图类?其实没有什么看法。我只有画布,所以画布没有 startAnimation 方法。请看我的代码。我知道 alpha 动画在正常情况下是如何工作的
    【解决方案2】:

    您可以使用 Paint 类中的 setAlpha(int a) 方法。 当您在一个单独的线程上执行此操作时,它应该可以工作,并且在循环中从 255 倒数到 0 会有一点时间延迟。

    这是一个代码示例,几年前我在早期版本的 Android 中尝试过:

    private final int FADE_TIME = 10;  // modify to your needs
    
    private void fade() {
    
        new Thread(new Runnable() {
            @Override
            public void run() {
    
                try {
                    int i = 255;
    
                    while (i >= 0) {
    
                        paint.setAlpha(i);
                        Thread.sleep(FADE_TIME);
                        i--;
                    }
    
    
                } catch (InterruptedException e) {
                    // do something in case of interruption
                }
            }
        }).start();
    
    }
    

    现在我可能会使用HandlerpostDelayed() 来完成这项工作,但这应该会让您对它是如何完成的有个印象。

    【讨论】:

      猜你喜欢
      • 2011-03-18
      • 2012-12-27
      • 1970-01-01
      • 2014-10-24
      • 1970-01-01
      • 2013-05-23
      • 2012-01-02
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多