本次主要实现点击图片消息显示原图(ShowImageActivity)非常简单的界面
public class ShowImageActivity extends BaseActivity {
private static final String TAG = ShowImageActivity.class.getSimpleName();
@BindView(R.id.image_view)
PhotoView mIvImage;
private IMMessage mMessage;
private Bitmap bitmap;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//隐藏系统状态栏
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
//requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_show_image);
ButterKnife.bind(this);
initImage();
}
private void initImage() {
mIvImage.setMaxScale(4);
mIvImage.enable();
mMessage = (IMMessage) getIntent().getSerializableExtra("IMMessage");
if (mMessage == null) {
ToastUtils.showMessage(this, "图片无法显示,请重试~");
finish();
return;
}
String path = ((ImageAttachment) mMessage.getAttachment()).getPath();
// 原图已经下载,显示原图
if (mMessage.getAttachStatus() == AttachStatusEnum.transferred
&& !TextUtils.isEmpty(path)) {
Bitmap origin_bitmap = BitmapFactory.decodeFile(path);
int height = (int) (origin_bitmap.getHeight() * (512.0 / origin_bitmap.getWidth()));
bitmap = Bitmap.createScaledBitmap(origin_bitmap, 512, height, true);
if (bitmap != null) {
mIvImage.setImageBitmap(bitmap);
} else {
ToastUtils.showMessage(ShowImageActivity.this, "原图 下载/显示 失败,请重试~");
finish();
}
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
if (bitmap != null && !bitmap.isRecycled())
bitmap.recycle();//防止图片过大造成java.lang.OutOfMemoryError
super.onDestroy();
}
}
有的图片比较大,在显示是会报错,通过上网搜素解决方法,最终决定缩放原图在显示:
int height = (int) (origin_bitmap.getHeight() * (512.0 / origin_bitmap.getWidth()));
bitmap = Bitmap.createScaledBitmap(origin_bitmap, 512, height, true);
最终效果如下图,因为用的PhotoView,所以自带两指缩放:
界面应该是隐藏系统状态栏的,但因为此手机截屏要下拉菜单,所以显示出了状态栏。