Android——BitMap位图
简介
BitMap位图:是x * y 个ARGB颜色像素信息的矩阵,用来保存图片的信息。如一张 720*1080的图片,它的位图就是 一个保存 720*1080个ARGB像素的矩阵。
参考博文:https://www.cnblogs.com/shakinghead/p/11025805.html
https://blog.csdn.net/u011686167/article/details/88682502
方法
创建一:不用
Bitmap Bitmap.creatBitmap( int width, int height, Bitmap.Config config )
参数:
width:创建的bitmap的宽
height:创建的bitmap的高
config:初始化配置:默认Bitmap.Config.ARGB8888
返回值:Bitmap
返回值意义:创建一个宽width高height的Bitmap
作用:创建一个宽width高height的Bitmap
创建二
BitmapFactory.decodeResource(Resource resource ,int resourceId )
参数:
resource:资源对象。通过View.getResource()或Activity.getResource()获取。
resourceId:资源对象Id。
返回值:Bitmap
返回值意义:获取资源里图片的Bitmap
作用:获取Resource里图片的Bitmap。
不用
BitMapFactory.decodeFile( String filePath )
参数:
filePath:资源绝对路径。这里的绝对路径相对手机。
返回值:Bitmap
返回值意义:获取图片文件的Bitmap
作用:获取图片文件的Bitmap
不用
BitmapFactory.decodeInputStream( InputStream inputstream )
参数:
inputStream:输入流
返回值:Bitmap
返回值意义:根据输入流获取bitmap
作用:获取输入流的bitmap
// 显示一张图片
public class MyCanvs extends View { public MyCanvs(Context context) { super(context); } public MyCanvs(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyCanvs(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hh); canvas.drawBitmap(bitmap,new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),new Rect(0,0,bitmap.getWidth(),bitmap.getHeight()),null); } private Bitmap changeBitmapSize(int resourceId,int newWidth,int newHeight) { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId); int width = bitmap.getWidth(); int height = bitmap.getHeight(); //计算压缩的比率 float scaleWidth= ((float)newWidth)/width; float scaleHeight= ((float)newHeight)/height; //获取想要缩放的matrix Matrix matrix = new Matrix(); matrix.postScale(scaleWidth,scaleHeight); //获取新的bitmap bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true); bitmap.getWidth(); bitmap.getHeight(); return bitmap; } }