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)得到如下效果:

java引入Android NinePatch技术的意义_第1张图片java引入Android NinePatch技术的意义_第2张图片

如果使用传统的双线性插值进行图像缩放,我们来对比下效果:

双线性插值应用代码:

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),效果如下:

java引入Android NinePatch技术的意义_第3张图片

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

更多相关文章

  1. android 使用xml selector设置按钮点击效果图片
  2. Android关于SD卡的读写操作及固定图片大小
  3. [转]Android 技术专题系列之九 -- 图形系统
  4. C# mono android 图片上传进度条实现
  5. android 技术
  6. 分享android技术牛人博客
  7. Android获取本地图片之ACTION_GET_CONTENT与ACTION_PICK区别
  8. 关于文字颜色/图片背景---selector状态列表
  9. [Android] 代码实现按钮/图片自旋转(中心旋转)

随机推荐

  1. Android(安卓)动态加载布局文件
  2. Android中添加自定义按键 ---- 非标准做
  3. Android(安卓)应用程式的基本档案结构
  4. Chris:怎样成为一名Android应用开发者
  5. Android(安卓)Picasso 图片加载库基础使
  6. Android开发学习总结(二)——使用Android
  7. [置顶] Android大事记(不断更新中)
  8. 【腾讯Bugly干货分享】Android(安卓)Link
  9. Android事件分发机制完全解析,带你从源码
  10. Android(安卓)studio 分配内存设置方法