如何为水平仪栏设置动画?

时间:2019-06-25 11:15:49

标签: java android canvas android-custom-view

我对Java和android studio相当陌生。我想为我创建的带有随机值的栏设置动画。我已经用android studio创建了customView并绘制了一个画布矩形。下一步是输入一些随机值并为条形动画。我用JavaScript(Math.random)实现了这一壮举。

    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动画bar?

enter image description here

1 个答案:

答案 0 :(得分:1)

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.