Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control Aspect Ratio of Theme Background (Android)

I have set up a loading screen (splash) for my app by following some advice that explains the "proper way" to do it:

styles.xml:

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

manifest:

<activity
        android:name="com.tba.versionc.SplashActivity"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

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

java:

public class SplashActivity extends AppCompatActivity {

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

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

When I designed the vector drawable for the splash screen, I set its aspect ratio to match my phone (16:9), and it seems to work fine, but I am concerned about what will happen to the aspect ratio when running on devices with screen ratios that are different.

To eliminate the chance that it will "stretch to fit" the wrong size screen, I would prefer if it was center cropped, but since it is not an ImageView, or even part of a layout file, I don't know of any way set the center crop attribute.

Anyone?

like image 622
Nerdy Bunz Avatar asked Jun 13 '16 08:06

Nerdy Bunz


People also ask

How to scale an image in imageview to keep the aspect ratio?

This example demonstrates how to scale an Image in ImageView to keep the aspect ratio in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

What is the aspect ratio?

Aspect ratio is the ratio of width to height. 16:9 standard aspect ratio. 4:3 standard aspect ratio. 16:9 standard aspect ratio. Constant Value: 1 (0x00000001) 4:3 standard aspect ratio.

How to make your Android app responsive and adaptive?

Android devices come in all shapes and sizes. As a result, your app's layout needs to be responsive and adaptive. Instead of defining the layout with static dimensions that assume a certain screen size and aspect ratio, design your app to gracefully accommodate different screen sizes and orientations.

How to resize an image proportionally in HTML?

You might think it should be easy: sadly, it's not. Achieving this result in HTML would be extremely easy: we just need to set our image width to 100% and avoid to set any height, letting the browser doing the whole proportional resize job.


Video Answer


1 Answers

See this guide by Malcolm Hollingsworth.

Basically, you have to modify your image in a specific way so that Android can know how you want it to change the image when the aspect ratio doesn't match the device's.

Another resource is here

I hope this helps!

like image 51
user31415 Avatar answered Jan 05 '23 02:01

user31415