我有一个view class
要为我的activity
. 但是它的启动很长(大部分代码,例如加载位图都放在构造函数中)。因此,我想在设置View
.
我尝试先设置加载屏幕并view
使用 asynctask 和处理程序在不同的线程上启动,但没有任何运气。第一个给我can't create handler inside thread that has not called looper.prepare()
,另一个只是逐行执行代码。
也许我做错了什么,任何想法如何划分加载过程,例如:1. set loading screen
。view class
2.在后台加载(实例化)。3. 设置view class
代替loading screen
。
谢谢
更新
public MainMenuView(Context context) {
super(context);
System.gc();
this.context = context;
sh = new SharedHelper(context);
am = context.getAssets();
sp = new SoundHelper(context,
SoundHelper.CLICK,
SoundHelper.MAIN_MELODY
);
screenWH = Helper.getScreenDimensions(context);
bg = new Background(screenWH, am);
scaleAmount = bg.scaleAmount();
scaleAmount = scaleAmount * 0.9f;
strings = Helper.getStrings((int)sh.getOption(Option.LANGUAGE), new DBHelper(context));
mainTitle = Helper.scaleBMP(Helper.assetImage(am, "Main/title.png"), scaleAmount);
mainTitleM.postTranslate(bg.getMargins()[0], -10);
/**
* Load cloud-like buttons
* */
float top = 0.03f * screenWH[1];
float left = bg.getMargins()[0] + mainTitle.getWidth();
MainButton repeat = new MainButton("Main/Buttons/repeat_on.png", "Main/Buttons/repeat.png", am, scaleAmount);
repeat.setPosition(left, top);
repeat.setType(Helper.REPEAT);
btns.add(repeat);
left += 0.3f * repeat.getBMP().getWidth();
top += repeat.getBMP().getHeight();
MainButton test = new MainButton("Main/Buttons/test_on.png", "Main/Buttons/test.png", am, scaleAmount);
test.setPosition(left, top);
test.setType(Helper.TEST);
btns.add(test);
left -= 0.7f * test.getBMP().getWidth();
top += 0.5f * test.getBMP().getHeight();
MainButton learn = new MainButton("Main/Buttons/learn_on.png", "Main/Buttons/learn.png", am, scaleAmount);
learn.setPosition(left, top);
learn.setType(Helper.LEARN);
btns.add(learn);
/**
* Main control buttons
* */
left = 5;
top = bg.getTopMargin();
buttonClass parent = new buttonClass("Buttons/parents control.png", am, scaleAmount, Helper.PARENTS);
parent.setPosition(left, top);
btns.add(parent);
top += parent.getBMP().getHeight() + 2;
buttonClass facebook = new buttonClass("Buttons/facebook.png", am, scaleAmount, Helper.FACEBOOK);
facebook.setPosition(left, top);
btns.add(facebook);
top += facebook.getBMP().getHeight() + 2;
buttonClass share = new buttonClass("Buttons/share.png", am, scaleAmount, Helper.SHARE);
share.setPosition(left, top);
btns.add(share);
top += share.getBMP().getHeight() + 2;
buttonClass pay = new buttonClass("Buttons/pay.png", am, scaleAmount, Helper.PAY);
pay.setPosition(left, top);
btns.add(pay);
/**
* Sprites
* */
float widthsix = 0;
float widthasix = 0;
float highest = 0;
for(int i=1; i<=11; i++){
mainSprite spr = new mainSprite(i, context, scaleAmount);
if(i <= 6){
widthsix += spr.getBMP().getWidth();
}
else{
widthasix += spr.getBMP().getWidth();
}
if(i == 9){
highest = spr.getBMP().getHeight();
}
sprites.add(spr);
}
float[] margins = bg.getMargins();
left = margins[0] + (margins[2] - margins[0] - widthsix) / 2;
float leftA = margins[0] + (margins[2] - margins[0] - widthasix) / 2;
top = screenWH[1] - highest;
float angle = 0;
float offset = 0.05f * screenWH[1];
float angle_step = 30.0f;
int i = 1;
float topA = 0;
for(mainSprite s : sprites){
float top_mod = top - s.getBMP().getHeight() + (float)Math.sin(Math.toRadians(angle)) * offset - offset;
s.setPosition(left, top_mod);
left += s.getBMP().getWidth();
angle += angle_step;
if(i == 3){
topA = screenWH[1];
}
if(i == 6){
top = topA;
left = leftA;
angle_step = 36f;
angle = 0;
}
i++;
}
sp.playSound(SoundHelper.MAIN_MELODY);
addDialog();
/**
* start animation
* */
currentSpr = 8;
sprites.get(currentSpr).start();
handle.postDelayed(runAnimation, 5000);
}
public void loadMain(){
screenWH = Helper.getScreenDimensions(context);
bg = new Background(screenWH, am);
scaleAmount = bg.scaleAmount();
scaleAmount = scaleAmount * 0.9f;
strings = Helper.getStrings((int)sh.getOption(Option.LANGUAGE), new DBHelper(context));
mainTitle = Helper.scaleBMP(Helper.assetImage(am, "Main/title.png"), scaleAmount);
mainTitleM.postTranslate(bg.getMargins()[0], -10);
/**
* Load cloud-like buttons
* */
float top = 0.03f * screenWH[1];
float left = bg.getMargins()[0] + mainTitle.getWidth();
MainButton repeat = new MainButton("Main/Buttons/repeat_on.png", "Main/Buttons/repeat.png", am, scaleAmount);
repeat.setPosition(left, top);
repeat.setType(Helper.REPEAT);
btns.add(repeat);
left += 0.3f * repeat.getBMP().getWidth();
top += repeat.getBMP().getHeight();
MainButton test = new MainButton("Main/Buttons/test_on.png", "Main/Buttons/test.png", am, scaleAmount);
test.setPosition(left, top);
test.setType(Helper.TEST);
btns.add(test);
left -= 0.7f * test.getBMP().getWidth();
top += 0.5f * test.getBMP().getHeight();
MainButton learn = new MainButton("Main/Buttons/learn_on.png", "Main/Buttons/learn.png", am, scaleAmount);
learn.setPosition(left, top);
learn.setType(Helper.LEARN);
btns.add(learn);
/**
* Main control buttons
* */
left = 5;
top = bg.getTopMargin();
buttonClass parent = new buttonClass("Buttons/parents control.png", am, scaleAmount, Helper.PARENTS);
parent.setPosition(left, top);
btns.add(parent);
top += parent.getBMP().getHeight() + 2;
buttonClass facebook = new buttonClass("Buttons/facebook.png", am, scaleAmount, Helper.FACEBOOK);
facebook.setPosition(left, top);
btns.add(facebook);
top += facebook.getBMP().getHeight() + 2;
buttonClass share = new buttonClass("Buttons/share.png", am, scaleAmount, Helper.SHARE);
share.setPosition(left, top);
btns.add(share);
top += share.getBMP().getHeight() + 2;
buttonClass pay = new buttonClass("Buttons/pay.png", am, scaleAmount, Helper.PAY);
pay.setPosition(left, top);
btns.add(pay);
/**
* Sprites
* */
float widthsix = 0;
float widthasix = 0;
float highest = 0;
for(int i=1; i<=11; i++){
mainSprite spr = new mainSprite(i, context, scaleAmount);
if(i <= 6){
widthsix += spr.getBMP().getWidth();
}
else{
widthasix += spr.getBMP().getWidth();
}
if(i == 9){
highest = spr.getBMP().getHeight();
}
sprites.add(spr);
}
float[] margins = bg.getMargins();
left = margins[0] + (margins[2] - margins[0] - widthsix) / 2;
float leftA = margins[0] + (margins[2] - margins[0] - widthasix) / 2;
top = screenWH[1] - highest;
float angle = 0;
float offset = 0.05f * screenWH[1];
float angle_step = 30.0f;
int i = 1;
float topA = 0;
for(mainSprite s : sprites){
float top_mod = top - s.getBMP().getHeight() + (float)Math.sin(Math.toRadians(angle)) * offset - offset;
s.setPosition(left, top_mod);
left += s.getBMP().getWidth();
angle += angle_step;
if(i == 3){
topA = screenWH[1];
}
if(i == 6){
top = topA;
left = leftA;
angle_step = 36f;
angle = 0;
}
i++;
}
sp.playSound(SoundHelper.MAIN_MELODY);
addDialog();
/**
* start animation
* */
currentSpr = 8;
sprites.get(currentSpr).start();
//handle.postDelayed(runAnimation, 5000);
}
我在 Activity 的 onCreate 方法中加载视图
new Handler().post(new Runnable(){
@Override
public void run(){
setContentView(new CustomView(context));
}
});