2017-04-19 18 views
-2

我想提出一個聊天應用程序。 我有一個imageview的用戶配置文件。如何轉換字符串「A」到圖像中的Android?

如果用戶圖像爲空/空,我想顯示用戶名的第一個字母,例如:假設用戶名是「UNKNOWN」,所以我只想在該圖像視圖上顯示U.
Click here to see sample of what I want

我曾嘗試:

首先我檢查,如果用戶圖像爲null

if (item.getSender_img_url() == null || item.getSender_img_url().isEmpty()||item.getSender_img_url().equals("null")) { 
       System.out.println(TAG + " photo is not available "); 

    //here i am getting first alphabet of Users Name 
       String[] result = item.getSenderName().split("\\s+"); 
      // This is a regex for matching spaces 
       // The string we'll create 
       String abbrev = ""; 

       // Loop over the results from the string splitting 
       for (int i = 0; i < result.length; i++) { 

        System.out.println(TAG + " abbrev 1 "+ abbrev); 
        // Grab the first character of this entry 
        char c = result[i].charAt(0); 

        System.out.println(TAG + " abbrev c "+ c); 
        // If its a number, add the whole number 
        if (c >= '0' && c <= '9') { 
         abbrev += result[i]; 
        } 

        // If its not a number, just append the character 
        else { 
         abbrev += c; 
        } 

        System.out.println(TAG + " abbrev 3 "+ abbrev); 
       } 

       //here i am converting string value into image 

       Bitmap bm = StringToBitMap(abbrev.toString()); 

       //here i am setting covertes bitmat image into image view 

       ((ViewHolder) holder).friends_image.setImageBitmap(bm); 


    } else { 
       System.out.println(TAG + " photo is available "); 
       try { 
        Picasso.with(mContext).load(item.getSender_img_url()) 
          .error(R.drawable.profile_avatar) 
          .placeholder(R.drawable.profile_avatar).resize(40, 40).centerCrop() 
          .into(((ViewHolder) holder).friends_image); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } 







................................. 

/** 
    * @param encodedString 
    * @return bitmap (from given string) 
    */ 
    public static Bitmap StringToBitMap(String encodedString){ 
     try { 
      byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT); 
      Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); 
      return bitmap; 
     } catch(Exception e) { 
      e.getMessage(); 
      return null; 
     } 
    } 

任何幫助嗎?

+0

只需要26個不同的預先構建的圖像並選擇所需的圖像可能會更容易,而不是試圖動態創建一個。 – Neil

回答

0

使用的TextView顯示的第一個字母,並添加背景顏色給它。每次使用隨機類獲取隨機顏色作爲背景。

private String senderFirstLetter; 
     private TextView senderProfilePic; 
// To retrieve first letter of the sender, 
    senderFirstLetter = (String) holder.sender.getText().subSequence(0, 1); 
      holder.senderProfilePic.setText(senderFirstLetter); 
// To get random color 
    GradientDrawable drawable = (GradientDrawable) holder.senderProfilePic.getBackground(); 
      Random randomBackgroundColor = new Random(); 
      int color = Color.argb(255, randomBackgroundColor.nextInt(256), randomBackgroundColor.nextInt(256), randomBackgroundColor.nextInt(256)); 
      drawable.setColor(color); 
1

而不是顯示與字符串圖像的你應該使用View與背景顏色和內部的走TextView。類似於

<RelativeLayout ... 
      ...> 

    <TextView></TextView> 

</RelativeLayout> 
+0

謝謝..我這樣做..但我想將字符串轉換爲圖像 –

+0

它可能需要更多的內存空間和處理時間我猜,如果您有數百條記錄,它將需要更多時間來創建這些圖像。 –

0

這其實很簡單。您可以將文本繪製到位圖中。

Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
textPaint.setColor(Color.BLACK); 

Paint circlePaint = new Paint(); 
circlePaint.setColor(Color.GREEN); 

int w = 30; // Width of profile picture 
int h = 30; // Height of profile picture 
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap); 
canvas.drawCircle(w/2, h/2, w/2, circlePaint); 
canvas.drawText("A", 0, 0, textPaint); 

您可以選擇通過查找文本大小使其居中。接下來你要做的就是將位圖繪製到onDraw的屏幕畫布上。

相關問題