我试图创建一个tutorial like in zomato app,它在我测试的所有手机中工作,除了oneplus设备,在oneplus one和oneplus X中,图像未设置且imageview是透明的。我以编程方式和xml尝试设置图像。使用Android版棒棒糖在这些设备中使用高分辨率图像是否有任何问题。
我的活动:
int ht,wi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
wi = width *2/3;
ht = height * 3/5;
ImageView wireframe = (ImageView) findViewById(R.id.wireframe);
BitmapWorkerTask task = new BitmapWorkerTask(wireframe);
task.execute(R.drawable.wireframe);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// 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;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
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);
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), data, wi, ht);
return bitmap;
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:descendantFocusability="afterDescendants">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="30dp">
<ImageView
android:id="@+id/wireframe"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/wireframe"/>
</RelativeLayout>
<TextView
android:id="@+id/tutorial_skip"
android:onClick="onLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SKIP LOGIN"
android:padding="5dp"
android:layout_gravity="right"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"
android:textSize="15dp"
android:textStyle="bold"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_weight="3"
android:id="@+id/bottom_layout"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
</LinearLayout>
</LinearLayout>
我试图加载的图片: