Android自定义视图淡化/延迟动画

时间:2017-01-21 19:43:01

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

我很容易上课,画圈子。我给出参数,视图计算其余的参数。在画到画布期间,我想给每个人一些延迟和淡化效果。我回顾了一些关于动画师和处理程序的文章,但我无法弄清楚。请给我看一些实现。感谢。

    @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);
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

这是一个按钮视图的简单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);

答案 1 :(得分:0)

您可以使用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()来完成这项工作,但这会给你一个如何做到的印象。