在android中,手机的屏幕大少为320*480,原点坐标在左上角(0,0),在绘制图形时一定要非常清楚要绘制的图形对坐标转换的关系。

简单的图形如,直线,画点,画圆大家可能不会有什么问题,但如果遇到复杂图形或图形旋转等情况时,比较游戏俄罗斯方块中,对不同组合的方块进行变换时,涉及到很多坐标变换情况,如果不是完全清楚的话,很难编码了。

如下:


引用 public int[][] rotateStone(int[][] currentPosition) {
int[][] newPosition = new int[4][2];
int x, y;
switch (blockID) {
case 2:
if (rotatePosition == 0) {//一横变一竖
x = currentPosition[3][0];
y = currentPosition[3][1];
newPosition[0][0] = x - 3;
newPosition[0][1] = y;
newPosition[1][0] = x - 2;
newPosition[1][1] = y;
newPosition[2][0] = x - 1;
newPosition[2][1] = y;
newPosition[3][0] = x;
newPosition[3][1] = y;
rotatePosition = 1;
} else {//反之
x = currentPosition[3][0];
y = currentPosition[3][1];
newPosition[0][0] = x;
newPosition[0][1] = y - 3;
newPosition[1][0] = x;
newPosition[1][1] = y - 2;
newPosition[2][0] = x;
newPosition[2][1] = y - 1;
newPosition[3][0] = x;
newPosition[3][1] = y;
rotatePosition = 0;
}
return newPosition;
case 3:
if (rotatePosition == 0) {//左T
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x;
newPosition[0][1] = y - 1;
newPosition[1][0] = x;
newPosition[1][1] = y + 1;
newPosition[2][0] = x;
newPosition[2][1] = y;
newPosition[3][0] = x + 1;
newPosition[3][1] = y;
rotatePosition = 1;
} else if (rotatePosition == 1) {//上T
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x;
newPosition[0][1] = y + 1;
newPosition[1][0] = x - 1;
newPosition[1][1] = y;
newPosition[2][0] = x;
newPosition[2][1] = y;
newPosition[3][0] = x + 1;
newPosition[3][1] = y;
rotatePosition = 2;
} else if (rotatePosition == 2) {//右T
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x;
newPosition[0][1] = y - 1;
newPosition[1][0] = x - 1;
newPosition[1][1] = y;
newPosition[2][0] = x;
newPosition[2][1] = y;
newPosition[3][0] = x;
newPosition[3][1] = y + 1;
rotatePosition = 3;
} else if (rotatePosition == 3) {//下T
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x - 1;
newPosition[0][1] = y;
newPosition[1][0] = x + 1;
newPosition[1][1] = y;
newPosition[2][0] = x;
newPosition[2][1] = y;
newPosition[3][0] = x;
newPosition[3][1] = y - 1;
rotatePosition = 0;
}
return newPosition;
case 4:
if (rotatePosition == 0) {//上右7
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x;
newPosition[0][1] = y;
newPosition[1][0] = x + 1;
newPosition[1][1] = y;
newPosition[2][0] = x + 2;
newPosition[2][1] = y;
newPosition[3][0] = x + 2;
newPosition[3][1] = y - 1;
rotatePosition = 1;
} else if (rotatePosition == 1) {//左上T
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x - 1;
newPosition[0][1] = y - 2;
newPosition[1][0] = x;
newPosition[1][1] = y - 2;
newPosition[2][0] = x;
newPosition[2][1] = y - 1;
newPosition[3][0] = x;
newPosition[3][1] = y;
rotatePosition = 2;
} else if (rotatePosition == 2) {//下左7
x = currentPosition[1][0];
y = currentPosition[1][1];
newPosition[0][0] = x - 2;
newPosition[0][1] = y + 1;
newPosition[1][0] = x - 2;
newPosition[1][1] = y;
newPosition[2][0] = x - 1;
newPosition[2][1] = y;
newPosition[3][0] = x;
newPosition[3][1] = y;
rotatePosition = 3;
} else if (rotatePosition == 3) {//左上
x = currentPosition[1][0];
y = currentPosition[1][1];
newPosition[0][0] = x;
newPosition[0][1] = y;
newPosition[1][0] = x;
newPosition[1][1] = y + 1;
newPosition[2][0] = x;
newPosition[2][1] = y + 2;
newPosition[3][0] = x + 1;
newPosition[3][1] = y + 2;
rotatePosition = 0;
}
return newPosition;
case 5:
if (rotatePosition == 0) {
x = currentPosition[1][0];
y = currentPosition[1][1];
newPosition[0][0] = x - 2;
newPosition[0][1] = y - 1;
newPosition[1][0] = x - 1;
newPosition[1][1] = y - 1;
newPosition[2][0] = x;
newPosition[2][1] = y - 1;
newPosition[3][0] = x;
newPosition[3][1] = y;
rotatePosition = 1;
} else if (rotatePosition == 1) {
x = currentPosition[0][0];
y = currentPosition[0][1];
newPosition[0][0] = x;
newPosition[0][1] = y;
newPosition[1][0] = x + 1;
newPosition[1][1] = y;
newPosition[2][0] = x;
newPosition[2][1] = y + 1;
newPosition[3][0] = x;
newPosition[3][1] = y + 2;
rotatePosition = 2;
} else if (rotatePosition == 2) {
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x;
newPosition[0][1] = y;
newPosition[1][0] = x;
newPosition[1][1] = y + 1;
newPosition[2][0] = x + 1;
newPosition[2][1] = y + 1;
newPosition[3][0] = x + 2;
newPosition[3][1] = y + 1;
rotatePosition = 3;
} else if (rotatePosition == 3) {
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x;
newPosition[0][1] = y;
newPosition[1][0] = x + 1;
newPosition[1][1] = y;
newPosition[2][0] = x + 1;
newPosition[2][1] = y - 1;
newPosition[3][0] = x + 1;
newPosition[3][1] = y - 2;
rotatePosition = 0;
}
return newPosition;
case 6:
if (rotatePosition == 0) {
x = currentPosition[2][0];
y = currentPosition[2][1];
newPosition[0][0] = x - 1;
newPosition[0][1] = y;
newPosition[1][0] = x;
newPosition[1][1] = y + 1;
newPosition[2][0] = x;
newPosition[2][1] = y;
newPosition[3][0] = x + 1;
newPosition[3][1] = y + 1;
rotatePosition = 1;
} else {
x = currentPosition[2][0];
y = currentPosition[2][1];

newPosition[0][0] = x;
newPosition[0][1] = y - 1;
newPosition[1][0] = x - 1;
newPosition[1][1] = y;
newPosition[2][0] = x;
newPosition[2][1] = y;
newPosition[3][0] = x - 1;
newPosition[3][1] = y + 1;
rotatePosition = 0;
}
return newPosition;
case 7:
if (rotatePosition == 0) {
x = currentPosition[2][0];
y = currentPosition[2][1];

newPosition[0][0] = x - 1;
newPosition[0][1] = y - 1;
newPosition[1][0] = x - 1;
newPosition[1][1] = y;
newPosition[2][0] = x;
newPosition[2][1] = y;
newPosition[3][0] = x;
newPosition[3][1] = y + 1;
rotatePosition = 1;
} else {
x = currentPosition[2][0];
y = currentPosition[2][1];

newPosition[0][0] = x + 1;
newPosition[0][1] = y;
newPosition[1][0] = x - 1;
newPosition[1][1] = y + 1;
newPosition[2][0] = x;
newPosition[2][1] = y;
newPosition[3][0] = x;
newPosition[3][1] = y + 1;
rotatePosition = 0;
}
return newPosition;
default:
return currentPosition;

}

}

以上代码就是俄罗斯方块中的图形变换。

这是其一,另外还要找准其旋转坐标点,方程变换。

(x,y), (x+1,y),(x+2, y)即为向屏幕x轴移动,(x,y),(x,y+1),(x,y+2)即为向屏幕y轴移动,其它一此类推。

更多相关文章

  1. android 触摸点在屏幕中的坐标与bitmap在屏幕中坐标的比较
  2. android中按钮随单击变换
  3. Android(安卓)UI秘笈:谨记该做什么不该做什么 - [图形界面]
  4. Android(安卓)4.0的图形硬件加速及绘制技巧(1)
  5. 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!
  6. Android图形图画学习(1)
  7. Android中图表AChartEngine学习使用与例子
  8. [Android] 滑动操作的原理及处理
  9. AChartEngine中大饼图

随机推荐

  1. Android(安卓)Q AppCompactor and LowMem
  2. android 下的webview 设置多点触控放大
  3. Android(安卓)SeekBar
  4. android 选择图片(从手机照相机或手机图
  5. android activity 设置为单点触摸
  6. Android(安卓)ListView滑动加载
  7. RN 打包流程
  8. android 根据Uri获取文件绝对路径
  9. 根据请求头跳转判断Android&iOS
  10. 调试 android jni 程序