功能描写叙述:
分解 assets 文件夹文件下的 PNG 图片成 各个小尺寸 PNG 图片 。


主函数运行:

// 创建文件夹, 用来保存分解出来的图片
createPath("/sdcard/gui_blocks/");
createPath("/sdcard/gui_blocks_2/");
createPath("/sdcard/gui_blocks_3/");
createPath("/sdcard/gui_blocks_4/");
createPath("/sdcard/items/");
createPath("/sdcard/items_3x/");
// 分解 gui_blocks.png
Bitmap resource = getImageFromAssetsFile("gui_blocks.png");
System.out.println("DTPrint first System.out.println"+resource);
Log.d("TAG", "DTPrint first tag"+resource);
int i = 0;
int j = 0;
String pic_Name=null;
for( i=0; i<10; i++)
{
for( j=0; j<10; j++)
{
Bitmap zero = Bitmap.createBitmap(resource, i*48, j*48, 48, 48);
pic_Name=String.format("gui_blocks/%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
// 分解 gui_blocks_2.png
Bitmap resource_gui_blocks_2 = getImageFromAssetsFile("gui_blocks_2.png");
i = 0;
j = 0;
for( i=0; i<10; i++)
{
for( j=0; j<7; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_gui_blocks_2, i*48, j*48, 48, 48);
pic_Name=String.format("gui_blocks_2/big_%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
for( i=0; i<32; i++)
{
for( j=0; j<3; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_gui_blocks_2, i*16, 384+j*16, 16, 16);
pic_Name=String.format("gui_blocks_2/small_%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
// 分解 gui_blocks_3.png
Bitmap resource_gui_blocks_3 = getImageFromAssetsFile("gui_blocks_3.png");
i = 0;
j = 0;
for( i=0; i<10; i++)
{
for( j=0; j<7; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_gui_blocks_3, i*48, j*48, 48, 48);
pic_Name=String.format("gui_blocks_3/big_%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
for( i=0; i<32; i++)
{
for( j=0; j<3; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_gui_blocks_3, i*16, 384+j*16, 16, 16);
pic_Name=String.format("gui_blocks_3/small_%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
// 分解 gui_blocks_4.png
Bitmap resource_gui_blocks_4 = getImageFromAssetsFile("gui_blocks_4.png");
i = 0;
j = 0;
for( i=0; i<10; i++)
{
for( j=0; j<9; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_gui_blocks_4, i*48, j*48, 48, 48);
pic_Name=String.format("gui_blocks_4/big_%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
for( i=0; i<32; i++)
{
for( j=0; j<4; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_gui_blocks_4, i*16, 432+j*16, 16, 16);
pic_Name=String.format("gui_blocks_4/small_%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
// items.png
Bitmap resource_items = getImageFromAssetsFile("items.png");
i = 0;
j = 0;
for( i=0; i<16; i++)
{
for( j=0; j<12; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_items, i*16, j*16, 16, 16);
pic_Name=String.format("items/big_%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
for( i=0; i<16; i++)
{
//for( j=0; j<4; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_items, i*16, 240, 16, 14);
pic_Name=String.format("items/small_%d.0.png", i);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
// items_3x.png
Bitmap resource_items_3x = getImageFromAssetsFile("items_3x.png");
i = 0;
j = 0;
for( i=0; i<16; i++)
{
for( j=0; j<12; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_items_3x, i*16, j*16, 16, 16);
pic_Name=String.format("items_3x/big_%d.%d.png", i, j);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}
for( i=0; i<16; i++)
{
//for( j=0; j<4; j++)
{
Bitmap zero = Bitmap.createBitmap(resource_items, i*16, 240, 16, 14);
pic_Name=String.format("items_3x/small_%d.0.png", i);
saveBitmap(zero, pic_Name);
// 先推断是否已经回收
if(zero != null && !zero.isRecycled()){
// 回收而且置为null
zero.recycle();
zero = null;
}
}
}

支持函数:
/**
* 从Assets中读取图片
*/
private Bitmap getImageFromAssetsFile(String fileName)
{
Bitmap image = null;
AssetManager am = getResources().getAssets();
try
{
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return image;
}
/** 保存方法 */
public void saveBitmap(Bitmap bm, String picName) {
Log.e("TAG", "保存图片");
File f = new File("/sdcard/", picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.i("TAG", "已经保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 创建文件夹文件
*/
public static void createPath(String path) {
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
}

在 AndroidManifest.xml 文件里加入权限支持:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

转化后的图片效果:










【Android开发学习46】Android平台切割PNG图片成小png图片的更多相关文章

  1. 吴裕雄--天生自然Android开发学习:android 背景相关与系统架构分析

    1.Android背景与当前的状况 Android系统是由Andy Rubin创建的,后来被Google收购了:最早的版本是:Android 1.1版本 而现在最新的版本是今年5.28,Google ...

  2. Android开发学习总结——Android开发的一些相关概念

    一.什么是3G.4G 1995年问世的第一代模拟制式手机(1G)只能进行语音通话. 1996到1997年出现的第二代GSM.CDMA等数字制式手机(2G)便增加了接收数据的功能 Ÿ 3G指的是第三代移 ...

  3. Android开发学习总结——Android开发的一些相关概念(转)

    一.什么是3G.4G 1995年问世的第一代模拟制式手机(1G)只能进行语音通话. 1996到1997年出现的第二代GSM.CDMA等数字制式手机(2G)便增加了接收数据的功能 Ÿ 3G指的是第三代移 ...

  4. 吴裕雄--天生自然Android开发学习:Android studio 3.5安装详解

    3. 建立AVD(安卓虚拟设备) 点击右上角AVD Manager图标,单击按钮Create Virtual Device,选择Nexus 5X,下一步,选择版本9.0,Download,然后Next ...

  5. 吴裕雄--天生自然Android开发学习:android开发知识学习思维导图

  6. Android开发学习总结(一)——搭建最新版本的Android开发环境

    Android开发学习总结(一)——搭建最新版本的Android开发环境(转) 最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是 ...

  7. Android开发学习之路--Android Studio cmake编译ffmpeg

      最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...

  8. Android开发学习路线的七个阶段和步骤

    Android开发学习路线的七个阶段和步骤           Android学习参考路线     第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和St ...

  9. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

随机推荐

  1. Code Conventions for the JavaScript Programming Language

    This is a set of coding conventions and rules for use in JavaScript programming. It is inspired by t ...

  2. Oracle学习笔记:ORA-22992 cannot use LOB locators selected from remote tables

    通过DB_LINK访问远程表的时候出现 ORA-22992: cannot use LOB locators selected from remote tables 错误. 原因:因为表中含有clob ...

  3. 【笔记】Python简明教程

    Python简明教程,此资源位于http://woodpecker.org.cn/abyteofpython_cn/chinese/ s=u'中文字符' #u表示unicode,使用u之后能正常显示中 ...

  4. gym 100531 三维几何+搜索

    精度有点毒, 其实可以不用double, 因为A, B必定在其中一个在三角形上,可以投影到只有x,y轴的地方叉积比较. #include<bits/stdc++.h> #define LL ...

  5. Ubuntu18.04 n卡配置

    一.背景 最近安装了ubutnu18.04后,安装系统后重启卡住,强制关机重启后,又在开机界面紫屏卡住,上网搜索后发现是n卡的驱动问题,使用以下方法安装驱动后成功解决该问题. 二.解决方法 1.进入恢 ...

  6. poj1182 食物链(带权并查集)

    题目链接 http://poj.org/problem?id=1182 思路 前面做的带权并查集的权值记录该结点与其父结点是否是同一类,只有两种取值情况(0,1),在这题中某结点a和其父结点b的取值共 ...

  7. ionic启动App时不使用淡入淡出效果

    找到我们项目下面 res下面的config.xml,然后加入下面两句就可以解决. <preference name="FadeSplashScreen" value=&quo ...

  8. 10 Best jQuery and HTML5 WYSIWYG Plugins

    https://www.sitepoint.com/10-best-html-wysiwyg-plugins/

  9. 启动win8.1时提示 api-ms-win-crt-runtime-l1-1-0.dll丢失

    电脑里没有安装windows的更新KB2999226 到微软官网 https://www.microsoft.com/zh-cn/download/details.aspx?id=49081 下载对应 ...

  10. libhiredis.so.0.13: cannot open shared object file: No such file or director

    Hiredis安装步骤: tar zxvf antirez-hiredis-v0.10.1-0-g3cc6a7f.zip cd antirez-hiredis-3cc6a7f make 解决办法: m ...