分类:
2008-09-10 10:08:05
代码如下:
package com.xx;
import javax.microedition.lcdui.Graphics;
/*
* 图片任意角度翻转算法
* 同时实现了Nokia特有API
* author. BB
* Sprite.java
*
*
*/
public class Sprite {
/** SIN TABLE **/
public final static int SIN_TABLE[] ={
0, 4, 8, 13, 17, 22, 26, 31, 35, 39,
44, 48, 53, 57, 61, 65, 70, 74, 78, 83,
87, 91, 95, 99, 103, 107, 111, 115, 119, 123,
127, 131, 135, 138, 142, 146, 149, 153, 156, 160,
163, 167, 170, 173, 177, 180, 183, 186, 189, 192,
195, 198, 200, 203, 206, 208, 211, 213, 216, 218,
220, 223, 225, 227, 229, 231, 232, 234, 236, 238,
239, 241, 242, 243, 245, 246, 247, 248, 249, 250,
251, 251, 252, 253, 253, 254, 254, 254, 254, 254,
255, 254, 254, 254, 254, 254, 253, 253, 252, 251,
251, 250, 249, 248, 247, 246, 245, 243, 242, 241,
239, 238, 236, 234, 232, 231, 229, 227, 225, 223,
220, 218, 216, 213, 211, 208, 206, 203, 200, 198,
195, 192, 189, 186, 183, 180, 177, 173, 170, 167,
163, 160, 156, 153, 149, 146, 142, 138, 135, 131,
127, 123, 119, 115, 111, 107, 103, 99, 95, 91,
87, 83, 78, 74, 70, 65, 61, 57, 53, 48,
44, 39, 35, 31, 26, 22, 17, 13, 8, 4,
0, -4, -8, -13, -17, -22, -26, -31, -35, -39,
-44, -48, -53, -57, -61, -65, -70, -74, -78, -83,
-87, -91, -95, -99, -103, -107, -111, -115, -119, -123,
-127, -131, -135, -138, -142, -146, -149, -153, -156, -160,
-163, -167, -170, -173, -177, -180, -183, -186, -189, -192,
-195, -198, -200, -203, -206, -208, -211, -213, -216, -218,
-220, -223, -225, -227, -229, -231, -232, -234, -236, -238,
-239, -241, -242, -243, -245, -246, -247, -248, -249, -250,
-251, -251, -252, -253, -253, -254, -254, -254, -254, -254,
-255, -254, -254, -254, -254, -254, -253, -253, -252, -251,
-251, -250, -249, -248, -247, -246, -245, -243, -242, -241,
-239, -238, -236, -234, -232, -231, -229, -227, -225, -223,
-220, -218, -216, -213, -211, -208, -206, -203, -200, -198,
-195, -192, -189, -186, -183, -180, -177, -173, -170, -167,
-163, -160, -156, -153, -149, -146, -142, -138, -135, -131,
-127, -123, -119, -115, -111, -107, -103, -99, -95, -91,
-87, -83, -78, -74, -70, -65, -61, -57, -53, -48,
-44, -39, -35, -31, -26, -22, -17, -13, -8, -4
};
/** 透明 **/
public static int TRANSPARENT = 0;
/** 不透明 **/
public static int OPAQUE = 15;
/**
* 像素值
* 包含动画帧,主下标标识动画帧数,副下标的值代表像素值
*/
public short pixels[][];
/*
* 精灵的宽度
* 该版本默认精灵各个帧的宽度一样大小
*/
public short width;
/*
* 精灵的高度
* 该版本默认精灵各个帧的高度一样大小
*/
public short height;
/*
* 动画帧数
*/
public byte numOfFrame;
/*
* 当前动画帧数
*/
public int curFrame;
/*
* 构造函数
* 确保传入的各个值为合理的值
* 使用的话请自己做判断
* @param _pixels 动画的像素值
* @param _width 宽度
* @param _height 高度
*/
public Sprite(short[][] _pixels, short _width, short _height){
pixels = _pixels;
width = _width;
height = _height;
numOfFrame = (byte)_pixels.length;
curFrame = 0;
}
/*
* 构造函数
*/
public Sprite(){
}
/*
* 投影
* @param cData
* @param l
* @param i1
* @param j1
* @param k1
* @return
*/
private final short project(short cData[], int l, int i1, int j1, int k1) {
int j3 = (j1 & 0xffff) >> 8;
int k3 = (k1 & 0xffff) >> 8;
int l3 = (256 - j3) * (256 - k3);
int i4 = j3 * (256 - k3);
int j4 = (256 - j3) * k3;
int k4 = j3 * k3;
int l2 = j1 >> 16;
int i3 = k1 >> 16;
l2 %= l;
i3 %= i1;
i3 *= l;
short word0 = cData[l2 + i3];
short word2 = cData[(l2 + (i3 + l)) % (cData.length - 1)];
l2 = ++l2 % l;
short word1 = cData[l2 + i3];
short word3 = cData[(l2 + (i3 + l)) % (cData.length - 1)];
int l1 = word0 >> 12 & 0xf;
int i2 = word1 >> 12 & 0xf;
int j2 = word2 >> 12 & 0xf;
int k2 = word3 >> 12 & 0xf;
int k5 = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
l1 = word0 >> 8 & 0xf;
i2 = word1 >> 8 & 0xf;
j2 = word2 >> 8 & 0xf;
k2 = word3 >> 8 & 0xf;
int l4 = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
l1 = word0 >> 4 & 0xf;
i2 = word1 >> 4 & 0xf;
j2 = word2 >> 4 & 0xf;
k2 = word3 >> 4 & 0xf;
int i5 = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
l1 = word0 & 0xf;
i2 = word1 & 0xf;
j2 = word2 & 0xf;
k2 = word3 & 0xf;
int j5 = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
return (short) ((k5 << 12) + (l4 << 8) + (i5 << 4) + j5);
}
[1]