启动画面同步加载

时间:2015-07-02 00:51:40

标签: android multithreading performance android-canvas splash-screen

我已经实现了加载屏幕(splashScreen),但希望它与我的实际应用加载同步。我希望应用程序在后台加载主页面,所以当我在加载屏幕上完成定时加载时,它已经将主屏幕拉到后台。我对Async任务和线程相对较新,因为我没有用游戏做很多应用程序,但可以使用一些帮助。感谢

Canvas Load Bar运动:

public class CustomRectangle extends View{

float left = 0, right = 500, top = 500, bot = 1000;
boolean check = false, finish = false;
int screenWidth = 0, screenHeight = 0;
Bitmap image;

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public CustomRectangle(Context context){
    super(context);

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    Point size = new Point();
    display.getSize(size);

    this.screenHeight = size.y;
    this.screenWidth = size.x;

    this.image = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
    this.image = Bitmap.createScaledBitmap(this.image, screenWidth, screenHeight, false);

}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(check == false) {
        Paint paintInitial = new Paint();
        paintInitial.setColor(Color.YELLOW);
        this.left = 0;
        this.top = canvas.getHeight() - 10;
        this.right = canvas.getWidth();
        this.bot = canvas.getHeight();
        canvas.drawRect(left, top - 10, right, bot, paintInitial);
        this.check = true;
    }
    if(check == true && finish == false) {
        Paint paintWhite = new Paint();
        paintWhite.setColor(Color.WHITE);
        Paint paintYellow = new Paint();
        paintYellow.setColor(Color.YELLOW);
        canvas.drawRect(left, top, right, bot, paintYellow);
        canvas.drawBitmap(image, 0, 0, null);
        top = top - 5;
        invalidate();
        if(top <= 0){
            finish = true;
            Intent intent = new Intent(getContext(), Home_Page.class);
            getContext().startActivity(intent);
            Log.e("", "Finished Loading!");

        }
    }
}

}

启动屏幕活动:

public class SplashScreen extends ActionBarActivity {

Rect loadBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new CustomRectangle(this));


}

}

截至目前,条形加载到屏幕顶部,然后启动主要活动,但我不认为主屏幕加载直到结束,所以这实际上不会同步加载。如果有人可以引导我朝着正确的方向发布示例或发布修改后的代码,那就太棒了。

1 个答案:

答案 0 :(得分:0)

我对你做了什么感到有点困惑,但如果你只需要一个加载栏,我建议只有一个活动。在布局文件中,使其成为FrameLayout或RelativeLayout。在里面,您可以拥有进度条,以及包含主要活动布局的另一个布局。使主活动布局不可见,并且两者的宽度/高度都应匹配为match_parent。

当您的活动启动时,它只会显示进度条,然后一旦您完成加载,您只需将进度条设置为不可见,并将主布局设置为可见。

要注意的一件事是确保在更改视图的可见性时在UI线程上运行它。您可以通过调用runOnUiThread

来完成此操作
runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                progressBar.setVisibility(View.INVISIBLE);
                mainLayout.setVisibility(View.VISIBLE);
            }
        }
);