• 【Android开发学习46】Android平台切割PNG图片成小png图片


    功能描写叙述:
    分解 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"/>

    转化后的图片效果:










  • 相关阅读:
    # codeforces 1199 D. Welfare State(思维+dp)
    # codeforces 1245 A. A. Good ol' Numbers Coloring(数学)
    # codeforces 1272 F. Two Bracket Sequences(三维dp + bfs)
    关于MySQL无法启动 ERROR 2003 (HY000)
    PAT 甲级测试题目 -- 1002 A+B for Polynomials
    PAT 甲级测试题目 -- 1001 A+B Format
    Flask 中接收 web 端 JSON 的小例子
    一些工具
    MySql的一些常见问题(Windows 和 Ubuntu 下)
    Python学习笔记
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5378671.html
Copyright © 2020-2023  润新知