android 将颜色应用于 alpha 动画

发布于 2024-09-10 00:05:47 字数 120 浏览 4 评论 0 原文

有没有办法在 Android 中将颜色应用于 alpha 动画?我知道如何使用 元素,但我想让 alpha 应用颜色以及 alpha,以便我可以突出显示布局。这可能吗?

is there a way to apply a color to an alpha animation in android? I know how to use the <alpha> element, but i'd like to have the alpha apply a color as well as an alpha so i can hightlight a layout. is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愚人国度 2024-09-17 00:05:48

Android 中的动画不能包含颜色变化——仅包含 Alpha、旋转、缩放和平移。

也就是说,您仍然可以通过将两个不同颜色的对象叠加在一起并使顶部的对象淡入或淡出来更改颜色。

您还可以查看 TransitionDrawableTextSwitcher 来完成类似的事情。希望我们能够在未来的更新中获得对彩色动画的全面支持。

Animations can not include color changes in Android--only alpha, rotation, scale, and translation are included.

That said, you can still make color changes by overlaying two objects of different colors on top of each other and fading the top one in or out.

You could also look into a TransitionDrawable or TextSwitcher to accomplish something similar. Hopefully we will be able to get full support for color animations in a future update.

z祗昰~ 2024-09-17 00:05:48

好吧,这是我的解决方案,对屏幕的特定区域进行动画处理(请参阅下面的演示)。请注意,此代码针对运行 >= API9 的设备。

输入图片此处描述

初学者友好,只需复制和粘贴即可。

FadeAnimationColored.java

public class FadeAnimationColored {

private View view;
private float maxBrightness = 1.0f;
private float minBrightness = 0.0f;
private long duration = 400L;
private long startOffset = 0L;
private int color = android.R.color.white;

// Constructors...

public FadeAnimationColored(View view, String color, float maxBrightness, float minBrightness, long duration, long startOffset) {
    this.view = view;
    this.color = Color.parseColor(color);
    this.maxBrightness = maxBrightness;
    this.minBrightness = minBrightness;
    this.duration = duration;
    this.startOffset = startOffset;
    prepareView();
}


public void fadeOut() {
    this.view.setAlpha(1f);
    Animation anim = new AlphaAnimation(minBrightness, maxBrightness);
    anim.setDuration(duration);
    anim.setStartOffset(startOffset);
    anim.setFillEnabled(true);
    anim.setFillAfter(true);
    view.startAnimation(anim);
}


public void fadeIn() {
    Animation anim = new AlphaAnimation(maxBrightness, minBrightness);
    anim.setDuration(duration);
    anim.setStartOffset(startOffset);
    anim.setFillEnabled(true);
    anim.setFillAfter(true);
    view.startAnimation(anim);
}


private void prepareView() {
    this.view.setBackgroundColor(this.color);
    this.view.setAlpha(0f);
}
}

接下来向您的布局添加一个额外的视图,将其视为覆盖层(我使用了一个简单的 FrameLayout ,它设置为 match_parent

下面是一个片段,展示了如何在 Activity 或 Fragment 中设置动画:

FrameLayout interceptorFrame = (FrameLayout) mView.findViewById(R.id.fl_interceptor);

final FadeAnimationColored fadeAnimationLayout =
        new FadeAnimationColored(interceptorFrame, MaterialColor.GREY_800, 0.9f, 0.0f, 400L, 0);

mFabMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
    @Override
    public void onMenuToggle(boolean opened) {
        if (opened) {
            fadeAnimationLayout.fadeOut();
        } else {
            fadeAnimationLayout.fadeIn();
        }
    }
});

Well, here is my solution animating a particular region of the screen (see demo down below). Please note that this code is targeting devices which run >= API9.

enter image description here

Beginner friendly, just copy and paste.

FadeAnimationColored.java

public class FadeAnimationColored {

private View view;
private float maxBrightness = 1.0f;
private float minBrightness = 0.0f;
private long duration = 400L;
private long startOffset = 0L;
private int color = android.R.color.white;

// Constructors...

public FadeAnimationColored(View view, String color, float maxBrightness, float minBrightness, long duration, long startOffset) {
    this.view = view;
    this.color = Color.parseColor(color);
    this.maxBrightness = maxBrightness;
    this.minBrightness = minBrightness;
    this.duration = duration;
    this.startOffset = startOffset;
    prepareView();
}


public void fadeOut() {
    this.view.setAlpha(1f);
    Animation anim = new AlphaAnimation(minBrightness, maxBrightness);
    anim.setDuration(duration);
    anim.setStartOffset(startOffset);
    anim.setFillEnabled(true);
    anim.setFillAfter(true);
    view.startAnimation(anim);
}


public void fadeIn() {
    Animation anim = new AlphaAnimation(maxBrightness, minBrightness);
    anim.setDuration(duration);
    anim.setStartOffset(startOffset);
    anim.setFillEnabled(true);
    anim.setFillAfter(true);
    view.startAnimation(anim);
}


private void prepareView() {
    this.view.setBackgroundColor(this.color);
    this.view.setAlpha(0f);
}
}

Next add an additional View to your layout, think of it as an overlay (I used a simple FrameLayout which is set to match_parent)

Here is a snippet which shows how to set up the animation in your Activity or Fragment:

FrameLayout interceptorFrame = (FrameLayout) mView.findViewById(R.id.fl_interceptor);

final FadeAnimationColored fadeAnimationLayout =
        new FadeAnimationColored(interceptorFrame, MaterialColor.GREY_800, 0.9f, 0.0f, 400L, 0);

mFabMenu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
    @Override
    public void onMenuToggle(boolean opened) {
        if (opened) {
            fadeAnimationLayout.fadeOut();
        } else {
            fadeAnimationLayout.fadeIn();
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文