当我尝试在位图缩放后在图像视图中设置图像时,我遇到了白色空白屏幕的奇怪问题。
我从服务器获取随机大小的图像,我只需要在我的图像视图中显示它们而不会丢失图像质量。我采用了一种布局,并通过代码动态添加图像视图。
以下是我在代码中所做的事情: -
获取Url表单服务器并相应地扩展它: -
url="http://www.petstory.co/attached_king_photos/hotel/7/attached_d67e2689e0d3370f7e76b5bba9f8e875e42a2e39.jpg"
AsyncTask task = new AsyncTask<String, String, Bitmap>() {
Bitmap bitmap = null;
@Override
protected Bitmap doInBackground(String... params) {
bitmap = getBitmapFromURL(params[0]);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (result.getHeight() > 7000){
getResizedBitmap(result, 4000, 4000, image);
}else{
getResizedBitmap(result, 800, 800, image);
}
}
}.execute(imageUrl);
public Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight, final ImageView image) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = bm.getWidth();
int height = bm.getHeight();
final int wantedgeightis = newHeight;
float scaleWidth = metrics.scaledDensity;
float scaleHeight = metrics.scaledDensity;
// float scaleWidth = ((float) newWidth) / width;
// float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
matrix.setRectToRect(new RectF(0, 0, bm.getWidth(), bm.getHeight()), new RectF(0, 0, newWidth, newHeight), Matrix.ScaleToFit.CENTER);
final Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
// final Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//
if (wantedgeightis == 4000) {
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setImageBitmap(resizedBitmap);
} else {
int bitmapWidth = resizedBitmap.getWidth();
int bitmapHeight = resizedBitmap.getHeight();
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, bitmapHeight);
image.setLayoutParams(params);
image.setPadding(10, 0, 10, 0);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setImageBitmap(resizedBitmap);
}
}
});
return resizedBitmap;
}
我在上述方法中设置的图像网址是: -
我正在对nexus进行测试,其余的图像是预期的,但只有这个图像显示我的白色空白
我甚至使用Glide和pisacco为此图像获得相同的问题,或者有时高度扭曲的图像会出现在屏幕上。
以下是我使用的Glide代码: -
Glide.with(this) //passing context
.load(imageUrl) //passing your url to load image.
.placeholder(R.drawable.petzview_logo) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
.error(R.drawable.petz_test)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
.diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
.fitCenter()//this method help to fit image into center of your ImageView
.dontAnimate()
.override(700, 4000)
.into(image);
我甚至尝试过波纹管代码: -
Glide.with(this)
.load(imageUrl)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(R.drawable.petzview_logo)
.dontAnimate()
.fitCenter()
.into(new ViewTarget<ImageView, GlideDrawable>(image) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation anim) {
Toast.makeText(HowAboutHere.this, "success", Toast.LENGTH_LONG).show();
image.setImageDrawable(resource.getCurrent());
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
Toast.makeText(HowAboutHere.this, "Glide image load failed for %s -> %s" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
image.setVisibility(View.INVISIBLE);
}
});
通过使用.override(700,4000),图像显示但是拉伸,如果我将值更改为.override(700,4100),则空白屏幕开始显示由于非常大的图像尺寸
或者我应该首先将图像从URL保存到任何文件,然后从文件中检索图像并在其上应用inSample size bitmap algo,这将耗费时间并利用更多内存并使应用程序变慢。如下所述两个链接: -
Best method to download image from url in Android
Strange out of memory issue while loading an image to a Bitmap object
然而,我在这里发布的图片看起来就像当我使用Bitmap代码表格显示来自URL
的巨大图像时正如您所见,图像仍然被拉伸。
任何帮助将不胜感激
感谢!!!