应用程序的背景是在显示白色棒棒糖是在、棒棒糖、应用程序、白色

2023-09-07 09:40:58 作者:叶隐知心魂

我创建支持从阿比15到23这是所有手机上运行良好,包括lollipop.But当我在另外一个棒棒糖手机是一屏显示的背景为白色测试的应用程序。我试图减少的背景图像的分辨率,但没有用的。如果有人知道答案请你帮忙。

I am creating an app that supports from Api 15 to 23. It was working well on all phone including lollipop.But when I tested on another lollipop phone it is showing the background of one screen as white. I tried to reduce the resolution of the background image but of no use. If anyone knows the answer please do help.

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

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="120dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/hometop" />

<ImageView
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="75dp"
    android:background="@drawable/homelogo" />

<LinearLayout
    android:id="@+id/lL_home_search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="150dp"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/search" />

    <EditText
        android:id="@+id/eT_home_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="@android:color/transparent"
        android:hint="Search"
        android:singleLine="true"
        android:textColor="#000000"
        android:textColorHint="#000000" />

</LinearLayout>

<View
    android:id="@+id/view_search"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/lL_home_search"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:background="#eaeaea" />

<LinearLayout
    android:id="@+id/lL_home_firstlayout"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_below="@+id/view_search"
    android:layout_marginTop="20dp">

    <ImageView
        android:id="@+id/iV_home_incio"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:src="@drawable/sone" />

    <ImageView
        android:id="@+id/iV_home_ultimas"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:src="@drawable/stwo" />


</LinearLayout>

<LinearLayout
    android:id="@+id/lL_home_secondlayout"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_below="@+id/lL_home_firstlayout"
    android:layout_marginTop="30dp"

    >

    <ImageView
        android:id="@+id/iV_home_radio"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:src="@drawable/sthree"

        />

    <ImageView
        android:id="@+id/iV_home_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:src="@drawable/sfour" />


</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:layout_below="@+id/lL_home_secondlayout"
    android:layout_marginTop="20dp">

    <ImageView
        android:id="@+id/iV_home_descarga"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:src="@drawable/sfive" />

    <ImageView
        android:id="@+id/iV_home_contactio"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:src="@drawable/ssix" />


</LinearLayout>

推荐答案

使用了类调整您的高分辨率图像宽高比和内存管理。

Use the class for resize your high resolution image aspect ratio and for memory management.

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;


/**
 * A simple subclass of {@link } that resizes images from resources given a target width
 * and height. Useful for when the input images might be too large to simply load directly into
 * memory.
 */
public class ImageResizer {

    public static Bitmap decodeSampledBitmapFromFile(String filename,
                                                     int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options
                options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filename, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filename, options);
    }

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight, boolean isLow) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        if (isLow) {
            options.inPreferredConfig = Bitmap.Config.RGB_565;
        }
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }


    /**
     * Calculate an inSampleSize for use in a {@link BitmapFactory.Options} object when decoding
     * bitmaps using the decode* methods from {@link BitmapFactory}. This implementation calculates
     * the closest inSampleSize that is a power of 2 and will result in the final decoded bitmap
     * having a width and height equal to or larger than the requested width and height.
     *
     * @param options   An options object with out* params already populated (run through a decode*
     *                  method with inJustDecodeBounds==true
     * @param reqWidth  The requested width of the resulting bitmap
     * @param reqHeight The requested height of the resulting bitmap
     * @return The value to be used for inSampleSize
     */
    public static int calculateInSampleSize(BitmapFactory.Options options,
                                            int reqWidth, int reqHeight) {
        // BEGIN_INCLUDE (calculate_sample_size)
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }

            // This offers some additional logic in case the image has a strange
            // aspect ratio. For example, a panorama may have a much larger
            // width than height. In these cases the total pixels might still
            // end up being too large to fit comfortably in memory, so we should
            // be more aggressive with sample down the image (=larger inSampleSize).

            long totalPixels = width * height / inSampleSize;

            // Anything more than 2x the requested pixels we'll sample down further
            final long totalReqPixelsCap = reqWidth * reqHeight * 2;

            while (totalPixels > totalReqPixelsCap) {
                inSampleSize *= 2;
                totalPixels /= 2;
            }
        }
        return inSampleSize;
        // END_INCLUDE (calculate_sample_size)
    }
}

在您的活动做以下..

In your activity do the following..

 private Bitmap mBackground;
 private Drawable mBackgroundDrawable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.entrance);
    initComponents();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void initComponents() {
  RelativeLayout layout = (RelativeLayout) findViewById(R.id.parent);
    final Resources res = getResources();
    int[] dimensions = Util.getDisplayDimensions(this);
                mBackground = ImageResizer.decodeSampledBitmapFromResource(res, R.drawable.homeplainbgone, dimensions[0], dimensions[1], false);
    mBackgroundDrawable = new BitmapDrawable(res, mBackground);
    layout.setBackground(mBackgroundDrawable);
}

@Override
protected void onDestroy() {
   recycle();
   super.onDestroy();
}

private void recycle() {
            if (mBackground != null) {
                mBackground.recycle();
                mBackground = null;
                if (mBackgroundDrawable != null)
                    mBackgroundDrawable = null;
                 }
          }

使用下面的方法获取显示尺寸。

Use the following method for get display dimensions..

public static int[] getDisplayDimensions(Activity activity) {
        int[] dimensions = new int[2];
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        dimensions[0] = metrics.widthPixels;
        dimensions[1] = metrics.heightPixels;
        return dimensions;
    }

替换您的XML

Replace your xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent"
    >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/hometop" />

    <ImageView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:background="@drawable/homelogo" />

    <LinearLayout
        android:id="@+id/lL_home_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="150dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:src="@drawable/search" />

        <EditText
            android:id="@+id/eT_home_search"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@android:color/transparent"
            android:hint="Search"
            android:singleLine="true"
            android:textColor="#000000"
            android:textColorHint="#000000" />

    </LinearLayout>

    <View
        android:id="@+id/view_search"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/lL_home_search"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:background="#eaeaea" />

    <LinearLayout
        android:id="@+id/lL_home_firstlayout"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_below="@+id/view_search"
        android:layout_marginTop="20dp">

        <ImageView
            android:id="@+id/iV_home_incio"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:src="@drawable/sone" />

        <ImageView
            android:id="@+id/iV_home_ultimas"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:src="@drawable/stwo" />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/lL_home_secondlayout"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_below="@+id/lL_home_firstlayout"
        android:layout_marginTop="30dp"

        >

        <ImageView
            android:id="@+id/iV_home_radio"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:src="@drawable/sthree"

            />

        <ImageView
            android:id="@+id/iV_home_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:src="@drawable/sfour" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:layout_below="@+id/lL_home_secondlayout"
        android:layout_marginTop="20dp">

        <ImageView
            android:id="@+id/iV_home_descarga"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:src="@drawable/sfive" />

        <ImageView
            android:id="@+id/iV_home_contactio"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:src="@drawable/ssix" />


    </LinearLayout>