所以我对Android开发人员来说相当新,而且我在使用ImageView时遇到了一个奇怪的问题。任何指针或建议都会受到欢迎!
我正在动态设置我的ImageViews的位图,这很有用。除了他们有时只显示图像,其余的时间我得到一个完整的颜色填充与大致的图像颜色如下所示。
我认为使用Android论坛提供的代码可以正确缩放它们,所以我不认为我遇到了内存问题....
public static Bitmap decodeSampledBitmapFromStream(InputStream inputStream, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream,null,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(inputStream,null,options);
}
答案 0 :(得分:1)
经过几个小时的处理后,我终于想通了,因为我使用AsyncTask来加载这些位图,所以它们偶尔会比主UI线程更快。当我打电话给myImageView.getHeight()
&宽度()。
所以这是我提出的解决方案,希望它可以帮助其他人:
public class DecodeTask extends AsyncTask<String, Void, Bitmap> {
public ImageView currentImage;
private static AssetManager mManager;
public DecodeTask(ImageView iv, AssetManager inputManager) {
currentImage = iv;
mManager = inputManager;
}
protected Bitmap doInBackground(String... params) {
int bottomOut = 1000;
while(currentImage.getMeasuredWidth() == 0 && currentImage.getMeasuredHeight() == 0 && (--bottomOut) > 0)
{
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
InputStream iStream = null;
Bitmap bitmap = null;
try {
iStream = mManager.open("photos" + File.separator + params[0]);
bitmap = ImageUtils.decodeSampledBitmapFromStream(iStream, currentImage.getMeasuredWidth(), currentImage.getMeasuredHeight());
iStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if(currentImage != null) {
currentImage.setImageBitmap(result);
currentImage.invalidate();
}
}
}