How to set a border to an imageview
I am working in an android application and I want to place an ImageView in a particular position in my view. For that I applied the particular code and worked successfully :
ImageView image = (ImageView) findViewById(R.id.imageView1); Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.mrng); Matrix mat = new Matrix(); mat.postRotate(350); Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(),bMap.getHeight(), mat, true); image.setImageBitmap(bMapRotate);
Now I want to set a border to this image. For that I made a an shape xml and I have set as the background of the image view.
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <stroke android:width="3dp" android:color="#000000" /> </shape>
But when I gave the background of the image view the shape it does not give the correct output.please help me.
I do not want a border around the ImageView, but rather border around the rotated image.
EDIT: I'm completely rewriting my answer based on the clarification of the question. Here's how I achieved what you want. The idea is to draw the frame and then rotate:
ImageView image = (ImageView) findViewById(R.id.TestImage); Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.image); final int BORDER_WIDTH = 3; final int BORDER_COLOR = Color.BLACK; Bitmap res = Bitmap.createBitmap(bMap.getWidth() + 2 * BORDER_WIDTH, bMap.getHeight() + 2 * BORDER_WIDTH, bMap.getConfig()); Canvas c = new Canvas(res); Paint p = new Paint(); p.setColor(BORDER_COLOR); c.drawRect(0, 0, res.getWidth(), res.getHeight(), p); p = new Paint(Paint.FILTER_BITMAP_FLAG); c.drawBitmap(bMap, BORDER_WIDTH, BORDER_WIDTH, p); Matrix mat = new Matrix(); mat.postRotate(350); Bitmap bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), mat, true); image.setImageBitmap(bMapRotate);
And here's the screenshot of the result:
There is way to put border around ImageView even any view in android. In XML put Four simple <View>'s on each side of the imageview. And these view's can be assigned any type of style. You can use relative layout in which you can place four borders easily around image view.
Kinda lengthy process but works.