2

実行時に NinePatchDrawable オブジェクトを作成する必要があります。これは、外部の .png 画像をサーバーから受信し、ボタンの背景 (たとえば) に 9 パッチとして適用する必要があるということです。

NinePatchDrawable オブジェクトを作成しようとしましたが、コンストラクターがパッチを記述する "byte[] チャンク" を求めてきました。問題は、9patch 情報を含まないビットマップからこのチャンクを構築する方法がわかりません。

このトピックに関するアイデアはありますか? 問題を間違った視点から見ていませんか?

4

2 に答える 2

2

実行時に NinePatch/NinePatchDrawable を作成するための私の回答を参照してください

(コンパイルされていない) NinePatch ビットマップから NinePatchDrawable を作成するツールを開発しています。https://gist.github.com/knight9999/86bec38071a9e0a781eeを参照してください。

メソッド NinePatchDrawable createNinePatchDrawable(Resources res, Bitmap bitmap) が役立ちます。

例えば、

    ImageView imageView = (ImageView) findViewById(R.id.imageview);
    Bitmap bitmap = loadBitmapAsset("my_nine_patch_image.9.png", this);
    NinePatchDrawable drawable = NinePatchBitmapFactory.createNinePatchDrawable(getResources(), bitmap);
    imageView.setBackground( drawable );

どこ

public static final Bitmap loadBitmapAsset(String fileName,Context context) {
    final AssetManager assetManager = context.getAssets();
    BufferedInputStream bis = null;
    try {
        bis = new BufferedInputStream(assetManager.open(fileName));
        return BitmapFactory.decodeStream(bis);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (Exception e) {

        }
    }
    return null;
}

この場合、my_nine_patch_image.9.pngは assets ディレクトリの下にあります。

于 2015-04-23T13:12:34.987 に答える
1

質問 5519768 でこれに回答しました。「ソース」と「コンパイル済み」の ninepatch イメージの違いに注意してください。

于 2011-04-01T23:20:13.777 に答える