功能描写叙述:
分解 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. MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme

    错误: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named p ...

  2. 使用Appium 测试微信小程序和微信公众号方法

    由于腾讯系QQ.微信等都是基于腾讯自研X5内核,不是google原生webview,需要打开TBS内核Inspector调试功能才能用Chrome浏览器查看页面元素,并实现Appium自动化测试微信小 ...

  3. Linux学习笔记:crontab定时任务

    通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本.时间间隔的单位可以是分钟.小时.日.月.周及以上的任意组合.这个命令非常适合周期性的日志分析或数据 ...

  4. 一个带bash,带glibc,中国时区,非root用户可运行crond命令的基于alpine镜像的Dockerfile

    这个镜像现在说起来简单, 带bash(增加执行脚本的兼容性,带GLIBC,中国时区,非root用户可运行crond命令-安全) 但让我开始陷入时,真的让我有段时间有点爆了. 比如,将filebeat文 ...

  5. Filebeat配置paths里,不支持递归所有子目录

    这个知识点要牢记哟,不然,牛B吹大了,收不回哈. 官方文档为证: Currently it is not possible to recursively fetch all files in all ...

  6. GridView监听器

    package com.example.wang.testapp2; import android.os.Bundle; import android.support.v7.app.AppCompat ...

  7. vuejs、eggjs、mqtt全栈式开发设备管理系统

    vuejs.eggjs.mqtt全栈式开发简单设备管理系统 业余时间用eggjs.vuejs开发了一个设备管理系统,通过mqtt协议上传设备数据至web端实时展现,包含设备参数分析.发送设备报警等模块 ...

  8. USACO 5.5 Picture

    PictureIOI 1998 A number, N (1 <= N < 5000), of rectangular posters, photographs and other pic ...

  9. Bootstrap进阶六:动态样式语言LESS简介

    LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, Firefox),也可以借助Node.js或者R ...

  10. NPOI操作Excel文件

    首先,通过NuGet添加NPOI. NPOI依赖SharpZipLib,通过NuGet添加SharpZipLib. 然后添加NPOI. 添加后项目的引用列表如下: 把DataTable转换成Excel ...