Lwuit实现九宫图特效程序

最近在网上看到很多九宫图的程序说明,但大多数是转载的,一些知识点的地方对于初学者来说还是比较难懂的,我在原来的程序上修改了一下,也加了一些注释,希望对大家有所帮助吧!

程序截图                              
Lwuit实现九宫图特效程序      Lwuit实现九宫图特效程序

package com.mopietek;

import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.animations.Transition;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;

public class UIDemoMIDlet extends MIDlet implements ActionListener{

	public static Command exitCommand = new Command("exit",1);
	//设置九宫图图标的名称
	private  String[] button_name = new String[]{"Image 1","Image 2","Image 3","Image 4","Image 5","Image 6","Image 7","Image 8","Image 9"};
	
	private static Transition componentTransition;
	private  static Form mainForm; //主窗体
	
	private  int cols;
	private  int elementWidth;
	
	public static Transition getComponentTransition() {
		return componentTransition;
	}

	public static void setComponentTransition(Transition componentTransition) {
		
		UIDemoMIDlet.componentTransition = componentTransition;
		//设置主窗体动画效果为false
		mainForm.setSmoothScrolling(false);
	}

	protected void startApp() throws MIDletStateChangeException {
		
		Display.init(this);
		try {
			Resources res = Resources.open("/resources.res");
			MainForm(res);
		} catch (IOException e) {
			e.printStackTrace();
			Dialog.show("Exception", e.getMessage(), "OK",null);
		}
	}
	
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	public void MainForm(Resources r){
		
		UIManager.getInstance().setThemeProps(r.getTheme("businessTheme"));
		mainForm = new Form("九宫图主窗体");
		//主窗体的宽度
		int width = Display.getInstance().getDisplayWidth();
		elementWidth = 0;
		
		//实现按钮3D翻转效果
		mainForm.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 500));
		mainForm.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 500));
		UIDemoMIDlet.setComponentTransition(Transition3D.createCube(400, false));
		
		//定义按钮选中时的图片
		Image [] selectedImages = new Image[button_name.length];
		//定义未m被选中时的图片
		Image [] unselectedImages = new Image[button_name.length];
		
		for(int i=0;i<button_name.length;i++){
			// 读取选中时的图片
			selectedImages[i] = r.getImage(button_name[i]);
			//读取未被选中时的图片
			unselectedImages[i] = r.getImage(button_name[i]);
			
//			final Button button = new Button(button_name[i],unselectedImages[i]){
			final Button button = new Button("图标"+(i+1),unselectedImages[i]){
				//当鼠标按下时图标变小的动画效果
				public Image getPressedIcon(){
					Image i = getIcon();
					//按钮图标缩小为原来的0.8倍
					return i.scaled((int) (i.getWidth()*0.8), (int)(i.getHeight()*0.8));
				}
				
			};
			//设置按钮翻转
			button.setRolloverIcon(selectedImages[i]);
			//得到按钮的风格
			Style s = button.getStyle();
			s.setBorder(null);
			s.setBgTransparency(0);
			s.setBgColor(0xffffff);
			button.setAlignment(Label.CENTER); //对齐方式
			button.setTextPosition(Label.BOTTOM);
			mainForm.addComponent(button);
			button.addActionListener(this);
			
			//按钮焦点处理,用于实现按钮特效
			button.addFocusListener(new FocusListener(){
                 //按钮得到焦点时的特效(事件)处理
				public void focusGained(Component cmp) {
					
					if(componentTransition != null){
						mainForm.replace(button, button, componentTransition);
					}
				}
				//按钮失去焦点时的特效(事件)处理
				public void focusLost(Component arg0) {
					
				}
			
			});
			
			elementWidth = Math.max(button.getPreferredW(), elementWidth);
		}
		
		cols = width / elementWidth;
		int rows = button_name.length / cols;
		mainForm.setLayout(new GridLayout(rows,cols));
		mainForm.addCommand(exitCommand);
		
		mainForm.setCommandListener(this);
		mainForm.show();
		
		
	}
	
	

	public static Form getMainForm() {
		return mainForm;
	}

	public static void setMainForm(Form mainForm) {
		UIDemoMIDlet.mainForm = mainForm;
	}

	public void actionPerformed(ActionEvent evt) {

		if(evt.getCommand() == exitCommand){
			this.notifyDestroyed();
		}
		
	}

}


关于资源文件可以在附加中下载
LwuitDemo7.jar文件是实例,大家可以下载测试一下,看看有哪些款式机子不支持,小弟在此谢过各位啦!

你可能感兴趣的:(sun)