Как я могу добавить индикатор на холсте без XML?

Это мой код для рисования текста.

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




    }

}

теперь у меня есть это:

Я хочу это так:

Я хочу прогресс-бар в этом месте, чтобы вы могли повышаться в игре. Нет загрузки, только индикатор, чтобы вы могли видеть, как далеко вы продвинулись в прокачке! Может кто-нибудь сделать это для меня или как я могу это сделать?

Большое спасибо

2 ответа

Решение
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);
 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);




}

}
Другие вопросы по тегам