将 GradientDrawable 转换为位图 [英] Convert GradientDrawable to Bitmap

查看:41
本文介绍了将 GradientDrawable 转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下渐变(动态生成):

I have the following gradient (generated dynamically):

    GradientDrawable dynamicDrawable = new GradientDrawable();
    dynamicDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
    dynamicDrawable.setUseLevel(false);
    int colors[] = new int[3];
    colors[0] = Color.parseColor("#711234");
    colors[1] = Color.parseColor("#269869");
    colors[2] = Color.parseColor("#269869");
    dynamicDrawable.setColors(colors);

我想使用 onDraw 方法在视图中设置可绘制对象.

and I want to set that drawable in a view using onDraw method.

当我想将 Drawable 分配给位图时,我使用了强制转换 (BitmapDrawable),但在这种情况下是不可能的,因为gradientDrawable 无法转换为 BitmapDrawable.

When I want to assign a Drawable to a bitmap I use the casting (BitmapDrawable), but in that case is not possible due the gradientDrawable cannot be cast to BitmapDrawable.

知道我如何解决这个问题吗?

Any idea about how I solve that?

提前致谢

推荐答案

  • 使用Bitmap.createBitmap()
  • 创建一个可变位图
  • 使用new Canvas(bitmap)
  • 基于位图创建一个Canvas
  • 然后在您的 GradientDrawable
  • 上调用 draw(canvas)

    这篇关于将 GradientDrawable 转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆