0

我有一个view class要为我的activity. 但是它的启动很长(大部分代码,例如加载位图都放在构造函数中)。因此,我想在设置View.

我尝试先设置加载屏幕并view使用 asynctask 和处​​理程序在不同的线程上启动,但没有任何运气。第一个给我can't create handler inside thread that has not called looper.prepare(),另一个只是逐行执行代码。

也许我做错了什么,任何想法如何划分加载过程,例如:1. set loading screenview class2.在后台加载(实例化)。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));
}
});
4

1 回答 1

0

移动加载所有位图和内容的代码,AsyncTask而不是在构造函数中执行。从onCreate活动方法中执行此操作。

然后,您可以使用onProgressUpdate回调AsyncTask来通知进度(例如加载进度条)。例如,您可以使用ProgresBar.setMax将其设置为要加载的项目数,在加载项目时将其传递给进度回调publishProgress,然后ProgressBar.setProgress使用该值调用。

在加载项目时,您可以将它们添加到Assets类中。这可以通过onPostExecute回调方法传回,然后您可以将其分配给活动中的成员变量。

然后,您可以让活动的布局看起来像:

LinearLayout
    RelativeLayout [id=load_screen, background=loading_bg]
        ProgressBar [id=loading_progress]
    RelativeLayout [id=main, visibility=hidden]
        ...

然后调用findViewById(R.id.load_screen).setVisible(View.GONE)隐藏加载屏幕和findViewById(R.id.load_screen).setVisible(View.VISIBLE)

或者,您可以加载替换视图(通过LayoutInflater.inflate调用),然后调用setContentView它。当inflate调用返回时,视图已完全加载,因此您可以在此处进行所有处理程序设置。

布局加载或可见性切换也会发生onPostExecuteAsyncTask.

于 2013-08-21T23:10:47.830 回答