我正在创建一个支持从Api 15到23的应用程序。它在包括棒棒糖在内的所有手机上运行良好。但是当我在另一个棒棒糖手机上测试时,它显示一个屏幕的背景为白色。我试图降低背景图像的分辨率但没有用。如果有人知道答案,请帮忙。
<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>
答案 0 :(得分: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>