1

こんにちは、私はアンドロイドプログラミングが初めてなので、知りたいです:

すべての画面サイズ (idpi、mdpi、hdpi、xhdpi、xxhdpi) と互換性のあるドローアブルを使用する方法

実際、すべての画面サイズとのドローアブル画面サイズの互換性について混乱しています

だから私はそれを知りたい:

アプリにすべての画面サイズとの互換性を持たせたい場合、すべての描画可能なフォルダーに画像を配置する必要がありますか?

例: 背景画像

  • • drawable-ldpi (QVGA Device 2.7 の場合は 240x320) この解像度で画像を配置
  • • drawable-mdpi (HVGA Device 3.2 の場合は 320x480) この解像度で画像を配置
  • • drawable-hdpi (WVGA デバイス 4.0 の場合は 480x800) この解像度で画像を配置
  • • drawable-xhdpi (WxVGA デバイス 4.7 の場合は 1280x720) この解像度で画像を配置します
  • • drawable-xxhdpi (Nexus 5 デバイスの場合は 1080x1920) この解像度で画像を配置

または、すべての画面サイズに 1 つの同じ画像を使用できます。

4

5 に答える 5

2

すべての dpi にドローアブルを提供する場合にのみ、画像はシャープで鮮明に見えます。Ldpi はサポートされなくなったため、ldpi を無視できます。

また、ランチャー アイコン用に、xxxhdpi イメージを提供します。フル HD 画面の Kit Kat で実行されているタブレットは、ランチャーでこれらの画像を使用します。

1 つの drawable を作成してdrawableフォルダーに入れるのは悪い習慣です! 画像がぼやけて見えます。

于 2014-03-20T10:51:19.100 に答える
1

私は、この状況が魔法のように機能するための非常に優れた方法を持っています。hd イメージを 1 つ作成するだけで、すべての方法で解決できます。

メソッドは次のとおりです。

/**
 * Get DRAWABLE with original size screen resolution independent
 * @param is Input stream of the drawable
 * @param fileName File name of the drawable
 * @param density Density value of the drawable
 * @param context Current application context
 * @return Drawable rearranged with its original values for all
 * different types of resolutions.
 */
public static Drawable getDrawable(InputStream is, String fileName, int density, Context context) {
    Options opts = new BitmapFactory.Options();
    opts.inDensity = density;
    opts.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
    return Drawable.createFromResourceStream(context.getResources(), null, is, fileName, opts);
}

ここでは、画像ファイルの入力ストリームを準備し、画面のどの使用領域でも適切な密度を設定する必要があります。密度が小さいほど品質が低くなります。値を変更して解決します。メソッドの使用例を次に示します。

1) アセット フォルダーからアセットを開きます。

getDrawable(assetManager.open("image.png"), "any_title", 250, context)

2) drawables フォルダーからドローアブルを開きます: ここではまず、入力ストリームに次のメソッドを提供する必要があります: a) メソッド:

/**
 * Get InputStream from a drawable
 * @param context Current application context
 * @param drawableId Id of the file inside drawable folder
 * @return InputStream of the given drawable
 */
public static ByteArrayInputStream getDrawableAsInputStream(Context context, int drawableId) {
    Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(drawableId)).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] imageInByte = stream.toByteArray();
    return new ByteArrayInputStream(imageInByte);
}

および使用法: b) 使用法:

getDrawable(getDrawableAsInputStream(getBaseContext(), R.drawable.a_drawable), "any_title", 250, context)

役に立つことを願っています。

于 2015-03-09T08:39:35.190 に答える