BitmapFactory方法:
当然,肯定也会给我们提供一个接口给我们来创建Bitmap的,而这个接口类就是:BitmapFactory!来来来,打开BitmapFactory类,我们点下左边的Structure可以看到BitmapFactory给我们
我们首先得获得这个 2.Bitmap常用方法
普通方法
也是前面学的那13种Drawable类型的可视化对象!我们可以理解成一个用来放画的——画框! Bitmap(位图):我们可以把他看作一个画架,我们先把画放到上面,然后我们可以实现代码:
进行一些处理,比如获取图像文件信息,做旋转切割,放大缩小等操作! Canvas(画布):如其名,画布,我们可以在上面作画(绘制),你既可以用Paint(画笔),//通过资源ID private Bitmap getBitmapFromResource(Resources res, int resId) { return BitmapFactory.decodeResource(res, resId); } //文件 private Bitmap getBitmapFromFile(String pathName) { return BitmapFactory.decodeFile(pathName); } //字节数组 public Bitmap Bytes2Bimap(byte[] b) { if (b.length != 0) { return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } } //输入流 private Bitmap getBitmapFromStream(InputStream inputStream) { return BitmapFactory.decodeStream(inputStream); } 4.获取Bitmap的相关信息:
这个,只要我们获取了Bitmap对象,就可以调用相关方法来获取对应的参数了,getByteCount获得大小,
我们对这里的某些参数的值进行设置,比如inJustDecodeBounds设置为true避免OOM(内存溢出),
都是静态方法,直接调,可以通过资源ID、路径、文件、数据流等方式来获取位图!
先来区分几个名词的概念: 如果要截全屏,自行谷歌~! 本节引言:在上一节中我们对Android中的13种类型的Drawable的类型进行了讲解,有没有应用到自己的
3.获取Bitmap位图
从资源中获取位图的方式有两种:通过BitmapDrawable或者BitmapFactory,下面演示下:
方法比较多,就不一一进行讲解了,我们从中挑几个用得较多的来讲解!
原图:
如题,本来可以直接说着三个东东的关系的,但是我就是要傲娇,就要看代码!
stream:输出流
BitmapDrawable方法:
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.pic_meizi); Bitmap bitmap2 = Bitmap.createBitmap(bitmap1,100,100,200,200); img_bg = (ImageView) findViewById(R.id.img_bg); img_bg.setImageBitmap(bitmap2);运行效果图:
如果你打开Bitmap类的源码,你会看到Bitmap的构造方法上有这样一段东东: public class MainActivity extends AppCompatActivity { static ByteArrayOutputStream byteOut = null; private Bitmap bitmap = null; private Button btn_cut; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_cut = (Button) findViewById(R.id.btn_cut); btn_cut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { captureScreen(); } }); } public void captureScreen() { Runnable action = new Runnable() { @Override public void run() { final View contentView = getWindow().getDecorView(); try{ Log.e(HEHE,contentView.getHeight()+:+contentView.getWidth()); bitmap = Bitmap.createBitmap(contentView.getWidth(), contentView.getHeight(), Bitmap.Config.ARGB_4444); contentView.draw(new Canvas(bitmap)); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOut); savePic(bitmap, sdcard/short.png); }catch (Exception e){e.printStackTrace();} finally { try{ if (null != byteOut) byteOut.close(); if (null != bitmap && !bitmap.isRecycled()) { // bitmap.recycle(); bitmap = null; } }catch (IOException e){e.printStackTrace();} } } }; try { action.run(); } catch (Exception e) { e.printStackTrace(); } } private void savePic(Bitmap b, String strFileName) { FileOutputStream fos = null; try { fos = new FileOutputStream(strFileName); if (null != fos) { boolean success= b.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); if(success) Toast.makeText(MainActivity.this, 截屏成功, Toast.LENGTH_SHORT).show(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }代码分析:
quality:压缩质量,0-100,0表示最低画质压缩,100最大质量(PNG无损,会忽略品质设定)什么,不知道OOM,没事,等下一点点跟你说清楚!最后回到我们的Bitmap!嗯,Bitmap中的
于是乎我们发现了这货是一个静态内部类:BitmapFacotry.Options!
public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)