Bitmap静态方法

Bitmap静态创建方法

static Bitmap createBitmap(int width, int height, Bitmap.Config config);
static Bitmap createBitmap(Bitmap src);
static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height);
static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter);
static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config);
static BitmapcrfeateBitmap(int[]  colors, int offset, int stride, int width, int height, Bitmap.Config config);
static Bitmap createScaledBitmap(Bitmap src, int dstWidth. int dstHeight, boolean filter);


static Bitmap createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config);
static Bitmap createBitmap(DisplayMetrics display, int[]  colors, int width, int height, Bitmap.Config config);
static Bitmap createBitmap(DisplayMetrics display, int[] colors, int offset, int stride, int width, int height, Bitmap.Config config);

createBitmap(int width, int height, Bitmap.Config config)

这个函数可以创建一幅指定大小的空白图像
width、height:用于指定所创建空白图像的尺寸,单位是px
config:用于指定单个像素的存储格式,取值有ALPHA_8, ARGB_4444, ARGB_8888, RGB_565.默认取值ARGB_8888

createBitmap(Bitmap src)

根据一张Bitmap创建一份完全一样的Bitmap

createBitmap(Bitmap bitmap, int x, int y, inr width, int height)

主要用于裁剪图片
bitmap:用于裁剪的源图像
int x, y:开始裁剪的位置点坐标
int width, height:裁剪的宽度和高度

eg:

Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.xxx);
Bitmap cuted = Bitmap.createBitmap(bitmap, bitmap.getWidth()/3, bitmap.getHeight()/3, bitmap.getWidth()/3, bitmap.getHeight()/3);

createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

Matrix:给裁剪后的图像添加矩阵
filter:对应paint.setFilterBitmap(filter)是否给图像添加滤波功能,如果设置为true,则能够减少图像中由于噪声引起的突兀的鼓励像素点或像素块

eg:

matrix matrix = new Matrix();
matrix.setScale(2,1);
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.xxx);
Bitmap cutBmp = Bitmap.createBitmap(bitmap, bitmap.getWidth()/3, bitmap.getHeight()/3, bitmap.getWidth()/3, bitmap.getHeight()/3,matirx, true);

指定色彩创建Bitmap

//colors:当前图像所对应的每个像素的数组,他的数组长度必须大于width*height
//width,height:需要创建的图像的宽和高
//config:用于指定每个像素的存储格式
static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config);
static Bitmap createBitmap(int[] ccolors, int offset, int stride, int width, int height, Bitmap.Config config);

eg:


public class BitmapStaticActivity extends AppCompatActivity {


    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap_static);
        imageView = (ImageView)findViewById(R.id.bitmap);
        createBmpByColors();


    }
    private int[] initColors(int width, int height){
        int[] colors = new int[width * height];
        //根据width和height遍历每个像素,生成argb值
        for(int y = 0;y<height;y++){
            for(int x = 0;x < width;x++){
                int r = x * 255 / (width - 1);
                int g = y * 255 / (width - 1);
                int b = 255 - Math.min(r,g);
                int a = Math.max(r,g);
                colors[y * width + x] = Color.argb(a,r,g,b);
            }
        }
        return colors;
    }


    private void createBmpByColors(){
        int width = 300, height = 200;
        int[] colors = initColors(width, height);
        Bitmap bitmap = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
        imageView.setImageBitmap(bitmap);
    }
}

createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)

src:需要缩放的源图像
dstWidth:缩放后的目标图像的宽
dstHeight:缩放后的目标图像的高
fliter:是否给目标图像添加滤波效果
但是如果缩放的宽和高与源图像一致,那么就会直接返回源图像,并不会创建一个新的图像

eg:

 //将图片将进行缩放
 Bitmap bitmap = Bitmap.createScaledBitmap(src, 300, 200, true);

总结:

  • 加载图像可以使用BitmapFactory和Bitmap的相关方法
  • 通过配置BitmapFactory.Options的参数,可以实现缩放图片,获取图片信息,配置比例缩放等功能
  • 如果需要剪裁或者缩放图片,只能使用Bitmap的create系列函数
  • 在加载或者创建Bitmap时,一定要使用try/catch捕捉OOM

常用函数

  • copy(Config config, boolean isMutable)
config:副本的格式
isMutable:新创建的图像是否可以更改其中的像素
根据源图像创建副本,可以指定副本的像素格式
  • 关于图像是否可以更改:
    • boolean isMutable()来进行判断,如果像素是可以更改的,返回true, 只有通过创建的bitmap的像素是可以更改的,通过加载的bitmap都是不可以进行更改的
    • 创建可以更改像素的Bitmap的函数如下:
copy(Config config, boolean isMutable)
createBitmap(int width, int height, Bitmap.Config config)
createScaledBitmap(Bitmap src, int dstWidth, int dstheight, boolean filter)
cretaeBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config)

Bitmap extractAlpha()

返回的像素存储的格式是ALPHA_8,只有透明的通道,不会存储颜色值

extractAlpha(Paint paint, int[] offsetXY)

从源图像中抽取处置带有Alpha值的图像,返回的结果的像素格式仍然为ALPHA_8
paint:具有MaskFilter效果的Paint对象,一般使用BlurMaskFilter模糊效果
offsetXY:返回在添加BlurMaskFilter效果以后原点的偏移量,是建议 其实绘制的位置

eg:

imageView = (ImageView)findViewById(R.id.alpha);
Bitmap srcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.cat_dog);
Paint paint = new Paint();
BlurMaskFilter blurMaskFilter = new BlurMaskFilter(6, BlurMaskFilter.Blur.NORMAL);
paint.setMaskFilter(blurMaskFilter);
int[] offsetXY = new int[2];
Bitmap alphabitmap = srcBmp.extractAlpha(paint, offsetXY);
Bitmap bitmap = Bitmap.createBitmap(alphabitmap.getWidth(), alphabitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
paint.setColor(Color.GRAY);
canvas.drawBitmap(alphabitmap, 0, 0, paint);
canvas.drawBitmap(srcBmp, -offsetXY[0], -offsetXY[1], null);
imageView.setImageBitmap(bitmap);
srcBmp.recycle();

计算为Bitmap所分配的空间

//API19引入
int getAlloccationByteCount()
//API12引入
int getByteCount()
/获取一行所占用的内存
int getRowBytes()

recycle()和isRecycled()

//强制回收Bitmap所占内存
public void recycled();

//判断当前Bitmap的内存是否被回收
public final boolean isRecycled()

eg:

if(bmp!=null && !bmp.isRecycled()){
    //回收图片所占内存
    bmp.recycled();
    bitmap = null;
    //提醒系统进行回收
    system.gc();
}

setDesity()和getDensity()

//densuity用于设置图像建议的屏幕尺寸
public void setDensity(int dennsity)

public int getDensity()

setPixel()和getPixel()

//用于针对Bitmap中的某个位置的像素进行设置和获取
public void setPixel(int x, int y, int color)\

//用于获取指定位置的像素值
public int getPixel(int x, int y)

compress()

//作用是压缩图像
//format:JPEG, PNG, WEBP
//quality:表示 压缩后的图像的画质,取值是0~100, 0表示最低画质压缩,100表示最高画质压缩,对于PNG等无格式的文件,会忽略此设置
stream:输出值,Bitmap被压缩后,会以OutputStream的形式输出
//返回值表示是否压缩成功
public boolean compress(CompressFormat format, int quality, OutputStream stream)
压缩格式:
  • Compress.Format.JPEG:采用JPEG的压缩算法是一种有损的压缩格式,JPEG不支持透明苏,当遇到透明度像素时,会以黑色背景填充
  • Compress.Format.PNG:采用压缩算法是一种支持透明度的压缩算法
  • Compress.Format.WEBP:是一种提供有损压缩和无损压缩的图片格式,派生自视频格式VP,在API14~17是有损压缩格式,在API18以后支持无损压缩和透明度压缩

猜你喜欢

转载自blog.csdn.net/qq_39424143/article/details/94394231
今日推荐