ListViews are, generally, pretty boring. If we’re displaying some kind of image-based data, we can bind ImageViews into them – and to make them slightly prettier, round off the top/bottom edges of the list. This works especially well with subheaded-lists.

To do this, I grabbed some code from http://www.ruibm.com/?p=184 that rounds all edges of a bitmap and modified it.

public Bitmap getRoundedCornerBitmap(Bitmap bitmap) 
{
	final int color = 0xff424242;
	final Paint paint = new Paint();
	final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
	Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
	Canvas canvas = new Canvas(output);

	// the float array passed to this function defines the x/y values of the corners
	// it starts top-left and works clockwise
	// so top-left-x, top-left-y, top-right-x etc
	// a curve of 10.0f will typically work pretty well for a larger image
	RoundRectShape rrs = new RoundRectShape(
			new float[] {0, 0, 0, 0, 0, 0, 0, 0}, 
						null, null);
	
	canvas.drawARGB(0, 0, 0, 0);
	paint.setAntiAlias(true);
	paint.setColor(color);
	rrs.resize(bitmap.getWidth(), bitmap.getHeight());
	rrs.draw(canvas, paint);
	
	paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
	canvas.drawBitmap(bitmap, rect, rect, paint);
	return output;
}

With this, we can do something like the below.

private List<something> mData;

public SomeAdapter(...)
{
	super(...);
	mData = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
	ViewHolder holder;
	
	if (convertView == null)
	{
		// ...
	}
	else
	{
		// ...
	}
	
	// get Bitmap b from a file or somewhere (it's our image for this list item)
	
	// i modified the function again to be 0 = top edges rounded
	if (position == 0)
		b = getRoundedCornerBitmap(b, 0);

	// no else, our item could be the only one in its list and hence may need rounding on both edges

	if (position == mData.size() - 1)
		b = getRoundedCornerBitmap(b, 1); // and 1 is bottom
Advertisements