Add a Background Image to Shape in Xml Android

Add a background image to shape in XML Android

Use following layerlist:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
<item android:drawable="@drawable/image_name_here" />
</layer-list>

How to set background image to shape?

I came across with this library. RoundedImageView Github link

All I needed to do was:

<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:src="@drawable/add_card_head_bg"
android:scaleType="centerCrop"
app:riv_corner_radius="90dip"
app:riv_mutate_background="true"
app:riv_corner_radius_bottom_left="50dp"/>

How to add a image to drawable xml in android

You can use a layer list like this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid
android:color="#666666"/>

<size
android:width="120dp"
android:height="120dp"/>
</shape>
</item>

<item android:drawable="@drawable/your_drawable"/>
</layer-list>

How can I add an Image in my custom shape xml

You can do it by using layer-list for ex:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#80000000"
android:startColor="#80000000" />
</shape>
</item>
<item>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:tileMode="repeat" android:gravity="center" />
</item>
</layer-list>

Save the above as @drawable/image_state2.

For more info:
Resize bitmap inside a drawable layer-list

How to fix background image with drawable xml file on Android?

The drawable will expand to fit your container. Notice that I didn't modify the bottom inset (android:bottom) so that the curve remains flush with the bottom of your container. You can modify the left, right, top and bottom attributes to get your desired curve.

rounded_header.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="-100dp"
android:left="-400dp"
android:right="-400dp">
<shape android:shape="oval">
<gradient
android:startColor="#F07B26"
android:endColor="#F8BA4C"/>
</shape>
</item>
</layer-list>

layout.xml

<View
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/rounded_header"/>

Result

Sample Image

Background image with rounded bottom in XML android

I got a solution and I did it this in the following way:
I am writing a part of my onCreate method

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
topbackground=(ImageView) findViewById(R.id.topbackground);
Bitmap image1 = BitmapFactory.decodeResource(getResources(),R.drawable.login_bg)
topbackground.setImageBitmap(roundedImage.getRoundedCornerBitmap(this,
image1,200,image1.getWidth(),image1.getHeight(),true,true,false,false ));
}
RoundedImage roundedImage = new RoundedImage();

There is an image with the name "login_bg" , "topbackground" is the view of Image and I am calling a separate class named "RoundedImage" and performing this by passing its parameters and my class RoundedImage is as follows:

 public class RoundedImage  {

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int
pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL,
boolean squareBR ) {

Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final float densityMultiplier =
context.getResources().getDisplayMetrics().density;

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);

//make sure that our rounded corner is scaled appropriately
final float roundPx = pixels*densityMultiplier;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

//draw rectangles over the corners we want to be square
if (squareTL ){
canvas.drawRect(0, 0, w/2, h/2, paint);
}
if (squareTR ){
canvas.drawRect(w/2, 0, w, h/2, paint);
}
if (squareBL ){
canvas.drawRect(0, h/2, w/2, h, paint);
}
if (squareBR ){
canvas.drawRect(w/2, h/2, w, h, paint);
}

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(input, 0,0, paint);

return output;
}
}

This is how it goes and I am able to get what was needed.

Change background image but keep drawable to a view android xml

Solution

Step 1. Go to res/values folder, create a xml file named attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundedImageView">
<attr name="topLeftCorner" format="dimension" />
<attr name="topRightCorner" format="dimension" />
<attr name="bottomRightCorner" format="dimension" />
<attr name="bottomLeftCorner" format="dimension" />
</declare-styleable>
</resources>

Step 2. Create a class that extends from AppCompatImageView, named RoundedImageView

public class RoundedImageView extends AppCompatImageView {

private final Path path = new Path();
private final float[] radii = new float[8];
private final RectF rect = new RectF();

public RoundedImageView(@NonNull Context context) {
this(context, null);
}

public RoundedImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedImageView);
try {
int topLeftCorner = a.getDimensionPixelSize(R.styleable.RoundedImageView_topLeftCorner, 0);
int topRightCorner = a.getDimensionPixelSize(R.styleable.RoundedImageView_topRightCorner, 0);
int bottomRightCorner = a.getDimensionPixelSize(R.styleable.RoundedImageView_bottomRightCorner, 0);
int bottomLeftCorner = a.getDimensionPixelSize(R.styleable.RoundedImageView_bottomLeftCorner, 0);
radii[0] = topLeftCorner;
radii[1] = topLeftCorner;
radii[2] = topRightCorner;
radii[3] = topRightCorner;
radii[4] = bottomRightCorner;
radii[5] = bottomRightCorner;
radii[6] = bottomLeftCorner;
radii[7] = bottomLeftCorner;
} finally {
a.recycle();
}
}

@Override
protected void onDraw(Canvas canvas) {
rect.left = 0;
rect.top = 0;
rect.right = getWidth();
rect.bottom = getHeight();
path.rewind();
path.addRoundRect(rect, radii, Path.Direction.CW);
canvas.clipPath(path);
super.onDraw(canvas);
}
}

Step 3. Use it from layout file.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F00"
android:gravity="center">

<com.example.roundedimageview.RoundedImageView
android:id="@+id/imageView"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/ic_android"
app:bottomRightCorner="20dp"
app:topLeftCorner="20dp" />
</FrameLayout>

Result

Sample Image

Benefit

  • You can set the corner radius for top-left, top-right, bottom-right, bottom-left.

  • Work with Glide, Picasso library

Limitations

  • The ScaleType is either CENTER_CROP or FIT_XY


Related Topics



Leave a reply



Submit