【问题标题】:Convert String to Drawable将字符串转换为可绘制对象
【发布时间】:2017-08-10 11:21:50
【问题描述】:

我想提出一个通知,在状态栏中显示一个图标 - 到目前为止一切都很好,但实际上我希望这个图标是 3 个字符的字符串。

所以我的问题是:有没有办法将我的字符串转换为Drawable 以在状态栏中将其显示为图标?

编辑:我最近发现了一个类似的应用 - 电池指示器

它在状态栏中将当前电池电量显示为通知图标 - 我想知道它是否真的使用了不同的 100 张图像

【问题讨论】:

  • 以防万一有人感兴趣:我发现上面提到的应用程序确实每个值使用一个图像
  • 这正是我所需要的。除了用电池状态创建 100 个 png(每个 dpi 值)之外,您找到解决方案了吗?

标签: android


【解决方案1】:

简短:不,你不能。

Long:通知需要R.drawable.something 作为图标,您不能在运行时创建它。

【讨论】:

    【解决方案2】:
       public Drawable getDrawable(String bitmapUrl) {
          try {
            URL url = new URL(bitmapUrl);
            Drawable d =new BitmapDrawable(BitmapFactory.decodeStream(url.openConnection().getInputStream()));
            return d; 
          }
          catch(Exception ex) {return null;}
        }
    

    【讨论】:

    • .setSmallIcon(int) 将 int 作为输入我将如何在那里使用 drawable?
    【解决方案3】:

    您可以制作自己的自定义可绘制对象,该可绘制对象与 textview 小部件一样工作,只是它是可绘制对象而不是视图。 textview 类只是包含文本的可绘制对象的容器。

    【讨论】:

    • 能否请您至少提供一份可以做到这一点的代码草稿?
    • @vault 我没有示例,但您要做的是扩展 Drawable 并覆盖 draw(Canvas),然后从 draw 中调用 canvas.drawText()。
    【解决方案4】:

    我使用了一种解决方法,它对我来说工作正常。

    首先我将字符串转换为位图,然后将其转换为可绘制对象,代码如下:

    byte [] encodeByte=Base64.decode(":",Base64.DEFAULT);
    Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);      
    Drawable d = new BitmapDrawable(bitmap);
    

    希望对你有帮助!

    【讨论】:

    • .setSmallIcon(int) 将 int 作为输入我将如何在那里使用 drawable?
    • 你只能在 .setLargIcon 方法上使用它,该方法以 drawable 作为参数,smallIcon 只接受资源。
    • 我尝试使用 set setLargIcon 但图标永远不可见。什么时候会显示大图标?
    【解决方案5】:

    您看过 API 演示 > 应用 > 通知 > 状态栏吗?

    如果您的字符串选项数量有限(例如笑脸),您可以为每个字符串创建可绘制对象。

    【讨论】:

    • 我想避免这种情况,因为它有很多可能性:)
    【解决方案6】:

    不,您不能,我认为您可以使用与此处相同的方法:Combine image and text to drawable,但您不能,因为通知采用可绘制 id,而不是可绘制对象。

    【讨论】:

      【解决方案7】:

      (我知道这并不能完全回答 OP 的问题,但标题让我来到这里,因为它很笼统。)

      在摆弄了一下之后,我想出了这个解决方案。它相当混乱,可能会改进,但它确实有效。

      在其当前形式中,该函数采用它传递的字符串的第一个字母和该字符串的唯一 ID。 ID 仅用于背景颜色的生成和记忆,因此如果您要使用稳定的颜色,可以将其删除。

      我这样做是为了为没有保存图像的联系人生成默认图像,但它应该很容易适应。它也恰好返回一个 InputStream 而不是一个 Drawable,但你可以在绘制到它之后只返回 bitmap,或者使用 Drawable.createFromStream()

      private static InputStream returnDefaultContact(Context context, String name, long id) {
          Paint textPaint = new Paint();
          textPaint.setColor(Color.WHITE);
          textPaint.setTextAlign(Paint.Align.CENTER);
          textPaint.setTextSize(110);
      
          int color = PreferenceManager.getDefaultSharedPreferences(context).getInt("contact_by_id_" + id, 0);
      
          if (color == 0) {
              int colorValue1 = (int)((56 + Math.random() * 200));
              int colorValue2 = (int)((56 + Math.random() * 200));
              int colorValue3 = (int)((56 + Math.random() * 200));
      
              color = Color.rgb(colorValue1, colorValue2, colorValue3);
      
              PreferenceManager.getDefaultSharedPreferences(context).edit().putInt("contact_by_id_" + id, color).apply();
          }
      
          Paint backgroundPaint = new Paint();
          backgroundPaint.setColor(color);
      
          Bitmap bitmap = Bitmap.createBitmap(120, 120, Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(bitmap);
      
          canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getHeight() / 2, backgroundPaint);
      
          int xPos = (canvas.getWidth() / 2);
          int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
      
          canvas.drawText(name.substring(0, 1), xPos, yPos, textPaint);
      
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
          byte[] imageInByte = stream.toByteArray();
      
          return new ByteArrayInputStream(imageInByte);
      }
      

      【讨论】:

        【解决方案8】:
        try  {
        
            InputStream inputStream = new URL(Your imageWebAddress).openStream();
        
            drawable = Drawable.createFromStream(inputStream, null);
            inputStream.close();
          }
          catch (MalformedURLException ex) { }
          catch (IOException ex) { }
          layout.setBackground(drawable);
        

        【讨论】:

          猜你喜欢
          • 2014-03-18
          • 2011-01-02
          • 1970-01-01
          • 1970-01-01
          • 2014-01-14
          • 2011-08-02
          • 2016-06-10
          相关资源
          最近更新 更多