Android 如何在没有xml的情况下向画布添加progressbar?

Android 如何在没有xml的情况下向画布添加progressbar?,android,android-canvas,android-progressbar,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: " +

这是我绘制文本的代码

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);




    }

}
我现在有这个:

我想要这样:

我想要一个进度条在那个地方,这样你们可以在游戏中升级。 没有加载条,只有一个进度条,这样你就可以看到你在升级中有多远了! 有人能帮我做这个吗?我该怎么做

非常感谢

 ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);
要显示progressbar,请写入
progressbar.show()
对于hide write
progressbar.disclose()

例如:-


如果我复制您的代码并将其粘贴到我的代码上,它将不起作用。可能是因为您编写了.show()和.disease()方法,但这些方法不连续地编写
progressbar.disease()
调用progressbar.show()时会出错;您在哪里初始化了ProgressBar..?
ProgressBar.setVisibility(View.VISIBLE)使用此选项以哪种方式显示?以进度条的形式?还是只发短信?
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);




}

}
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();
                }
            }
        });
canvas.drawRect(0,0,canvas.getWidth()*animationValue,getWidth()/8,mBackgroundBorder);