zoukankan      html  css  js  c++  java
  • 【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"/>

    转化后的图片效果:










  • 相关阅读:
    Python 30分钟入门——数据类型 and 控制结构
    类球多面体生成——经纬划分法
    GLUT的简洁OO封装
    Resource Acquisition Is Initialization(RAII Idiom)
    为什么operator>>(istream&, string&)能够安全地读入长度未知的字符串?
    斯坦福2014机器学习笔记六----神经网络(一)
    斯坦福2014机器学习笔记五----正则化
    斯坦福2014机器学习笔记四----逻辑回归
    斯坦福2014机器学习笔记三----多变量线性回归与梯度下降法
    斯坦福2014机器学习笔记二----梯度下降法
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5378671.html
Copyright © 2011-2022 走看看