0

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

derekantrican
  • 1,891
  • 3
  • 27
  • 57

2 Answers2

1

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • If I change the bitmap element to an ImageView, I get an "Error Inflating Class" error – derekantrican Mar 05 '17 at 20:56
  • You don't do it in the xml drawable. You change whatever overall layout you're using to use an ImageView instead. Do you understand what the difference between a view and a drawable is? – Gabe Sechan Mar 05 '17 at 20:57
  • Sorry, I'm really new to this. So I'm going to guess no: not really. I'll update my question with the rest of my layout – derekantrican Mar 05 '17 at 21:01
  • So a View is a graphical element on screen. It can respond to user input and had a fixed location and size (once laid out). It is part of a view hierarchy belonging to your activity. A Drawable is something that can be drawn to the screen at any location, and will stretch to fill the size requested. It does not belong to any location, and does not have a fixed position or size. Its used by Views to hold refreshable, resizable items to draw to screen like icons, backgrounds, etc. – Gabe Sechan Mar 05 '17 at 21:04
  • I was about to edit and make a suggestion on how to do it, but looking at your code, you're doing things the hard way. Use a FloatingActionButton instead. See https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html – Gabe Sechan Mar 05 '17 at 21:06
0
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;
}
Bro
  • 53
  • 6