java与Android本署一个平台。大部分技术可以移植。在java标准平台中引入Android NinePatch技术可以使其UI设计大大得到改善:

图片准备:

AndroidNinePatch技术介绍: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 {@code 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) */@Overrideprotected 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 opaquecontentPane.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() {@Overridepublic 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 {@code 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;}}@Overrideprotected 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 opaquecontentPane.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() {@Overridepublic void run() {createAndShowGUI();}});}}


使用的前2幅图(格式.png),效果如下:


不用我多说,就可以看出NinePatch技术的优势了。

更多相关文章

  1. android 使用xml selector设置按钮点击效果图片
  2. 【android编程】 第六讲-Android菜单实践题
  3. [转]Android(安卓)技术专题系列之九 -- 图形系统
  4. android ctl属性的实现过程
  5. Android(安卓)带有角标的imageview,类似于qq、微信未读消息提示效
  6. android sdk setup时出现:Failed to fetch URL
  7. 【Android(安卓)界面效果12】EditText中的多行输入问题
  8. Android中TextView中内容不换行的解决方法
  9. 用android:clipChildren来实现红心变大特效

随机推荐

  1. Tabhost中Activity绑定Service
  2. AutoCompleteTextView(自动完成文本框)
  3. android bluetooth
  4. android EditText 的键盘弹出(不弹出)坑爹
  5. Android中的Binder机制一(实名Binder)
  6. Android中的文本框,图片以及点击事件的设
  7. Android(安卓)8.1 中Systemui中的常见修
  8. Android编译过程详解(二)
  9. Android(安卓)- 修改最小SDK版本(minSdkV
  10. Android属性之build.prop生成过程分析