一、简介
命名: ImageViewMagnifierStrategy
使用场景:-针对ImageView加载大图片压缩时显示更清晰
核心:提取-局部增大-应用
二、步骤
private intmWidth;
private intmHeight;
...
Bitmap bm1 = BitmapFactory.decodeFile(uri); //uri :Your image path
mWidth=bm.getWidth();
mHeight=bm.getHeight();
int[][]b=new int[mWidth][mHeight];
Bitmap bm2=Bitmap.createBitmap(bm.getWidth(),bm.getHeight(),Bitmap.Config.ARGB_8888);
//1.提取&局部增大
for (int i = 100; i < mWidth - 100; i++) {
for (int j = 100; j < mHeight - 100; j++) {
if((bm1.getPixel(i,j)&0xFF)<255) {
b[i][j]=0xff000000;
b[i-1][j]=0xff000000;
b[i+1][j]=0xff000000;
b[i][j-1]=0xff000000;
b[i][j+1]=0xff000000;
b[i-1][j-1]=0xff000000;
b[i+1][j+1]=0xff000000;
b[i-1][j+1]=0xff000000;
b[i+2][j-1]=0xff000000;
b[i+2][j]=0xff000000;
b[i+2][j+1]=0xff000000;
b[i+2][j+2]=0xff000000;
b[i-1][j+2]=0xff000000;
b[i][j+2]=0xff000000;
b[i+1][j+2]=0xff000000;
b[i][j+2]=0xff000000;
b[i+2][j]=0xff000000;
//根据情况再加
b[i][j+3]=0xff000000;
b[i+3][j]=0xff000000;
b[i][j+4]=0xff000000;
b[i+4][j]=0xff000000;
}
}
}
//3.应用
for (int i = 100; i < mWidth; i++) {
for (int j = 100; j < mHeight; j++) {
bm2.setPixel(i,j,b[i][j]);
}
}
ivCover.setImageBitmap(bm2); //ivCover : Your ImageView
四、总结回顾
提取bitmap像素密度,进行局部放大,重新设置bitmap的像素密度
新建二维数组储存像素点,从bm的宽高开始一个一个像素的查找,得到的每一个bm像素值&0xFF把负值变成正值(画布是白色(0xFFFFFF),有颜色就是rgb即有值,& 0xFF后得到的值小于255代表找到了rgb不为FF的情况,因为rgb的范围是0-255,遍历找到bitmap中的所有符合的像素点,然后以该点为中心,扩散到周围9个点的点阵,如果还需要继续放大,往右往下再扩散一圈,直到你想要的清晰度,最后再遍历,绘制已经局部放大的像素点,设置到bitmap,运用即可.
网友评论