java引入Android NinePatch技术的意义

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 {@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)
	 */
	@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 {@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;
		}
	}

	@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技术的优势了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值