您现在的位置是:群英 > 开发技术 > 移动开发
Android怎样做颜色渐变的动画,方法是什么
Admin发表于 2022-05-16 17:16:04869 次浏览
相信很多人对“Android怎样做颜色渐变的动画,方法是什么”都不太了解,下面群英小编为你详细解释一下这个问题,希望对你有一定的帮助

前言

案例效果的实现比较简单,利用android自带的颜色插值器argbevaluator()进行计算即可,而本文的重点就是讲讲插值器。

效果图:

一、android中插值器typeevaluator

typeevaluator是一个接口,在开发中可以自定义该接口实例,利用valueanimator的setevaluator(typeevaluator)方法来控制动画的更新计算表达式。在日常开发中,不可能只是需要操纵单一数值的变化,如果需要同时操纵对象的多个属性,如定义动画的x,y移动的坐标等,那就需要对typeevaluator有所了解了。

二、案例效果实现

1.利用android自带的颜色插值器argbevaluator

valueanimator coloranim = objectanimator.ofint(this, "backgroundcolor", red, blue);
        coloranim.setduration(4000);
        coloranim.setevaluator(new argbevaluator());
        coloranim.setrepeatcount(valueanimator.infinite);
        coloranim.setrepeatmode(valueanimator.reverse);
        coloranim.start();

2.看看android自带颜色插值器argbevaluator核心代码

@override
    public object evaluate(float fraction, object startvalue, object endvalue) {
        int startint = (integer) startvalue;
        float starta = ((startint >> 24) & 0xff) / 255.0f;
        float startr = ((startint >> 16) & 0xff) / 255.0f;
        float startg = ((startint >>  8) & 0xff) / 255.0f;
        float startb = ( startint        & 0xff) / 255.0f;

        int endint = (integer) endvalue;
        float enda = ((endint >> 24) & 0xff) / 255.0f;
        float endr = ((endint >> 16) & 0xff) / 255.0f;
        float endg = ((endint >>  8) & 0xff) / 255.0f;
        float endb = ( endint        & 0xff) / 255.0f;

        // 将srgb转化成线性
        startr = (float) math.pow(startr, 2.2);
        startg = (float) math.pow(startg, 2.2);
        startb = (float) math.pow(startb, 2.2);

        endr = (float) math.pow(endr, 2.2);
        endg = (float) math.pow(endg, 2.2);
        endb = (float) math.pow(endb, 2.2);

        //在线性空间中计算插值的颜色
        float a = starta + fraction * (enda - starta);
        float r = startr + fraction * (endr - startr);
        float g = startg + fraction * (endg - startg);
        float b = startb + fraction * (endb - startb);

        //转换回srgb在[0..255]范围
        a = a * 255.0f;
        r = (float) math.pow(r, 1.0 / 2.2) * 255.0f;
        g = (float) math.pow(g, 1.0 / 2.2) * 255.0f;
        b = (float) math.pow(b, 1.0 / 2.2) * 255.0f;

        return math.round(a) << 24 | math.round(r) << 16 | math.round(g) << 8 | math.round(b);
    }

3.根据argbevaluator的实现来自定义一个颜色插值器

public class mycolorevaluator implements typeevaluator

接下来换一种颜色的计算方式,在本人看相关api的过程中,发现color中有colortohsv和hsvtocolor的方法,于是在网上找了一个hvs的计算方式。(以下代码来源于网络)。

@override
    public integer evaluate(float fraction, integer startvalue, integer endvalue) {
        color.colortohsv(startvalue,starthsv);
        color.colortohsv(endvalue,endhsv);
        int alpha = startvalue >> 24 + (int) ((endvalue >> 24 - startvalue >> 24) * fraction);
        // 计算当前动画完成度(fraction)所对应的颜色值
        if (endhsv[0] - starthsv[0] > 180) {
            endhsv[0] -= 360;
        } else if (endhsv[0] - starthsv[0] < -180) {
            endhsv[0] += 360;
        }
        outhsv[0] = starthsv[0] + (endhsv[0] - starthsv[0]) * fraction;
        if (outhsv[0] > 360) {
            outhsv[0] -= 360;
        } else if (outhsv[0] < 0) {
            outhsv[0] += 360;
        }
        outhsv[1]=starthsv[1]+(endhsv[1]-starthsv[1])*fraction;
        outhsv[2]=starthsv[2]+(endhsv[2]-starthsv[2])*fraction;


        return color.hsvtocolor(alpha,outhsv);

    }

4.使用自己定义的颜色插值器mycolorevaluator

valueanimator coloranim = objectanimator.ofint(this, "backgroundcolor", red, blue);
        coloranim.setduration(4000);
        coloranim.setevaluator(new mycolorevaluator());
        coloranim.setrepeatcount(valueanimator.infinite);
        coloranim.setrepeatmode(valueanimator.reverse);
        coloranim.start();

三、源码

colorgradient.java:

public class colorgradient extends view {

    public colorgradient(context context) {
        super(context);
    }

    public colorgradient(context context, @nullable attributeset attrs) {
        super(context, attrs);
        animation();
    }

    public colorgradient(context context, @nullable attributeset attrs, int defstyleattr) {
        super(context, attrs, defstyleattr);
    }
    private void animation(){
        valueanimator coloranim = objectanimator.ofint(this, "backgroundcolor", red, blue);
        coloranim.setduration(4000);
        coloranim.setevaluator(new mycolorevaluator());
        coloranim.setrepeatcount(valueanimator.infinite);
        coloranim.setrepeatmode(valueanimator.reverse);
        coloranim.start();
    }

    
}

mycolorevaluator.java:

public class mycolorevaluator implements typeevaluator<integer> {
    float[] starthsv=new float[3];
    float[] endhsv=new float[3];
    float[] outhsv=new float[3];

    @override
    public integer evaluate(float fraction, integer startvalue, integer endvalue) {
        color.colortohsv(startvalue,starthsv);
        color.colortohsv(endvalue,endhsv);
        int alpha = startvalue >> 24 + (int) ((endvalue >> 24 - startvalue >> 24) * fraction);
        // 计算当前动画完成度(fraction)所对应的颜色值
        if (endhsv[0] - starthsv[0] > 180) {
            endhsv[0] -= 360;
        } else if (endhsv[0] - starthsv[0] < -180) {
            endhsv[0] += 360;
        }
        outhsv[0] = starthsv[0] + (endhsv[0] - starthsv[0]) * fraction;
        if (outhsv[0] > 360) {
            outhsv[0] -= 360;
        } else if (outhsv[0] < 0) {
            outhsv[0] += 360;
        }
        outhsv[1]=starthsv[1]+(endhsv[1]-starthsv[1])*fraction;
        outhsv[2]=starthsv[2]+(endhsv[2]-starthsv[2])*fraction;


        return color.hsvtocolor(alpha,outhsv);

    }
}

通过以上内容的阐述,相信大家对“Android怎样做颜色渐变的动画,方法是什么”已经有了进一步的了解,更多相关的问题,欢迎关注群英网络或到群英官网咨询客服。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-02-10 18:16:11 
摘要:在Excel表格中,对于隔行显示不同颜色的表格还是很常见的,而且实现很方便。那么我们利用PHP怎样做表格隔行颜色的效果呢?文中示例代码介绍的非常详细,对大家学习PHP有一定的帮助,那么感兴趣的朋友接下来一起跟随小编了解看看吧。
2022-07-22 17:44:57 
摘要:python打开py文件一闪而过的解决办法:首先打开py文件的相应代码;然后在程序末尾加上“input()”函数;最后双击【.py】文件的时候就可以看见打印信息。
2022-05-16 17:52:43 
摘要:pma-desktop绿色单文件的 phpmyadmin 桌面版本软件为绿色单文件软件,目前仅支持win,后续支持 linux 和 mac项目目标不用配置php环境,使phpmyadmin更方便的被桌
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部