简体   繁体   English

android将颜色应用于alpha动画

[英]android apply color to alpha animation

is there a way to apply a color to an alpha animation in android? 有没有办法将颜色应用于android中的alpha动画? 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. 我知道如何使用<alpha>元素,但我想让alpha应用颜色和alpha以便我可以高亮显示布局。 is this possible? 这可能吗?

Animations can not include color changes in Android --only alpha, rotation, scale, and translation are included. 动画不能包含Android中的颜色变化 - 仅包括 alpha,旋转,缩放和平移。

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. 您还可以查看TransitionDrawableTextSwitcher来完成类似的操作。 Hopefully we will be able to get full support for color animations in a future update. 希望我们能够在将来的更新中获得对彩色动画的全面支持。

I am also trying the same thing on alpha . 我也在尝试alpha上同样的事情。 but http://nathanael.hevenet.com/android-dev-fading-background-animation/ this link may answer your question. http://nathanael.hevenet.com/android-dev-fading-background-animation/此链接可能会回答您的问题。 using transition its possible... :) 使用转换可能...... :)

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 . 请注意,此代码的目标是运行> = API9的设备

在此输入图像描述

Beginner friendly, just copy and paste. 初学者友好,只需复制和粘贴。

FadeAnimationColored.java 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 ) 接下来为您的布局添加一个额外的View,将其视为叠加层(我使用了一个设置为match_parent的简单FrameLayout

Here is a snippet which shows how to set up the animation in your Activity or Fragment: 这是一个片段,显示如何在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();
        }
    }
});

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

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