实训第八周(2)

本次主要实现点击图片消息显示原图(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,所以自带两指缩放:


界面应该是隐藏系统状态栏的,但因为此手机截屏要下拉菜单,所以显示出了状态栏。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值