⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rotation_4444.java

📁 基于j2me的任意角度图片旋转
💻 JAVA
字号:
package com.nokia;

import javax.microedition.lcdui.*;


/**
 * 
 * @author pandonix
 *	在原BB的Sprite.java基础上修改后的代码
 *	在此特别感谢BB共享的这段代码
 */
public class Rotation_4444 {

	/** 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 = 0xf;
	
	/**
	 * 主体的旋转算法
	 * @param cData
	 * @param w
	 * @param h
	 * @param x
	 * @param y
	 * @return
	 */
	private static final short project(short cData[], int w, int h, int x, int y) {
		int j3 = (x & 0xffff) >> 8;
		int k3 = (y & 0xffff) >> 8;
		
		int l3 = (256 - j3) * (256 - k3);
		int i4 = j3 * (256 - k3);
		int j4 = (256 - j3) * k3;
		int k4 = j3 * k3;
		int l2 = x >> 16;
		int i3 = y >> 16;
		l2 %= w;
		i3 %= h;
		i3 *= w;
		short word0 = cData[l2 + i3];
		short word2 = cData[(l2 + (i3 + w)) % (cData.length - 1)];
		l2 = ++l2 % w;
		short word1 = cData[l2 + i3];
		short word3 = cData[(l2 + (i3 + w)) % (cData.length - 1)];
		int l1 = word0 >> 12 & 0xf;
		int i2 = word1 >> 12 & 0xf;
		int j2 = word2 >> 12 & 0xf;
		int k2 = word3 >> 12 & 0xf;
		int a = 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 r = 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 g = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
		l1 = word0 & 0xf;
		i2 = word1 & 0xf;
		j2 = word2 & 0xf;
		k2 = word3 & 0xf;
		int b = l1 * l3 + i2 * i4 + j2 * j4 + k2 * k4 >> 16;
		return (short) ((a << 12) + (r << 8) + (g << 4) + b);
	}
	
	/**
	 * 原BB代码采用枚举来获取直径,由于CLDC1.1已经支持sqrt,所以这里直接计算。
	 * 在实际使用中,考虑到支持CLDC1.0的手机,建议先手动计算出直径,然后再次列举出来
	 */
	public static int getRadius(int _width,int _height)
	{
		return 198;
	}
	
	/**
	 * 旋转算法的主要实现
	 * @param _pixels 原图像的像素数组,像素采用TYPE_USHORT_4444_ARGB格式
	 * @param _width 
	 * @param _height
	 * @param _angle 旋转的角度(0~360)
	 * @return
	 */
	public static short[] getRotatePixels(short[] _pixels, int _width, int _height,int _radius,int _angle)
	{
        int newWidth = _radius;
        int newHeight = _radius;
        short[] newPixels = new short[newWidth * newHeight];
        int w = _width; //原来图片的宽度
        int h = _height;
        _width = newWidth;
        _height = newHeight;
        int r = _width / 2;
        int w2 = w / 2;
        int h2 = h / 2;
        int temp_sin = SIN_TABLE[_angle % 360] << 8; //sin
        int temp_cos = SIN_TABLE[(_angle + 90) % 360] << 8; //cos
        int idx = 0;
        int x1 = -r * temp_cos;
        int y1 = -r * temp_sin;
        for (int i = 0; i < newPixels.length; i++) {
          //alpha高位
          newPixels[i] = (short) (TRANSPARENT << 12);
        }
        
        for (int i = 0; i < _height; i++) {
          int x2 = -r * temp_cos;
          int y2 = -r * temp_sin;
          for (int j = 0; j < _width; j++) {
            int x3 = (x2 - y1 >> 16) + w2;
            int y3 = (x1 + y2 >> 16) + h2;
            //只处理在原图片大小范围之内的
            if (x3 >= 0 && y3 >= 0 && y3 < h && x3 < w) {
              newPixels[idx] = project(_pixels, w, h,(x2 - y1) + (w2 << 16),x1 + y2 + (h2 << 16));
            }
            x2 += temp_cos;
            y2 += temp_sin;
            idx++;
          }
          x1 += temp_cos;
          y1 += temp_sin;
          idx -= _width;
          idx += newWidth;
        }
        _height = h;
        _width = w;
		return newPixels;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -