点击发送后图片查看大图的实现思路:
1.通过Intent传递key,包括大图和小图的key
2.通过大图的key从缓存中查询图片的Bitmap
3.如果缓存中已经有了,则直接显示
4.如果缓存中没有,则先显示小图,然后下载大图后再显示
主要代码如下:
/**
* Created by zhanghuirong on 2016/2/26.
*/
public class ImageDisplayActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
mLocalHandler = new LocalHandler();
mLocalHandler.registerHandleCallback(mCallback);
mImageView = (ImageView)findViewById(R.id.image_view);
ViewTreeObserver viewTreeObserver = mImageView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
if (intent != null) {
String smallImageKey = intent.getStringExtra(SMALL_IMAGE_KEY);
String imageKey = intent.getStringExtra(IMAGE_KEY);
Bitmap bitmap = ImageMsgManager.getCacheBitmap(imageKey);
if (bitmap == null) {
Bitmap smallBitmap = ImageMsgManager.getCacheBitmap(smallImageKey);
if (smallBitmap != null) {
showImage(smallBitmap);
}
downloadImage(imageKey);
} else {
showImage(bitmap);
}
}
}
});
}
private void downloadImage(String imageKey) {
ImageMsgManager.ImgageDownloadListener listener = new ImageMsgManager.ImgageDownloadListener() {
@Override
public void onSuccess(final Bitmap img) {
ImageMsgManager.setCacheBitmap(imageKey, img);
if (mLocalHandler != null) {
mLocalHandler.sendMessageDelayed(mLocalHandler.obtainMessage(CALLBACK_DOWNLOAD_BIG_IMAGE_SUCCESS, imageKey), 1000);
}
}
@Override
public void onFailure() {
}
};
String filePath = ImageBitmapUtil.getBitmapCacheDirByUrl(ctx, imageKey);
ImageMsgManager.downloadImage(imageKey, filePath, listener);
}
private void showImage(Bitmap bitmap) {
if (bitmap == null || bitmap.isRecycled()) {
return;
}
int viewWidth = mImageView.getMeasuredWidth();
int viewHeight = mImageView.getMeasuredHeight();
int imgWidth = bitmap.getWidth();
int imgHeight = bitmap.getHeight();
float scale = 1;
float baseX = 0;
float baseY = 0;
float scaleX = (float) viewWidth / imgWidth;
float scaleY = (float) viewHeight / imgHeight;
if (scaleX < scaleY) { //按宽度缩放
scale = scaleX;
baseY = ((viewHeight - (imgHeight * scale)) / 2);
} else if (scaleX > scaleY) { //按高度缩放
scale = scaleY;
baseX = ((viewWidth - (imgWidth * scale)) / 2);
}
Matrix matrix = new Matrix();
matrix.set(mImageView.getImageMatrix());
matrix.postScale(scale, scale);
matrix.postTranslate(baseX, baseY);
mImageView.setImageMatrix(matrix);
mImageView.setImageBitmap(bitmap);
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocalHandler.unRegisterHandleCallback();
mLocalHandler = null;
}
private Handler.Callback mCallback = new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case CALLBACK_DOWNLOAD_BIG_IMAGE_SUCCESS:
String imageKey = (String) msg.obj;
Bitmap bitmap = ImageMsgManager.getCacheBitmap(imageKey);
showImage(bitmap);
break;
default:
break;
}
return false;
}
};
}