android 长图显示模糊,android 加载大图长图失真或者不显示

Android 加载大图或者长图时会遇到失真或者不显示的问题(imageloader 一般会失真,fresco一般不会显示)。

1、首先说一下为什么会出现这样的情况。

Canvas在绘制Bitmap的时候其实对Bitmap的长宽是有限制的,一般是2048或者是4096.这个值可以通过下面代码获取

int[] maxTextureSize = new int[1];

GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0);

int maxBitmapDimension = Math.max(maxTextureSize[0], 2048);

maxBitmapDimension 就是Canvas 可以绘制Bitmap的最大宽高。Imageloader 在加载图片时对Bitmap 宽高做了限制,加载的图片都在这个值以内。比如你要加载的图片高度为8000,而你的手机的Canvas 可绘制图片高度最大值为4096,imageloader经过处理之后加载的图片高度其实是2000,比原始图片缩小了四倍,所以看起来才会模糊。而fresco 没有做这样的处理,所以图片不会显示,只会有异常提示:Bitmap too large to be uploaded into a texture 。

2、下面我们就来解决这个问题。

既然Canvas 只能绘制比4096小的图片,那我们就只能把图片限制在4096以内,但是我们又不显图像失真,所以不能采取像素压缩,那么怎么办呢。我们可以利用BitmapRegionDecoder类。BitmapRegionDecoder 可以获取指定区域的图片。比如高度为8000的图片。我们可以用BitmapRegionDecoder生成两张高度为4000的图片。这样我们就可以用Canvas把两张图片画出来。

public class BigImageView extends ImageView{

private int windowWidth;

private BitmapRegionDecoder mDecoder;

private Rect bsrc;

private Rect src;

private Rect dst;

private Paint paint;

private ArrayList bmps;

public BigImageView(Context context) {

super(context);

// TODO Auto-generated constructor stub

init();

}

public BigImageView(Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

init();

}

private void init() {

// TODO Auto-generated method stub

windowWidth = getResources().getDisplayMetrics().widthPixels;

bmps = new ArrayList();

src = new Rect();

dst = new Rect();

bsrc = new Rect();

paint = new Paint();

paint.setAntiAlias(true);

paint.setFilterBitmap(true);

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

int count = bmps.size();

for(int i = 0;i

Bitmap bmp = bmps.get(i);

src.left = 0;

src.top = 0;

src.right = bmp.getWidth();

src.bottom = bmp.getHeight();

dst.left = 0;

dst.top = i*getHeight()/count;

dst.right = getWidth();

dst.bottom = dst.top+getHeight()/count;

canvas.drawBitmap(bmp, src, dst, paint);

}

}

@Override

public void setImageBitmap(final Bitmap bm) {

super.setImageBitmap(bm);

if(bm!=null){

float imgmul = (float)bm.getHeight()/(float)bm.getWidth();

LayoutParams lp = this.getLayoutParams();

lp.width = windowWidth;

lp.height = (int) (imgmul*windowWidth);

this.setLayoutParams(lp);

}

new Thread() {

public void run() {

try{

int imageCount = bm.getHeight() % ImageUtils.getMaxHeight();

ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

InputStream isBm = new ByteArrayInputStream(baos.toByteArray());

mDecoder = BitmapRegionDecoder.newInstance(isBm, true);

for (int i = 0; i < imageCount; i++) {

bsrc.left = 0;

bsrc.top = i*bm.getHeight()/imageCount;

bsrc.right = bm.getWidth();

bsrc.bottom = bsrc.top+bm.getHeight()/imageCount;

Bitmap bmp = mDecoder.decodeRegion(bsrc,null);

bmps.add(bmp);

}

postInvalidate();

}catch(Exception e){

e.printStackTrace();

}

}

}.start();

}

}

public class ImageUtils {

private static final int DEFAULT_MAX_BITMAP_DIMENSION = 2048;

private static int maxHeight = 0;

static {

int[] maxTextureSize = new int[1];

GLES10.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0);

maxHeight = Math.max(maxTextureSize[0], DEFAULT_MAX_BITMAP_DIMENSION);

}

public static int getMaxHeight(){

return maxHeight;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值