derekantrican
derekantrican

Reputation: 2283

How to resize a bitmap element in android

I have an android layout like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:text="00:00"
        android:textSize="100dp"
        android:fontFamily="sans-serif-thin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />
    <TextView
        android:id="@+id/hangRest"
        android:text=""
        android:textSize="45dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/floating_shape"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"
            android:background="@drawable/start_button"
            android:elevation="30dp"
            android:layout_gravity="bottom|right" />
        <ImageView
            android:id="@+id/reset_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginBottom="15dp"
            android:src="@drawable/reset_button"
            android:elevation="30dp"
            android:layout_gravity="bottom|left" />
    </FrameLayout>
</LinearLayout>

and here is the "start_button" resource:

<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="oval">
      <size android:width="65dp" android:height="65dp"/>
      <solid android:color="#009525"/>
    </shape>
  </item>
  <item android:top="15dp" android:right="15dp" android:bottom="15dp" android:left="15dp">
    <bitmap android:src="@drawable/triangle"/>
  </item>
</layer-list>

The problem is that I want my button to be sized to the 65dp x 65dp that the oval is set to, but the bitmap doesn't seem to be cooperating with the margin I want to put in (see image below). How do I force the bitmap to allow itself to be resized (to snap to the 65dp x 65dp, less the 15dp margin)?

enter image description here

Upvotes: 0

Views: 548

Answers (2)

Gabe Sechan
Gabe Sechan

Reputation: 93559

That's not really what xml drawables are for. The idea of a Drawable is that it scales to any size. If you want an exact size image, use an ImageView and set the width. If you want an exact size image with a background, use an ImageView with a background set and use padding to set the padding around it. If need be, set an appropriate scale type, like centerInside.

Upvotes: 1

Bro
Bro

Reputation: 53

public Bitmap getBitmap(Bitmap bitmap, int newHeight, int newWidth) {

    int height = bitmap.getHeight();
    int width = bitmap.getWidth();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();
    // here we do resize the bitmap
    matrix.postScale(scaleWidth, scaleHeight);

    // and create new one
    Bitmap newBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return newBitmap;
}

Upvotes: 0

Related Questions