Android

How to set a border to an imageview

tomato13 2013. 7. 3. 13:39

http://stackoverflow.com/questions/10975710/how-to-set-a-border-to-an-imageview


   setContentView(R.layout.activity_main);

   ImageView image = (ImageView) findViewById(R.id.imageview);

   Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);


   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);   


or


   ImageView image = (ImageView) findViewById(R.id.imageview);

   Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);


   final int BORDER_WIDTH = 3;

   final int BORDER_COLOR = Color.BLUE;

Bitmap res = Bitmap.createBitmap(bMap.getWidth(),

                                    bMap.getHeight(),

                                    bMap.getConfig());

   Canvas c = new Canvas(res);

   Paint p = new Paint(Paint.FILTER_BITMAP_FLAG);

   c.drawBitmap(bMap, BORDER_WIDTH, BORDER_WIDTH, p);

   p = new Paint();

   p.setColor(BORDER_COLOR);

   p.setStrokeWidth(10);

   //c.drawRect(0, 0, res.getWidth(), res.getHeight(), p);

   c.drawLine(0, 0, bMap.getWidth(), 0, p);

   c.drawLine(0, 0, 0, bMap.getHeight(), p);

   c.drawLine(bMap.getWidth(), 0, bMap.getWidth(), bMap.getHeight(), p);

   c.drawLine(0, bMap.getHeight(), bMap.getWidth(), bMap.getHeight(), p);


   Matrix mat = new Matrix();

   //mat.postRotate(350);

   Bitmap bMapRotate = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), mat, true);

   image.setImageBitmap(bMapRotate);