android imageview 从assets里获取图片资源

2017-11-28  本文已影响0人  PengboGai

最常见:

ImageView.setImageResource(R.drawable.icon);

看下面这段话

Sets a drawable as the content of this ImageView.

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider usingsetImageDrawable(Drawable)orsetImageBitmap(Bitmap)andBitmapFactoryinstead.

这个ImageView设置一个动态引用的图片内容。(我在这里因为资源引用卡住了,记得有不同方法getString,getDrawable,getInteger)

如果这是一个问题,考虑usingsetImageDrawable(可移动)或setImageBitmap(位图)和BitmapFactory代替。

此函数使用UI线程,可以用下面3种替代:

ImageView iv;

String fileName = "/data/data/com.test/aa.png;

Bitmap bm = BitmapFactory.decodeFile(fileName);

iv.setImageBitmap(bm);

ImageView iv = new ImageView(context);

iv.setImageResource(iv[position]);

iv.setScaleType(ImageView.ScaleType.FIT_XY);

iv.setLayoutParams(new Gallery.LayoutParams(136,88));

mImageView = (ImageView)this.findViewById(R.id.myImageView1);

mImageView.setImageDrawable(getResources().getDrawable(R.drawable.right)

/*

* 从Assets中读取图片

*/

Bitmapbitmapimg = getImageFromAssetsFile("Cat_Blink/cat_blink0000.png");

private Bitmap getImageFromAssetsFile(String fileName)

{

Bitmap image =null;

AssetManager am = getResources().getAssets();

try

{

InputStream is = am.open(fileName);

image = BitmapFactory.decodeStream(is);

is.close();

}

catch(IOException e)

{

e.printStackTrace();

}

returnimage;

}

上一篇下一篇

猜你喜欢

热点阅读