java与Android本署一个平台。大部分技术可以移植。在java标准平台中引入Android NinePatch技术可以使其UI设计大大得到改善:
图片准备:
Android NinePatch技术介绍:http://developer.android.com/tools/help/draw9patch.html
附NinePatch jar包下载:http://download.csdn.net/detail/gaowen_han/5204821
应用NinePatch技术代码:
package com.han;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Rectangle;import java.io.IOException;import java.io.InputStream;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;import com.android.ninepatch.NinePatch;@SuppressWarnings("serial")public class NinePatchTest extends JPanel { NinePatch ninePatch; /** * This constructor which serves as a content pane uses a default flow * layout and a double-buffering strategy. */ public NinePatchTest() { ninePatch = loadNinePatch("/images/content_bg1.9.png"); add(new JButton("Button 1")); add(new JButton("Button 2")); add(new JButton("Button 3")); add(new JButton("Button 4")); } /** * @param path * - the image path. * @return an NinePatch object, or [email protected] null} if the given path is not * valid or an error occurs during loading. */ private NinePatch loadNinePatch(String path) { InputStream stream = this.getClass().getResourceAsStream(path); if (stream != null) { try { return NinePatch.load(stream, true, false); } catch (IOException e) { System.err.println("An error occurs during loading."); e.printStackTrace(); return null; } } else { System.err.println("Couldn't find the file: " + path); return null; } } /** * To improve the repaint speed, the code block contained in * paintComponent() must be able to be executed quickly. For example, we * usually put the load image code out of the paintComponent() for rapid UI * update. * * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) */ @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle clip = g2.getClipBounds(); ninePatch.draw(g2, clip.x, clip.y, clip.width, clip.height); } private static void createAndShowGUI() { // Create and set up the window. JFrame frame = new JFrame("NinePatch test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set up the content pane. JPanel contentPane = new NinePatchTest(); contentPane.setOpaque(true); // content pane must be opaque contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); frame.setContentPane(contentPane); // Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { // Schedule a job for the EDT: // Creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); }}
利用后2幅图(格式.9.png)得到如下效果:
如果使用传统的双线性插值进行图像缩放,我们来对比下效果:
双线性插值应用代码:
package com.han;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Rectangle;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.InputStream;import javax.imageio.ImageIO;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;@SuppressWarnings("serial")public class NinePatchTestForCompare extends JPanel { BufferedImage image; /** * This constructor uses a default flow layout and a double-buffering * strategy. */ public NinePatchTestForCompare() { image = loadImage("/images/content_bg2.png"); add(new JButton("Button 1")); add(new JButton("Button 2")); add(new JButton("Button 3")); add(new JButton("Button 4")); } /** * @param path * - the image path. * @return an BufferedImage object, or [email protected] null} if the given path is not * valid or an error occurs during loading. */ private BufferedImage loadImage(String path) { InputStream stream = this.getClass().getResourceAsStream(path); if (stream != null) { try { return ImageIO.read(stream); } catch (IOException e) { System.err.println("An error occurs during loading."); e.printStackTrace(); return null; } } else { System.err.println("Couldn't find the file: " + path); return null; } } @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle clip = g2.getClipBounds(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.drawImage(image, clip.x, clip.y, clip.width, clip.height, null); } private static void createAndShowGUI() { // Create and set up the window. JFrame frame = new JFrame("NinePatchTestForCompare"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set up the content pane. JPanel contentPane = new NinePatchTestForCompare(); contentPane.setOpaque(true); // content pane must be opaque contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); frame.setContentPane(contentPane); // Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { // Schedule a job for the EDT: // Creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); }}
使用的前2幅图(格式.png),效果如下:
不用我多说,就可以看出NinePatch技术的优势了。