Как анимировать индикатор уровня? - PullRequest
0 голосов
/ 25 июня 2019

Я довольно новичок в Java и Android Studio. Я хотел бы анимировать мой бар, который я создал, со случайным значением. Я создал customView с Android Studio и нарисовал прямоугольник холста. Следующим шагом является подача некоторых случайных значений и анимация бара. Я достиг этого навыка с помощью (Math.random) в JavaScript.

    public class CustomView extends View {

    private Rect rect;
    private Paint paint;
    int radius;

    public CustomView(Context context){
        super(context);
        init(null);
    }

    public CustomView(Context context, AttributeSet attrs){
        super(context, attrs);
        init(attrs);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
        super(context, attrs, defStyleAttr, defStyleRes);
        init(attrs);
    }

    private void init(@Nullable AttributeSet set){
        rect = new Rect();
        // the flag is more pixelated
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        postInvalidate();
    }

    //drawing a canvas
    @Override
    protected void onDraw(Canvas canvas){
        int viewWidth = getWidth();
        int viewHeight = getHeight();

        int leftTopX = viewWidth - 750;
        int leftTopY = viewHeight - 750;

        int rightBotX = viewWidth + 150;
        int rightBotY = viewHeight + 150;

        paint.setColor(Color.MAGENTA);
        canvas.drawRect(leftTopX,leftTopY, rightBotX,rightBotY,paint);
        paint.setColor(Color.GREEN);

    }
}

Это то, чего я достиг к настоящему времени. Но как я могу анимировать панель с Java?

enter image description here

1 Ответ

1 голос
/ 25 июня 2019
Try this -
 I used it with seekbar. You can use it for your view. As per your case. You should Progressbar or Seekbar.

seekbar = ((CustomSeekbar) findViewById(R.id.customSeekBar));

       seekbar.setMax(2600);

final ObjectAnimator animation = ObjectAnimator.ofInt(seekbar, "progress", 0, 800;
      animation.setDuration(1500);
      animation.setInterpolator(new DecelerateInterpolator());
      animation.start();
      seekbar.clearAnimation();

Here 0 is the starting value and 800 is the max value. Please change is as per your requirement.
...