方便,好体验得显示长图片

/*

显示长图片,但是要小心OOM,目前我的图片为5.32MB可以正常使用.

如果出现OOM,可以尝试使用ListView

也可以使用webView,但是webView体验不如本地原生.

*/


public void setLongPicture(String assetsLocation){

try {

//获得图片的宽、高

        BitmapFactory.Options tmpOptions =new BitmapFactory.Options();

        tmpOptions.inJustDecodeBounds =true;

        BitmapFactory.decodeStream(getAssets().open(assetsLocation), null, tmpOptions);

        int width =tmpOptions.outWidth;

        int height =tmpOptions.outHeight;

        //设置显示图片的中心区域

        BitmapRegionDecoder bitmapRegionDecoder =BitmapRegionDecoder.newInstance(getAssets().open(assetsLocation), false);

        BitmapFactory.Options options =new BitmapFactory.Options();

        options.inPreferredConfig =Bitmap.Config.ARGB_8888;

        int beforeHeight;

        int currentHeight =0;

        int singleIncrease =4096;

        Bitmap bitmap;

        ImageView imageView;

        while (true){

beforeHeight =currentHeight;

            currentHeight+=singleIncrease;

            if (currentHeight

bitmap =bitmapRegionDecoder.decodeRegion(new Rect(0, beforeHeight, width, currentHeight), options);

                imageView =new ImageView(this);

                imageView.setImageBitmap(bitmap);

                mLl.addView(imageView);

            }else{

bitmap =bitmapRegionDecoder.decodeRegion(new Rect(0, beforeHeight, width, height), options);

                imageView =new ImageView(this);

                imageView.setImageBitmap(bitmap);

                mLl.addView(imageView);

                return;

            }

}

}catch (IOException e) {

e.printStackTrace();

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容