看起來你沒有正確地創建位圖,但如果我是你的位置我想創建一個縮放位圖如下所示:
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
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
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
然後將其設置爲以下這樣一個ImageView的:
mImg.setImageBitmap(img);
整體而言,這應該是這樣的:
public void loadImage() {
Picasso.with(getBaseContext()).load("image url").into(new Target() {
// ....
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
// Pick arbitrary values for width and height
Bitmap resizedBitmap = getResizedBitmap(bitmap, newWidth, newHeight);
mImageView.setBitmap(resizedBitmap);
}
// ....
});
}
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
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
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
但我你使用質疑總而言之,通常是針對非常特殊的情況。你應該在相同的班級中調用Picasso
的單身人士,你將會顯示圖像。通常這是在Adapter
(RecyclerView適配器也許)像這樣:
Picasso.with(mContext)
.load("image url")
.into(mImageView);
謝謝,我已經有到位的調整方法(http://developer.android.com/training/displaying-bitmaps/load-bitmap .html)。我只是需要從一個URL加載一個圖像到一個位圖(不直接進入一個ImageView),從上面的註釋中找出它。非常感謝。 – Alex
爲什麼使用異步任務來加載圖像。你可以在.into(mImageView,new Callback <> {...})中實現回調。 – AndyRoid
實際上,我正在嘗試一種快速的方法將圖像加載到位圖中,然後將其確實傳遞給Adapter(RecycleView),而不是從資源文件夾中加載它。我試圖調試這裏描述的問題http://stackoverflow.com/questions/32554358/encountering-lag-when-updating-a-cardview-item-in-a-recycleview。我可能會嘗試在它將顯示的類中加載圖像。但是,儘管畢加索只能用於一個活動課,或者至少這是我從某些帖子中瞭解的。我想我需要將上下文傳遞給Adapter類,對吧? – Alex