画面幅の 80%の画像をコードで作った!
結局はコードで画像を画面幅の 80%に縮小して ImageView#setImageBitmap で表示するようにした。
layout.xml はこんな感じ。
<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" tools:context=".MainActivity" > <ImageView android:id="@+id/listViewSplashImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:contentDescription="@string/splash_image" android:scaleType="centerInside" android:src="@drawable/splash_image" /> </RelativeLayout>
真ん中に画像を表示してるだけ。
80%だなんだは関係なし。
で、コードの方はこんな感じ。
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.splash_image); Bitmap b2 = resizeBitmapToDisplaySize(this, b, (float) 0.8); ImageView iv = (ImageView) findViewById(R.id.listViewSplashImage); iv.setImageBitmap(b2); Bitmap resizeBitmapToDisplaySize(Activity activity, Bitmap src, float ratio) { int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); Matrix matrix = new Matrix(); DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); float screenWidth = (float) metrics.widthPixels; float screenHeight = (float) metrics.heightPixels; float targetWidth = screenWidth * ratio; float widthScale = targetWidth / srcWidth; matrix.postScale(widthScale, widthScale); Bitmap dst = Bitmap.createBitmap(src, 0, 0, srcWidth, srcHeight, matrix, true); src = null; return dst; }
画像をリサイズするコードは、ここから拝借してちょっと変更した。
というわけで、無事スプラッシュスクリーンは横幅 80%のサイズでロゴが表示されることになったのだけど、なんで 2.2 の頃は大丈夫だったものが 4.2.2 でダメになったんだろう…?