0

ゲームのヘルスバーを作成しようとしていますが、失敗しています: よろしくお願いします!

    //there a corresponding ImageView in the layout
    healthLevel = (ImageView) findViewById(R.id.healthbar);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), "res/drawable/health.png");

    //vertical bar cropped from top
    clipDrawable = new ClipDrawable(bitmapDrawable, Gravity.BOTTOM, ClipDrawable.VERTICAL);  
    healthLevel.setImageDrawable(clipDrawable);

    HelperClass.setHealth();
    clipDrawable.setLevel(10000);
    clipDrawable.invalidateSelf();

ログ:

11-12 19:32:46.272: W/BitmapDrawable(10524): BitmapDrawable cannot decode res/drawable/health.png

解決:

    //there a corresponding ImageView in the layout
    healthLevel = (ImageView) findViewById(R.id.healthbar);
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.health);

    //Convert Bitmap to Drawable
    Drawable bitmapDrawable = new BitmapDrawable(getResources(),bmp); 

    //vertical bar cropped from top
    clipDrawable = new ClipDrawable(bitmapDrawable, Gravity.BOTTOM, ClipDrawable.VERTICAL);  
    healthLevel.setImageDrawable(clipDrawable);

    HelperClass.setHealth();
    clipDrawable.setLevel(10000);
    clipDrawable.invalidateSelf();
4

1 に答える 1

1

あなたの質問に関しては、ファイル パスから を作成するには、アプリ パッケージの相対パスではなく、ファイルの絶対ストレージ パスBitmapDrawableが必要です。

次のコードを試してみましたが、動作します。

    final ImageView iv = (ImageView) findViewById(R.id.image_view);
    File file = new File("/storage/sdcard0/Download/ic_launcher.png");
    BitmapDrawable bw = new BitmapDrawable(file.getAbsolutePath());
    iv.setImageDrawable(bw);

Bitmapただし、必要なのは、で作成できるのようなものだと思います

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

このチュートリアルこのリンクが役立つ場合があります。

于 2013-11-13T02:29:17.493 に答える