android的颜色渐变动画效果图,android – 渐变色动画

我想要实现的目标:

径向渐变,开始和结束颜色随着时间的推移从一种定义的颜色到另一种颜色的平滑变化.

到目前为止我尝试过的:

像这样使用ObjectAnimator:

searchAnimator = ObjectAnimator.ofFloat(drawThread, new Property(Float.TYPE, "fraction") {

@Override

public Float get(DrawThread object) {

return object.fraction;

}

@Override

public void set(DrawThread object, Float value) {

object.setFraction(value);

}

}, 0, 1);

searchAnimator.setDuration(maxSearchDuration);

searchAnimator.setInterpolator(new LinearInterpolator());

这将调用DrawThread.setFraction(value);随着时间的推移.在线程内部,我使用SurfaceView执行Canvas绘图,如下所示:

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mPaint.setDither(true);

mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

radius = (int) Math.sqrt(canvas.getWidth() / 2 * canvas.getWidth() / 2 + canvas.getHeight() / 2 * canvas.getHeight() / 2);

//calculating colors for current fraction using ARGBEvaluator

int start = (int) argbEvaluator.evaluate(fraction, colors[0].start, colors[1].start);

int end = (int) argbEvaluator.evaluate(fraction, colors[0].end, colors[1].end);

//end

mPaint.setShader(new RadialGradient(canvas.getWidth() / 2, canvas.getHeight() / 2,

radius, start, end, Shader.TileMode.CLAMP));

canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, radius, mPaint);

问题:

>渐变不平滑.图像看起来像低色

>性能非常低.我在FullHD Snapdragon 801设备上得到aprox 16 FPS.

所以我想问的是提高性能(甚至是完全不同的方式)以及提高最终图像质量的任何帮助.

解决方法:

您可以像这样设置渐变中的颜色变化:

int colorFrom = ContextCompat.getColor(this, R.color.red);

int colorTo = ContextCompat.getColor(this, R.color.blue);

ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);

colorAnimation.setDuration(2000);

int color1 = ContextCompat.getColor(this, R.color.red);

int color2 = ContextCompat.getColor(this, R.color.green);

GradientDrawable gradientDrawable = new GradientDrawable(

GradientDrawable.Orientation.TOP_BOTTOM, new int[]{color1, color2});

gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);

colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animator) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

gradientDrawable.setColors(new int[]{(int) animator.getAnimatedValue(), color2});

viewWithGradientBg.setBackground(gradientDrawable);

}

}

});

colorAnimation.start();

标签:android,gradient,animation,performance

来源: https://codeday.me/bug/20190708/1399780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值