Border for an Image View in Android

Border for an Image view in Android?

I set the below xml to the background of the Image View as Drawable. It works.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>

And then add android:background="@drawable/yourXmlFileName" to your ImageView

Android - Set a border around an ImageView

If you put the padding=1 and the background color in the LinearLayout, you'll have a 1px yellow border.

Android add border image to ImageView

You have got multiple options:

  1. Place an other View with the border (also imageview) above your Imageview. Thay might be the simples solution. Just add an other view to your xml and make sure they are overlapping. (Use for example a Relative or FrameLayout as container)

  2. Use a Layer List and draw a shape above a Bitmap and add that to your ImageView

  3. Write a custom ImageView and use the Canvas to draw the Border in the overwritten onDraw method. E.g. canvas.drawRect(...) its pretty straightforward.

How can I remove borders for imageView in Android Studio?

Instead of using android:background="@drawable/needle" for your image, use android:src="@drawable/needle". android:background="@drawable/needle" is for setting a background behind the image you want to display on your imageview. Edit: OP wants to make the line extend from left to right of the view. Changing layout_width="wrap_content" to layout_width="match_parent" should do the trick.

Scale ImageView with border in your content

you can do two things ::

1] Using 9 patch images:

  • https://romannurik.github.io/AndroidAssetStudio/nine-patches.html#&sourceDensity=320&name=example
  • https://www.nubeslogic.com/nine-patch-generator/
  • https://developer.android.com/studio/write/draw9patch.html

This will use your image & transform in 9 patch image..so that the border exceed for portion will be only inside of the view not the border.

2] Building drawble for that..
- You can use the two drawble for that and use as single unit. for that..

just like ::

For the square simply ::

name ::square.xml in the drawable folder

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/line_color" />
</shape>

For the triangle at bottom :: you can use the image for the directly

Suppose its name triangle.png,

then for final the layout ::

 <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/margin_8">

<ImageView
android:layout_gravity="bottom"
android:layout_width="@dimen/margin_20"
android:layout_height="@dimen/margin_20"
android:src="@drawable/triangle" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/square"
android:orientation="vertical"
android:padding="@dimen/margin_normal">

<TextView
android:id="@+id/tvMyMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Is it possible scale an ImageView in Android without distort the image?Is it possible scale an ImageView in Android without distort the image?Is it possible scale an ImageView in Android without distort the image?Is it possible scale an ImageView in Android without distort the image?"
android:padding="5dp"
android:gravity="start"
android:textColor="@color/black_title" />
</LinearLayout>
</LinearLayout>

Please tell if need more assistance

Android draw border in ImageView

There are two ways to achieve this:
1) add padding to the imageView and set a background color to it.

final ImageView imageView = new ImageView(context);
imageView.setPadding(2*border,2*border,0,0);
final ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(width,height);
params.leftMargin = marginYouWouldSet + border;
params.topMargin = marginYouWouldSet + border;
imageView.setBackgroundDrawable(drawable);
imageView.setBackgroundColor(borderColor);
addView(imageView, params);

2) another option is to override the draw method of your view and there draw the border:

@Override
protected void dispatchDraw(Canvas canvas)
{
borderDrawable.draw(canvas);
super.dispatchDraw(canvas);
}
...
public class BorderDrawable extends Drawable{

private Rect mBounds;
private Paint mBorderPaint;

public BorderDrawable(Rect bounds, int thickness, int color) {
mBounds = bounds;
mBorderPaint = new Paint();
mBorderPaint.setStrokeWidth(thickness);
mBorderPaint.setColor(color);
}

@Override
public void draw(Canvas canvas) {
//left border
canvas.drawLine(
mBounds.left - thickness/2,
mBounds.top,
mBounds.left - thickness/2,
mBounds.bottom,
mBorderPaint);
//top border
canvas.drawLine(
mBounds.left,
mBounds.top - thickness/2,
mBounds.right,
mBounds.top - thickness/2,
mBorderPaint);
//right border
canvas.drawLine(
mBounds.right + thickness/2,
mBounds.top,
mBounds.right + thickness/2,
mBounds.bottom,
mBorderPaint);
//bottom border
canvas.drawLine(
mBounds.left,
mBounds.bottom + thickness/2,
mBounds.right,
mBounds.bottom + thickness/2,
mBorderPaint);
}

}

Note that you are to give the middle of the line you want to draw(!) And also I haven't run, nor compiled this, so I'm not 100% sure it's correct, but these are the ways :) Rect bounds should be the bounding rect of your view - (0,0,width,height).



Related Topics



Leave a reply



Submit