简体   繁体   中英

Bitmap drawable resource being clipped

I've assigned a drawable resource to my app's loading theme. However, the bitmap within the drawable resource is being clipped and I can't figure out why. Using android:gravity="fill_horizontal" stops the horizontal clipping, but also changes the image's aspect ratio.

How can I use the image without clipping the edges and while maintaining its original aspect ratio?

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- Launcher/splash theme. -->
    <style name="AppTheme.Launcher">
        <item name="android:windowBackground">@drawable/launch_screen</item>
        <item name="android:adjustViewBounds">true</item>
    </style>

</resources>

launch_screen.xml

<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">

    <!-- Background color-->
    <item android:drawable="@android:color/white"/>

    <!-- Splash Logo -->
    <item>
        <bitmap
            android:src="@drawable/splash_screen"
            android:gravity="center"/>
    </item>
</layer-list>

Original Bitmap

Bitmap using---android:gravity="center"

Bitmap using---android:gravity="fill_horizontal"

Temporary Solution: I've found an imperfect solution. By tweaking the item's height and width I've managed ensure the image remains within the container's boundaries. The image does not scale equally across different screen sizes. For now, this setting is the best solution I've come across.

Amended launch_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Use android:opacity=”opaque” to prevent black flash during theme 
transition. -->

<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">

<!-- Background color-->
<item android:drawable="@android:color/white"/>

<!-- Splash Logo -->
<item
    android:height="400dp"
    android:width="400dp"
    android:gravity="center">
    <bitmap
        android:src="@drawable/splash_screen"
        />
</item>

</layer-list>

Amended launch_screen.xml

Change your launch_screen.xml with this

<?xml version="1.0" encoding="utf-8"?>
<layer-list 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:opacity="opaque">

    <item android:width="220dp" android:height="220dp">
        <bitmap
            android:src="@drawable/splash_screen"
            android:gravity="center"/>
    </item>
</layer-list>

Edit: I updated the code, try this.

In your AndroidStudioProjects folder, usually located C:\\Users\\username\\AndroidStudioProjects you will find drawable dimension folders .

Dimensions

 - LDPI:
    - Portrait: 200x320px
    - Landscape: 320x200px
 - MDPI:
    - Portrait: 320x480px
    - Landscape: 480x320px
 - HDPI:
    - Portrait: 480x800px
    - Landscape: 800x480px
 - XHDPI:
    - Portrait: 720px1280px
    - Landscape: 1280x720px
 - XXHDPI
    - Portrait: 960x1600px
    - Landscape: 1600x960px
 - XXXHDPI 
    - Portrait: 1280x1920px
    - Landscape: 1920x1280px

Resize your original image with following dimensions and put them in the right folder. I also see that your image is a logo with a white background so you can just make your logo transparent in an image editor like photoshop.

Then create a drawable in your drawable folder

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/color1"/> <-- this is the background color in your splashscreen.

    <item>
        <bitmap
            android:gravity="top"
            android:src="@drawable/splashlogo"/> <-- your transparent logo.
    </item>

</layer-list>

Then create Style in your values folder -> styles.xml

 <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_screen</item>
 </style>

And finally add the theme style to your manifest

<activity
      android:name=".SplashActivity"
      android:theme="@style/SplashTheme"  <-- your splashscreen theme style.
      android:label="@string/app_name"
      android:screenOrientation="portrait">
          <intent-filter>
             <action android:name="android.intent.action.MAIN" />

             <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
</activity>

PS: make sure you created an Activity for your Splashscreen and in onCreate start an intent to open your MainActivity.

@Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
     startActivity(mainIntent);
     finish();      
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM