Introduction
In couple of previous blog i was explained about custom listview with images,that all images positioned in normal rectangular shape,now i am going to explain custom list view with rounded circle images
demo
Using the code
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp">
<TextView
android:id="@+id/title"
android:textColor="#000"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageview"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher"
android:layout_marginRight="10dp"
android:contentDescription="@string/app_name"/>
</LinearLayout>
MainActivity.java:
package com.example.list;
import java.util.ArrayList;
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends ListActivity
{
{
private String[] listview_names = {"India","Bangladesh", "China","Indonesia" };
static Context mcontext;
private static int[] listview_images =
{
R.drawable.india,R.drawable.bangladesh,R.drawable.china,
R.drawable.indonesia};
{
R.drawable.india,R.drawable.bangladesh,R.drawable.china,
R.drawable.indonesia};
private ListView lv;
private static ArrayList<String> array_sort;
private static ArrayList<Integer> image_sort;
@Override
protected void onCreate(Bundle savedInstanceState)
{
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(android.R.id.list);
array_sort=new ArrayList<String> (Arrays.asList(listview_names));
image_sort=new ArrayList<Integer>();
for (int index = 0; index < listview_images.length; index++)
{
image_sort.add(listview_images[index]);
}
setListAdapter(new bsAdapter(this));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
Toast.makeText(getApplicationContext(), array_sort.get(position),
Toast.LENGTH_SHORT).show();
}
});
}
public static class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
// TODO Auto-generated constructor stub
this.cntx=context;
}
public int getCount()
{
// TODO Auto-generated method stub
return array_sort.size();
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return array_sort.get(position);
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return array_sort.size();
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.list_item, null);
TextView tv = (TextView) row.findViewById(R.id.title);
ImageView im = (ImageView) row.findViewById(R.id.imageview);
tv.setText(array_sort.get(position));
im.setImageBitmap(getRoundedShape(decodeFile(cntx, listview_images[pos ition]),200));
return row;
}
public static Bitmap decodeFile(Context context,int resId) {
try {
// decode image size
mcontext=context;
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeResource(mcontext.getResources(), resId, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 200;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true)
{
{
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeResource(mcontext.getResources(), resId, o2);
} catch (Exception e) {
}
return null;
}
}
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage,int width) {
// TODO Auto-generated method stub
int targetWidth = width;
int targetHeight = width;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth,
targetHeight), null);
return targetBitmap;
}
}
21 comments
Write commentsI do not even know how I ended up here, but I thought this post was good.
ReplyI do not know who you are but definitely you're going to a famous blogger
if you are not already ;) Cheers!
Feel free to visit my site ... christian louboutin cheap
How do I handle "listview_images[pos ition]),200" if I don´t use array? I get images by json/url and it is List
ReplyHi madson,
ReplyHow do you get and store json/url values.
Thanks for d info
Replythx sir, i always wanted to make a custom list wid the rounded images as i m newbe....
ReplyThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
Replyandroid development course fees in chennai | android app development training in chennai|Android Training institute in chennai with placement | Best Android Training in velachery
I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.
ReplyClick here:
Microsoft azure training in sollinganallur
Click here:
Microsoft azure training in btm
I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
ReplyClick here:
angularjs4 Training in Chennai
Click here:
angularjs5 Training in Chennai
I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this
ReplyClick here:
angularjs training in sholinganallur
Click here:
angularjs training in btm
Click here:
angularjs training in rajajinagar
Click here:
angularjs training in marathahalli
Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up.
ReplyBlueprism training in annanagar
Blueprism training in velachery
Blueprism training in marathahalli
AWS Training in chennai
AWS Training in bangalore
Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
ReplyIf you are looking for any Big data Hadoop Related information please visit our website hadoop classes in pune page!
Its great post.Very easy to understand,got lot of information.Thanks for posting,keep updating.IT managers suggest to take Hadoop Admin Training in Bangalore because its best suited for them who have basic linux experience.
ReplyIts very amazing an helpful data..
ReplyThanks for sharing with us,
We are again come on your website,
Thanks and good day,
Please visit our site,
buylogo
this is most informative one...
ReplyAngularJS Training in Chennai | AngularJS Training in Anna Nagar | AngularJS Training in OMR | AngularJS Training in Porur | AngularJS Training in Tambaram | AngularJS Training in Velachery
I like it and help me to development very well. Thank you for this brief explanation and very nice information. Well, got a good knowledge.
ReplyAngular js Training in Chennai
Angular js Training in Velachery
Angular js Training in Tambaram
Angular js Training in Porur
Angular js Training in Omr
Angular js Training in Annanagar
Wonderful Blog!! Indeed it is very interesting learn all the new things around. I have been wanting to improve my skills and this blog really helps! Indeed it is an amazing blog. loved it. Appreciate all your work
ReplySelenium Training in Chennai
Selenium Training in Velachery
Selenium Training in Tambaram
Selenium Training in Porur
Selenium Training in Omr
Selenium Training in Annanagar
Its very amazing an helpful data.
Replyamazon web services aws training in chennai
microsoft azure course in chennai
workday course in chennai
android course in chennai
ios course in chennai
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Replytally training in chennai
hadoop training in chennai
sap training in chennai
oracle training in chennai
angular js training in chennai
Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.
ReplyJava training in chennai
python training in chennai
web designing and development training course in chennai
selenium training in chennai
digital-marketing seo training in chennai
Its very amazing an helpful data..
ReplyThanks for sharing with us,
We are again come on your website,
Thanks and good day,
IELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
Spoken english classes in chennai | Communication training
This was a very informative article, indeed loved to read and clear my doubts. Keep us posted a lot more blogs. Also check out our blog pages too.
Replydata science training in chennai
ccna training in chennai
iot training in chennai
cyber security training in chennai
ethical hacking training in chennai
EmoticonEmoticon