在Android中,我可以在布局XML使用来自资产的形象呢?布局、形象、资产、Android

2023-09-04 07:24:19 作者:心里心外

这是绘制资源目前正在加载的图像 -

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;
 }