繁体   English   中英

如何在不使用xml的情况下将进度条添加到画布?

[英]How can I add an progressbar to my canvas without 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);




    }

}

我现在有这个: 在此处输入图片说明

我想要这样:

在此处输入图片说明

我想要一个在该位置上的进度条,以便您可以升级游戏。 没有载入栏,只有进度栏,所以您可以看到升级的程度! 有人可以帮我做这个还是我怎么做?

非常感谢

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




}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM