Android: create a thumbnail of contacts from initials of names
I have an Android ListView
that contains multiple emails. Some of them have pin thumbnails, while some of them don't. For unknown contact thumbnails, I want to create a bitmap containing the initials of the corresponding email id. How can I achieve this? I've seen some Android apps using this method to display contact thumbnails like this:
First, I use the following code:
ImageView iv=(ImageView)findViewById(R.id.imageView1);
TextView view = (TextView)findViewById(R.id.textView1);
Bitmap b=Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
view.draw(c);
iv.setImageBitmap(b);
Where TextView contains a simple string. Unfortunately, this won't work. Can someone please give me some pointers? Many thanks for your help!
source to share
Well, you're on the right track. Try something like this.
ImageView iv=(ImageView)findViewById(R.id.imageView1);
Bitmap b=Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawText(initials,x,y,paint);
iv.setImageBitmap(b);
You must create your own logic to center the text. You can use some of the following Paint.getTextSize () helper methods to find out how much space is required for the text.
source to share
Just using this library you can try: D
https://github.com/fahmisdk6/AvatarView
this code in your XML file
<me.fahmisdk6.avatarview.AvatarView
android:id="@+id/avatar_user5"
android:layout_width="48dp"
android:layout_height="48dp"
app:avCornerRadius="8dp"
app:avBgTextColor="@color/colorAccent"
app:avTextColor="@color/colorPrimary"
app:avTextSize="10sp"
app:avFont="font/avenir_black.ttf"/>
And this is in the Java file
AvatarView avatarView5 = (AvatarView) findViewById(R.id.avatar_user5);
avatarView5.bind("Fahmi Sidik", "https://avatars2.githubusercontent.com/u/10940190?v=3&s=460");
source to share
Check, this answer will definitely solve your problem fooobar.com/questions/17330532 / ...
source to share