?? imageset.java
字號:
package com.thinkenjoy.tools;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
//#if NOK_7260 || NOK_7210 || NOK_6101 || NOK_6230i || NOK_7370 || NOK_QD
//# import com.nokia.mid.ui.DirectUtils;
//#endif
/**
*
* 圖片幀的容器
* 此類主要是負責(zé)一些圖片處理方面的事務(wù)
*/
public class ImageSet {
private int totalStates; // 只被addState方法調(diào)用
private Image[][] stateFrames;
private int[] stateAnimTime, stateFrameWidth, stateFrameHeight;
/**
* 構(gòu)造函數(shù),初始化相應(yīng)數(shù)組
*
* @param numStates
*/
public ImageSet(int numStates) {
stateAnimTime = new int[numStates];
stateFrameWidth = new int[numStates];
stateFrameHeight = new int[numStates];
stateFrames = new Image[numStates][];
}
/**
* 添加新的圖片序列狀態(tài),
* @param frames
* 圖片幀數(shù)組
* @param animTime
* 整個圖片幀數(shù)組的顯示時間
*/
public final void addState(Image frames[], int animTime) {
int state = totalStates++;
if (state >= stateFrames.length) {
// 擴大狀態(tài)的數(shù)量
stateAnimTime = Tools.expandArray(stateAnimTime, 1);
stateFrameWidth = Tools.expandArray(stateFrameWidth, 1);
stateFrameHeight = Tools.expandArray(stateFrameHeight, 1);
stateFrames = Tools.expandArray(stateFrames, 1);
}
stateAnimTime[state] = animTime;
stateFrameWidth[state] = frames[0].getWidth();
stateFrameHeight[state] = frames[0].getHeight();
stateFrames[state] = frames;
}
/**
* 獲得指定狀態(tài)下的圖片幀數(shù)量
*
* @param state
* 某一狀態(tài)
* @return 指定狀態(tài)下的圖片幀數(shù)量
*/
public final int getTotalFrames(int state) {
return stateFrames[state].length;
}
/**
* 獲得相應(yīng)狀態(tài)的持續(xù)總時間 注:并不是每幀的延遲時間
*
* @param state
* 相應(yīng)狀態(tài)
* @return 持續(xù)總時間
*/
public final int getAnimTime(int state) {
return stateAnimTime[state];
}
/**
* 獲得每幀的延遲時間
*
* @param state
* 相應(yīng)狀態(tài)
* @return 每幀的延遲時間
*/
public final int getAnimTimePerFrame(int state) {
return stateAnimTime[state] / stateFrames[state].length;
}
/**
* 在畫布上畫相應(yīng)狀態(tài)的相應(yīng)幀
*
* @param target
* 相應(yīng)畫布
* @param state
* 相應(yīng)狀態(tài)
* @param frame
* 相應(yīng)幀
* @param targetX
* 繪制幀的相應(yīng)X坐標(biāo)
* @param targetY
* 繪制幀的相應(yīng)Y坐標(biāo)
*/
public final void draw(Graphics target, int state, int frame, int targetX,
int targetY) {
if (stateFrames[state][frame] != null) {
target.drawImage(stateFrames[state][frame], targetX, targetY,
Tools.GRAPHICS_TOP_LEFT);
}
}
/**
* 提取出相應(yīng)狀態(tài)相應(yīng)幀的圖片
*
* @param state
* 相應(yīng)狀態(tài)
* @param frame
* 相應(yīng)幀
* @return
*/
public final Image getFrame(int state, int frame) {
return stateFrames[state][frame];
}
/**
* 得到一張圖片的相應(yīng)裁剪區(qū)域
*
* @param filename
* 圖片文件的名字
* @param originX
* 裁剪區(qū)域的起始X坐標(biāo)
* @param originY
* 裁剪區(qū)域的起始Y坐標(biāo)
* @param width
* 裁剪區(qū)域的寬度
* @param height
* 裁剪區(qū)域的高度
* @return 裁剪區(qū)域的圖片
*/
public final static Image loadClippedImage(String filename, int originX,
int originY, int width, int height) {
try {
// 載入整張圖片
Image fileImage = Image.createImage(filename);
// 用getImageRegion方法用以提取出想要的圖片
return getImageRegion(fileImage, originX, originY, width, height);
}
catch (IOException ioe) {
System.out.println("can't load file: " + filename);
return null;
}
}
/**
* 得到一張圖片的相應(yīng)裁剪區(qū)域(從相應(yīng)的位置起裁剪原始圖片的剩余部分)
*
* @param filename
* 圖片文件的名字
* @param originX
* 裁剪區(qū)域的起始X坐標(biāo)
* @param originY
* 裁剪區(qū)域的起始Y坐標(biāo)
* @return 裁剪區(qū)域的圖片
*/
public final static Image loadClippedImage(String filename, int originX,
int originY) {
try {
// 載入整張圖片
Image fileImage = Image.createImage(filename);
// 如果只是完整地載入一張圖片
if (originX == 0 && originY == 0)
return fileImage;
// 得到我們想要的圖片區(qū)域
return getImageRegion(fileImage, originX, originY, fileImage
.getWidth(), fileImage.getHeight());
}
catch (IOException ioe) {
System.out.println("can't load file: " + filename);
return null;
}
}
/**
* 在原始圖片上剪切出一張新圖片
*
* @param source 原始圖片
* @param x
* 新圖片在原始圖片上的起始X坐標(biāo)
* @param y
* 新圖片在原始圖片上的起始Y坐標(biāo)
* @param width
* 新圖片的寬度
* @param height
* 新圖片的高度
* @return 僅僅包含原始圖片部分的新圖片
*/
public final static Image getImageRegion(Image source, int x, int y,
int width, int height) {
//#ifdef debug
//# if (x + width > source.getWidth() || y + height > source.getHeight())
//# System.out.println("Warning: attempting extract using (" + x + ","
//# + y + "," + width + "," + height + ") when image is " + "("
//# + source.getWidth() + "," + source.getHeight() + ")");
//#endif
// 為新圖片創(chuàng)建一張空白圖片
//#if NOK_7260 || NOK_7210 || NOK_6101 || NOK_6230i || NOK_7370
//# Image result = DirectUtils.createImage(width, height, 0);
//# // 在空白圖片上根據(jù)相應(yīng)位置繪制
//# result.getGraphics().drawImage(source, -x, -y, Tools.GRAPHICS_TOP_LEFT);
//#else
Image result = Image.createImage(source, x, y, width, height, 0x00000000);
//#endif
return result;
}
/**
* 分離幀
*
* @param sourceImage
* 原始合并圖片
* @param sourceX
* 原始圖片的分離起始X坐標(biāo)
* @param sourceY
* 原始圖片的分離起始Y坐標(biāo)
* @param framesWide
* 橫向的分離數(shù)量
* @param framesHigh
* 縱向的分離數(shù)量
* @param frameWidth
* 每幀的寬度
* @param frameHeight
* 每幀的高度
* @return 存儲每幀圖片的數(shù)組
*/
public final static Image[] extractFrames(Image sourceImage, int sourceX,
int sourceY, int framesWide,
int framesHigh, int frameWidth,
int frameHeight) {
// 存儲每幀圖片的數(shù)組
Image[] frames = new Image[framesWide * framesHigh];
int frameCount = 0;
for (int fy = 0; fy < framesHigh; fy++)
for (int fx = 0; fx < framesWide; fx++)
frames[frameCount++] = getImageRegion(sourceImage,
sourceX + (fx * frameWidth),
sourceY + (fy * frameHeight),
frameWidth, frameHeight);
return frames;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -