Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Recommended reading:
Today, we offer three CocosCreator three common adaptation mode, ado, directly on the code
/**************************
*
* @author:shirln
* @time:2019.7.15
*/
cls.Ground = cc.Class({
extends: cc.Component,
statics: {
// 所有UserInfo节点列表
pool: [],
///
// update
},
properties: {
mode: { default: 1 },
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start() {
gm.Ground = this;
// 视口大小
var csz = cc.view.getCanvasSize();
// 设计分辨率
var des = new cc.Vec2(1080, 1920);
// 进行分辨率适配
cc.view.setDesignResolutionSize(des.x, des.y, cc.ResolutionPolicy.SHOW_ALL);
// fitWidth
var sx = csz.width / des.x;
// fitHeight
var sy = csz.height / des.y;
// show_all
var min = Math.min(sx, sy);
// EXACT_FIT
var max = Math.max(sx, sy);
/// show_all下缩放背景
if (this.mode == 1) {
// 等比例黑边
this.node.setScale(max / min, max / min);
}
else if (this.mode == 2) {
// 拉伸填充
this.node.setScale(sx == min ? 1 : max / min, sy == min ? 1 : max / min);
}
else if (this.mode == 3) {
// 等比例裁剪
this.node.setScale(max / max, max / max);
}
},
// update (dt) {},
});
Usage: Add the script on the need to adapt the panel, and assign adaptation mode (1,2,3), one of three
note here is that when you use ScrollView components may occur bug: scrollView species than the content will not be cut, such as a red frame in the range of ScrollView:
Workaround: Add the widget to add a component view, and assign its target ScrollView, as follows: