2015-10-06 2 views
0

Я создаю приложение, которое поддерживает от Api с 15 до 23. Это хорошо работает на всех телефонах, включая lollipop. Но когда я тестировал на другом телефоне с леденец, он показывает фон одного экран белый. Я попытался уменьшить разрешение фонового изображения, но бесполезно. Если кто-нибудь знает ответ, пожалуйста, помогите.Фон приложения показывает белый в lollipop

<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> 

+1

Plzzzzzzzzzzzzzzzzzz. .post ur code ... –

+1

ты используя изображение в фоновом режиме? Некоторые снимки экрана или код будут полезны. – Shahzeb

+0

Это мой xml-код –

ответ

0

Используйте класс для изменения размера с высоким разрешением соотношения сторон изображения и для управления памятью.

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) 
    } 
} 

В вашей деятельности сделайте следующее ..

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; 
       } 
      } 

Используйте следующий метод для размеров дисплея ПОЛУЧИТЬ ..

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

<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> 
+0

спасибо anoop для ur help..это попробую это –

+0

этот код показывает из памяти исключение. Я не знаю, почему –

+0

Кажется, ур тоже много images.it's oki, если они не слишком большие. Теперь попробуйте не помещать 'android: background =" @ drawable/homeplainbgone "в xml, так как мы делаем это программно .. –

Смежные вопросы