在Android中,我可以在布局XML使用来自资产的形象呢? [英] In Android, can I use image from assets in layout xml?
本文介绍了在Android中,我可以在布局XML使用来自资产的形象呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是绘制资源目前正在加载的图像 -
Currently am loading image from drawable resource-
<ImageView
android:id="@+id/stestImg"
android:src="@drawable/testImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
我可以用图像从资产在这里,直接到布局XML?我需要做任何编码的!请帮忙。
can I use image from assets here, directly to the layout XML? Do I need to do any coding for that! Please help.
推荐答案
您可以通过这种方式做到这一点::
ImageView i;
Bitmap bm = getBitmapFromAsset("pic1.png");
i = (ImageView)findViewById(R.id.image);
i.setImageBitmap(bm);
拨打方法::
private Bitmap getBitmapFromAsset(String strName) throws IOException
{
AssetManager assetManager = getAssets();
InputStream istr = assetManager.open(strName);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
return bitmap;
}
这篇关于在Android中,我可以在布局XML使用来自资产的形象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文