androidxml

Can I use android:width, android:height and android:viewportWidth, android:viewportHeight when I want to draw a png bitmap?


I created the following xml file from an .svg vector file in Android Studio:

ic_cercle.xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="288dp"
    android:height="288dp"
    android:viewportWidth="76.2"
    android:viewportHeight="76.2">
  <path
      android:pathData="M38.1,38.1m-25.1354,0a25.1354,25.1354 0,1 1,50.2708 0a25.1354,25.1354 0,1 1,-50.2708 0"
      android:strokeWidth="0.529167"
      android:fillColor="#ff0000"
      android:strokeColor="#000000"/>
</vector>

Then, I use it here in my project:

ic_myimage.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="@color/color_background" />
    <item
        android:drawable="@drawable/ic_cercle"
        android:gravity="center"/>
</layer-list>

Now, I want to draw the same image as bitmap image instead of vector. I have added a 288x288dp png file(ic_cercle_png) to my project.

How can I draw a 288x288dp bitmap image instead of the vector image?

I have created this xml file but I don't know if I can add android:width="288dp", android:height="288dp", android:viewportWidth="76.2" and android:viewportHeight="76.2" to ic_myimage_png.xml.

ic_myimage_png.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="@color/color_background"/>
  </item>
  <item>
    <bitmap
        android:src="@drawable/ic_cercle_png"
        android:tileMode="disabled"
        android:gravity="center"/>
  </item>
</layer-list>

Can I use android:width, android:height and android:viewportWidth, android:viewportHeight when I want to draw a bitmap in the center of the screen?

What is android:viewportWidth and android:viewportHeight? Should I use them when I draw a bitmap image?


Solution

  • When working with bitmap pictures in the element, you do not require the android:width, android:height, android:viewportWidth, and android:viewportHeight properties.

    Android:viewportWidth and android:viewportHeight are used to scale and position vector graphics in vector drawables by defining the vector drawable's coordinate system. Bitmap pictures are not compatible with these properties.

    The android:gravity="center" attribute that you have in your ic_myimage_png.xml file should be adequate if your aim is to center the bitmap image. If necessary, change the bitmap's size in the ic_cercle_png source file.