/**
* 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);
// 从输出流创建Pixmap
byte[] 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;
}