Full screen background image in an activity

2019-01-05 06:50发布

I see many applications that use a full-screen image as background. This is an example:

Full screen background image

I want to use this in a project, the best way I've found so far to do this is to use an image with a large size, put it in a ImageView and use android: adjustViewBounds="true" to adjust the margins

The problem is that if a screen with a very high resolution, the image falls short.

Another option I thought of is to use the image in a FrameLayout, with match_parent in width and height as background... this stretches the image, but I think the result is not very good.

How would you do it?

13条回答
Viruses.
2楼-- · 2019-01-05 07:38

You should put the various size images into the followings folder

for more detail visit this link

  • ldpi

  • mdpi

  • hdpi

  • xhdpi

  • xxhdpi

and use RelativeLayout or LinearLayout background instead of using ImageView as follwoing example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:background="@drawable/your_image">

</RelativeLayout>
查看更多
神经病院院长
3楼-- · 2019-01-05 07:40

Another option is to add a single image (not necessarily big) in the drawables (let's name it backgroung.jpg), create an ImageView iv_background at the root of your xml without a "src" attribute. Then in the onCreate method of the corresponding activity:

    /* create a full screen window */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.your_activity);

    /* adapt the image to the size of the display */
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
      getResources(),R.drawable.background),size.x,size.y,true);

    /* fill the background ImageView with the resized image */
    ImageView iv_background = (ImageView) findViewById(R.id.iv_background);
    iv_background.setImageBitmap(bmp);

No cropping, no many different sized images. Hope it helps!

查看更多
爷的心禁止访问
4楼-- · 2019-01-05 07:40

If you have bg.png as your background image then simply:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"/>
</RelativeLayout>
查看更多
Juvenile、少年°
5楼-- · 2019-01-05 07:46

three step for put background

1)you should choose your like picture. for example :enter image description here

2)Then you copy this picture in drawable. warning: you should choose types short for name picture.

enter image description here

3)you go to page xml Intended and write :

android:background="id picture" for example my picture id is @drawable/download.

enter image description here

finish.

查看更多
混吃等死
6楼-- · 2019-01-05 07:50

What about

android:background="@drawable/your_image"

on the main layout of your activity?

This way you can also have different images for different screen densities by placing them in the appropriate res/drawable-**dpi folders.

查看更多
三岁会撩人
7楼-- · 2019-01-05 07:50

If you want your image to show BEHIND a transparent Action Bar, put the following into your Theme's style definition:

<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>

Enjoy!

查看更多
登录 后发表回答