有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何设置水平仪栏的动画?

我对Java和安卓 studio还相当陌生。我想动画我的酒吧,我用随机值创建。我用安卓 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制作酒吧的动画呢

enter image description here


共 (1) 个答案

  1. # 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.