当前位置: 首页 > news >正文

快速生成 Android 的 Splash 的 9 Patch 图片

import java.io.File;import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics2D;import java.awt.image.BufferedImage;import javax.imageio.ImageIO;/*** 实在受不了 Android Studio 里制作 9 Patch 难用的 Editor** 快速生成  Android 的 Splash 的 9 Patch 图片** 找最大一张图片,作为  xxxdpi,比如 1024 * 1024,生成如下尺寸的,注意图片里的上下左右空白边距必须大于 4 * 3 / 0.75 = 16 个像素** xxxhdpi  1024  4.00x**  xxhdpi   768  3.00x**   xhdpi   512  2.00x**   hdpi    384  1.50x**   mdpi    256  1.00x**   ldpi    192  0.75x** @author NeedJava@126.com** @since 2025.07.12*/
public final class Create9Patch
{private static final Item[] ITEMS = new Item[]{new Item( "drawable-xxxhdpi", 4.00F ),  //1024 = * 4.00F / 4new Item( "drawable-xxhdpi",  3.00F ),  // 768 = * 3.00F / 4new Item( "drawable-xhdpi",   2.00F ),  // 512 = * 2.00F / 4new Item( "drawable-hdpi",    1.50F ),  // 384 = * 1.50F / 4new Item( "drawable-mdpi",    1.00F ),  // 256 = * 1.00F / 4new Item( "drawable-ldpi",    0.75F ),  // 192 = * 0.75F / 4};public static void main( final String[] args ) throws Exception{final java.io.BufferedReader reader = new java.io.BufferedReader( new java.io.InputStreamReader( System.in ) );while( true ){System.err.println( "\r\nPlease input image path and press Enter:" );final String source = reader.readLine().trim();final File sourceFile = new File( source.startsWith( "\"" ) && source.endsWith( "\"" ) ? source.substring( 1, source.length() - 1 ) : source );if( sourceFile.isFile() == false ){ System.err.println( "Image not exists! " + sourceFile ); continue; }////////////////////////////////////////////////////////////////////////////////////////////////////////////System.err.println( "\r\nPlease input 9 patch file name and press Enter:" );final String outputName = reader.readLine().trim();////////////////////////////////////////////////////////////////////////////////////////////////////////////create( sourceFile, outputName );System.err.println( "\r\nDONE" );}}public static void create( final File oldFile, final String outputName ) throws Exception{final File outputFolder = new File( oldFile.getParent(), "output"/*HARD_CODE*/ );final String oldName = isTrimEmpty( outputName ) ? "image_splash.9.png"/*HARD_CODE*/ : outputName + ".9.png"/*HARD_CODE*/;////////////////////////////////////////////////////////////////////////////////////////////////////////////////final BufferedImage oldImage = ImageIO.read( oldFile );final int oldWidth  = oldImage.getWidth();final int oldHeight = oldImage.getHeight();////////////////////////////////////////////////////////////////////////////////////////////////////////////////Item item = null; File folder = null; int newWidth = 0, newHeight = 0;for( int i = 0; i < ITEMS.length; i ++ ){if( ( item = ITEMS[i] ) == null || isTrimEmpty( item.mFolder ) ){ continue; }folder = new File( outputFolder, item.mFolder ); folder.mkdirs();newWidth  = (int) ( oldWidth  * item.mTimes / 4/*HARD_CODE*/ );newHeight = (int) ( oldHeight * item.mTimes / 4/*HARD_CODE*/ );ImageIO.write( scaleImageByWidth( oldImage, newWidth, newHeight ), "png"/*HARD_CODE*/, new File( folder, oldName ) );}}public static BufferedImage scaleImageByWidth( final BufferedImage oldImage, final int newWidth, final int newHeight ){final BufferedImage newImage = new BufferedImage( newWidth, newHeight, BufferedImage.TYPE_INT_ARGB );final Graphics2D g2d = newImage.createGraphics();g2d.drawImage( oldImage.getScaledInstance( newWidth, newHeight, Image.SCALE_SMOOTH ), 0, 0, null );////////////////////////////////////////////////////////////////////////////////////////////////////////////////g2d.setColor( Color.BLACK );//////////////////////////////////////////////////////////// Topg2d.drawLine( 1, 0, 2, 0 );g2d.drawLine( newWidth - 3, 0, newWidth - 2, 0 );//////////////////////////////////////////////////////////// Leftg2d.drawLine( 0, 1, 0, 2 );g2d.drawLine( 0, newHeight - 3, 0, newHeight - 2 );//////////////////////////////////////////////////////////// Rightg2d.drawLine( newWidth - 1, 3, newWidth - 1, newHeight - 4 );//////////////////////////////////////////////////////////// Bottomg2d.drawLine( 3, newHeight - 1, newWidth - 4, newHeight - 1 );////////////////////////////////////////////////////////////////////////////////////////////////////////////////g2d.dispose();return newImage;}public static boolean isTrimEmpty( final String string ){return ( string == null ? true : string.trim().length() == 0 );}
}final class Item
{public final String mFolder;public final float mTimes;public Item( final String folder, final float times ){this.mFolder = folder;this.mTimes  = times;}
}

http://www.dtcms.com/a/277217.html

相关文章:

  • 【零基础入门unity游戏开发——unity3D篇】3D光源之——unity反射和反射探针技术
  • AI进化论12:大语言模型的爆发——GPT系列“出圈”,AI飞入寻常百姓家
  • Kafka——Kafka 线上集群部署方案怎么做?
  • c语言初阶 结构体
  • 【Python】venv:配置独立镜像源
  • 常用的docker命令备份
  • 007_用例与应用场景
  • python 列表(List) vs. 元组(Tuple):什么时候该用不可变的元组?它们在性能和用途上有什么区别?
  • 暑期自学嵌入式——Day01(C语言阶段)
  • 协程的基本使用
  • 【保姆级图文详解】MCP架构(客户端-服务端)、三种方式使用MCP服务、Spring AI MCP客户端和服务端开发、MCP部署方案、MCP安全性
  • 基于 CentOS 7 的 LVS+DR+Web+NFS 旅游攻略分享平台部署
  • CentOS系统下前后端项目部署攻略
  • 从 Manifest V2 升级到 Manifest V3:常见问题与解决方案
  • vue-component
  • [Linux入门 ] RAID存储技术概述
  • (S4)Efficiently Modeling Long Sequences with Structured State Spaces论文精读(逐段解析)
  • [Rust 基础课程]Hello World
  • 数据结构 单链表(2)--单链表的实现
  • 聊一聊Java生态接口测试常见的框架
  • 在 Spring Boot 中使用 MyBatis 的 XML 文件编写 SQL 语句详解
  • MySQL SQL语句精要:DDL、DML与DCL的深度探究
  • Design Compiler:什么是代价函数(Cost Function)
  • HarmonyOS组件/模板集成创新活动-元服务小云体重管理引入案例(步骤条UI组件)
  • python赤道上空的大气环流剖面图(纬向-高度剖面)
  • 多级@JsonTypeInfo和@JsonSubTypes注解使用详解及场景分析
  • 剑指offer59_翻转单词顺序
  • Redis 命令总结
  • Docker三剑客
  • Docker 基于 Cgroups 实现资源限制详解【实战+源码】