如何在没有xml的情况下向我的画布添加进度条?

时间:2016-10-28 09:59:27

标签: android android-canvas android-progressbar

这是我绘制文字的代码。

public void drawText(Canvas canvas)
{



    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(30);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    canvas.drawText("DISTANCE: " + (player.getScore()) + " M", 10, HEIGHT - 10, paint);
    canvas.drawText("BEST: " + HighScore + " M", WIDTH - 215, HEIGHT - 10, paint);



    if(!player.getPlaying()&&newGameCreated&&reset)
    {

        Bitmap b2 = BitmapFactory.decodeResource(getResources(), R.drawable.coollogo);
        canvas.drawBitmap(b2, WIDTH/2 - 440, HEIGHT/2 - 360, paint);



        canvas.drawRect(380, 380,630,465 , paint);
        canvas.drawBitmap(b1, WIDTH/2 - 260, HEIGHT/2 + 20, paint);




    }

}

我现在有这个: enter image description here

我想这样:

enter image description here

我想在那个地方有一个进度条,所以你可以在游戏中升级。 没有Loadingbar,只是一个进度条,所以你可以看到你升级到多远! 有人可以为我做这个或我该怎么做?

非常感谢

2 个答案:

答案 0 :(得分:1)

 ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

要显示进度条,请写progressbar.show(); 并为隐藏写progressbar.dismiss();

例如: -

public void drawText(Canvas canvas)
{

ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);
progressBar.show();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(30);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
canvas.drawText("DISTANCE: " + (player.getScore()) + " M", 10, HEIGHT - 10, paint);
canvas.drawText("BEST: " + HighScore + " M", WIDTH - 215, HEIGHT - 10, paint);



if(!player.getPlaying()&&newGameCreated&&reset)
{

    Bitmap b2 = BitmapFactory.decodeResource(getResources(), R.drawable.coollogo);
    canvas.drawBitmap(b2, WIDTH/2 - 440, HEIGHT/2 - 360, paint);



    canvas.drawRect(380, 380,630,465 , paint);
    canvas.drawBitmap(b1, WIDTH/2 - 260, HEIGHT/2 + 20, paint);




}

}

答案 1 :(得分:1)

animator= ValueAnimator.ofFloat(0, 1);

        // It will take 5000ms for the animator to go from 0 to 1
        animator.setDuration(5000);

        // Callback that executes on animation steps.
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animationValue=((Float) (animation.getAnimatedValue())).floatValue();

                if(animationValue<=1.0 && animationValue>0.0) {
                    Log.i(TAG, "onAnimationUpdate: .... " + animationValue);
                    invalidate();
                }
            }
        });

使用animationValue

绘制进度
canvas.drawRect(0,0,canvas.getWidth()*animationValue,getWidth()/8,mBackgroundBorder);