如何将字符串 "A" 转换为 android 中的图像?
How to Convert String "A" into Image in android?
我正在制作一个聊天应用程序。
我有一个用于用户个人资料的图像视图。
如果用户图像是 null/empty,我想显示用户名的第一个字母,例如:假设用户名是 "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;
}
}
有什么帮助吗?
不应显示带字符串的图像,而应使用 View
和背景色,内部采用 TextView
。像
<RelativeLayout ...
...>
<TextView></TextView>
</RelativeLayout>
这个其实很简单。您可以将文本绘制到位图中。
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.
中将位图绘制到屏幕上 canvas
使用textview显示首字母并为其添加背景色。每次使用 Random class 获取随机颜色作为背景。
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);
我正在制作一个聊天应用程序。 我有一个用于用户个人资料的图像视图。
如果用户图像是 null/empty,我想显示用户名的第一个字母,例如:假设用户名是 "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;
}
}
有什么帮助吗?
不应显示带字符串的图像,而应使用 View
和背景色,内部采用 TextView
。像
<RelativeLayout ...
...>
<TextView></TextView>
</RelativeLayout>
这个其实很简单。您可以将文本绘制到位图中。
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.
中将位图绘制到屏幕上 canvas使用textview显示首字母并为其添加背景色。每次使用 Random class 获取随机颜色作为背景。
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);