Bitmap与Drawable相互转化

来源:互联网 发布:淘宝刷心悦会员被骗了 编辑:程序博客网 时间:2024/06/13 10:54

/** * Bitmap转化为Drawable */public Drawable Bitmap2Drawable(Bitmap bitmap){BitmapDrawable drawable = new BitmapDrawable(bitmap);return drawable;}/** * Drawable转化为Bitmap */public Bitmap Drawable2Bitmap(Drawable drawable){BitmapDrawable bitDrawable = (BitmapDrawable)drawable;return bitDrawable.getBitmap();}

/** Bitmap转化为Pixmap */public static Pixmap ToPixmap(Bitmap bitmap){if (bitmap == null) return null;// 从Bitmap创建输出流int size = bitmap.getWidth() * bitmap.getHeight();ByteArrayOutputStream outStream = new ByteArrayOutputStream(size);bitmap.compress(Bitmap.CompressFormat.PNG, 0, outStream);// 从输出流创建Pixmapbyte[] img = outStream.toByteArray();return new Pixmap(img, 0, img.length);}

/** 从Assets目录中读取图片, fileName = "texture/0_1.jpeg" */@SuppressWarnings("unused")public static Bitmap getAssetsBitmap(String fileName){// 判断文件是否存在FileHandle handle = Gdx.files.internal(fileName);if (!handle.exists()) return null;Bitmap image = null;AssetManager manager = HlgeActivity.$this.getResources().getAssets();try{InputStream is = manager.open(fileName);image = BitmapFactory.decodeStream(is);is.close();}catch (IOException e){e.printStackTrace();}return image;}






0 0
原创粉丝点击