実行時に 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 ディレクトリの下にあります。