鸿蒙有很多布局组件,但我们不需要他们的自适应布局,因为这只适合软件不适合游戏,所以布局最好只使用DependentLayout(相对布局),组件只使用Image当我们的精灵。
层级:
鸿蒙没有层级思想,只有先后加载,后加载的层级大于前面的层级。但有父子的思想。所以我们可以提前设置层级。比如下图这个意思
ohos:id="$+id:game_root"
ohos:width="match_parent"
ohos:height="match_parent">
ohos:id="$+id:layout1"
ohos:width="match_parent"
ohos:height="match_parent"/>
ohos:id="$+id:layout2"
ohos:width="match_parent"
ohos:height="match_parent"/>
这样通过不同的id层,加载不同层的Image(精灵),达到设置层级的目的
裁切:
裁切可以通过该不同的手段等,颜色混合也可以,但目前我还没在鸿蒙上使用颜色混合,所以通过“马赛克原理”,直接从图片本身下手,,直接从图片本身动手,我们对获取到的图片的像素码(图片保存在计算的真正的值)直接进行修改,直接获取到裁切过后的图片public static PixelMap getPixelMap(Context context, int picId, int minX, int minY, int width, int height) throws Exception {
String drawingPath = context.getResourceManager().getMediaPath(picId);
Resource a***t = context.getResourceManager().getRawFileEntry(drawingPath).openRawFile();
ImageSource source = ImageSource.create(a***t, getSrcOpts());
getDecodingOptions().desiredRegion = new Rect(minX, minY, width, height);
// getDecodingOptions().desiredSize = new Size(width, height);
return source.createPixelmap(getDecodingOptions());
}
帧动画:
很简单,就是根据定时去换图,当然这个游戏必要的一部分,也是为了延伸下面的移动@Override
protected void onUpdate(double dt) {
apertureTime += dt;
if (apertureTime >= 50) { // 切到下一帧
apertureTime -= 50;
imageAperturep.setPixelMap(listAperturep.get(apertureIndex));
}
}
移动之贝塞尔:
通过起始点,终点,和控制点,,,计算出贝塞尔的轨迹集合,然后在通过帧动画一样的形式设置左边距和上边距,达到运动出贝塞尔的轨迹,,,(同理可以运动花里胡哨的轨迹,只不过要通过运算)// 贝尔曲线点集合
public static List> getBezier(int x1, int y1, int x2, int y2, int x3, int y3, float t){
List> positionList = new ArrayList<>();
int[][] keyPointP = new int[][]{
{x1,y1},
{x2,y2},
{x3,y3}
};
int x, y;
for (double k = t; k <= 1 + t; k += t) {
double r = 1 - k;
x = (int) (Math.pow(r, 2) * keyPointP[0][0]+ 2 * k * r * keyPointP[1][0] + Math.pow(k, 2) * keyPointP[2][0]);
y = (int) (Math.pow(r, 2) * keyPointP[0][1] + 2 * k * r * keyPointP[1][1] + Math.pow(k, 2) * keyPointP[2][1]);
Map position = new HashMap<>();
position.put("x",x);
position.put("y",y);
positionList.add(position);
}
return positionList;
}