2017-04-19 3 views
-2

私はチャットアプリケーションを作っています。 私はユーザープロフィールのために1つのイメージビューを持っています。文字列 "A"をアンドロイドの画像に変換する方法は?

ユーザーの画像がnull /空の場合、ユーザー名の最初の文字を表示する必要があります。たとえば、ユーザー名が「UNKNOWN」と仮定して、その画像ビューでUのみを表示したいとします。
Click here to see sample of what I want

私が試してみました:ユーザーのイメージが

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; 
     } 
    } 

すべてのヘルプnullの場合

は、まず私がチェックしましたか?

+0

26種類の事前構築された画像があり、必要なものを動的に作成するのではなく、簡単に選択できるようになる場合があります。 – Neil

答えて

0

textviewを使用して最初の文字を表示し、背景色を追加します。 Randomクラスを使用すると、毎回背景としてランダムな色を取得できます。

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のスクリーンキャンバスに描画するだけです。

関連する問題