简体   繁体   English

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

[英]Android Custom View Fade/Delay Animation

I have quite easy class that draws circles. 我的课很简单,很容易画圈。 I'm giving the parameter, view calculates the rest. 我给参数,视图计算其余部分。 All I want to give some delay and fade effect for each during draw to canvas. 我只想在绘制到画布的过程中给每个延迟和淡入淡出效果。 I reviewed a few articles about animators and handlers but I couldn't figure out. 我查看了一些有关动画师和处理程序的文章,但我不知道。 Please, show me some implementations. 请给我看一些实现。 Thanks. 谢谢。

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

在此处输入图片说明

Here's a simple alpha animation of a button view [it makes the button blink](it's not so hard ;O) ): 这是一个按钮视图的简单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);

You could use the setAlpha(int a) method from the Paint class. 您可以使用Paint类中的setAlpha(int a)方法。 It should work when you do it on a separate Thread with a little time delay in a loop where you count down from 255 to 0. 当您在一个单独的线程中执行此操作时,它应该会工作,并在从255倒数到0的循环中稍微延迟一下时间。

Here is a code sample where I tried this for earlier versions of Android some years ago : 这是一个代码示例,几年前我在较早版本的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();

}

Nowadays I would probably use a Handler with postDelayed() for this job, but this should give you an impression of how it can be done. 如今,我可能会使用带有postDelayed()Handler来完成这项工作,但这应该使您印象深刻。

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

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