当前位置: 首页 > 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.lryc.cn/news/586788.html

相关文章:

  • Docker 搭建本地Harbor私有镜像仓库
  • SpringBoot单元测试类拿不到bean报空指针异常
  • 从架构到代码:飞算JavaAI电商订单管理系统技术解构
  • 决策树的相关理论学习
  • FusionOne HCI 23 超融合实施手册(超聚变超融合)
  • 【C++】多线程同步三剑客介绍
  • 代码随想录算法训练营第十七天
  • 【C++】第十五节—一文详解 | 继承
  • JVM 垃圾收集算法全面解析
  • DC-DC变换器最基本拓扑 -Buck电路和Boost电路
  • ROS2---NodeOptions
  • MacOS使用Multipass快速搭建轻量级k3s集群
  • mac上BRPC的CMakeLists.txt优化:解决Protobuf路径问题
  • TensorFlow深度学习实战(24)——变分自编码器详解与实现
  • Vue 3 动态ref问题
  • 封装---统一封装处理页面标题
  • C++模版编程:类模版与继承
  • Qt 3D模块加载复杂模型
  • vue应用如何实现在 A 标签页登出,希望 B 标签页也自动感知并退出登录
  • 语音识别的速度革命:从 Whisper 到 Whisper-CTranslate2,我经历了什么?
  • 数据库3.0
  • HarmonyOS-ArkUI Web控件基础铺垫1-HTTP协议-数据包内容
  • EPLAN多项目并行,电气设计许可如何不浪费?
  • (S4)Efficiently Modeling Long Sequences with Structured State Spaces论文精读(逐段解析)
  • ReAct论文解读(1)—什么是ReAct?
  • 基于YOLOv11的无人机目标检测实战(Windows环境)
  • Spring Cloud Gateway 实战指南
  • 力扣经典算法篇-21- 两数之和 II - 输入有序数组(固定一端 + 二分查找法,双指针法)
  • MongoDB性能优化实战指南:原理、实践与案例
  • git实际工作流程