无法将自定义可绘制对象转换为位图对象

问题描述 投票:0回答:1

我正在使用this库生成图形艺术。它具有自己的视图以及生成和显示图形的方法。它有一个名为TrianglifyDrawable的类,该类扩展了Drawable的类,并保存图形数据。

我正在尝试执行的操作以在背景中生成图形(使用WorkManager)并将其设置为墙纸。由于WallpaperManager的setWallpaper()仅接受BitMap对象,因此我需要在位图对象中获取图形,但是由于某些原因,我无法从可绘制对象转换为位图。

我已经尝试过在StackOverflow中找到所有可能的方式],然后将该库作为项目导入,并更改了TrianglifyDrawable类的扩展名BitmapDrawable,并尝试了BitmapDrawable.getBitmap()以获取位图对象但它没有用。

最后,我使用setBackground(drawable)将可绘制对象应用于ImageView,然后尝试从imageView.getBackground()中获取可绘制对象,并尝试将其转换为位图,并且它成功完成了[[!

ImageView imageView = findViewById(R.id.testImage); TrianglifyDrawable drawable = new TrianglifyDrawable(100, 100, 100, 100, new BrewerColorGenerator(ColorBrewer.YlGnBu), new CircularPointGenerator()); int[] res = Helper.getDeviceRes(this); imageView.setBackground(drawable); Bitmap bitmap = Bitmap.createBitmap(res[0], res[1], Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); imageView.getBackground().draw(canvas); imageView.setBackgroundColor(Color.RED); imageView.setImageBitmap(bitmap);

我只是不明白所生成的可绘制对象与ImageView背景中的对象之间的区别。

如何复制:

    导入https://github.com/manolovn/trianglify
  1. 尝试使用默认构造函数生成TrianglifyDrawable对象。
  2. 将可绘制对象转换为位图
  3. 尝试使用WallpaperManager.setBitmap将其设置为墙纸
  4. 我如何确定它不起作用:

    生成的可绘制对象可以设置为图像的背景,但转换后的位图为空白。
  1. 相同的转换代码适用于其他可绘制对象
  • 我正在使用这个生成图形艺术的库。它具有自己的视图以及生成和显示图形的方法。它有一个名为TrianglifyDrawable的类,该类扩展了Drawable类,并且包含...
  • java android bitmap drawable android-bitmap
    1个回答
    0
    投票
    生成的可绘制对象最初的宽度为0,高度为0。当用作ImageView的背景时(使用setBackGround(drawable)),该方法将边界设置为等于ImageView的宽度和高度。因此,当转换为位图时,位图显示为空白。
    © www.soinside.com 2019 - 2024. All rights reserved.