2017-04-19 6 views
-2

채팅 응용 프로그램을 만들고 있습니다. 사용자 프로필에 대해 하나의 이미지 뷰가 있습니다.문자열 "A"를 안드로이드의 이미지로 변환하는 방법?

사용자 이미지가 null이거나 비어있는 경우 사용자 이름의 첫 번째 문자를 표시해야합니다. 예를 들어 사용자 이름이 '알 수 없음'으로 가정하고 해당 이미지보기에 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

문자열로 이미지를 표시하는 대신 과 배경색을 사용하고 그 안에 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의 화면 캔버스에 그립니다.