@Override public void run() { invalidate(); } };
public BoxLoaderView(Context context) { super(context); init(context, null); }
@Override protected void onDraw(Canvas canvas) { canvas.drawRect(outBox.getLeft(), outBox.getTop(), outBox.getRight(), outBox.getBottom(), outBox.getPaint()); dirChange = box.bounce(canvas, strokeWidth); rectifyBoundaries(canvas, box); canvas.drawRect(box.getLeft(), box.getTop(), box.getRight(), box.getBottom(), box.getPaint()); handler.postDelayed(r, dirChange ? FRAME_RATE * 20 : FRAME_RATE); }
case 0: if(rect.right < canvas.getWidth() - strokeWidth){ increaseRight(); } else { increaseLeft(); if(rect.left > canvas.getWidth()/2){ dir++; case 1: if(rect.bottom < canvas.getHeight() - strokeWidth){ increaseBottom(); } else { increaseTop(); if(rect.top > canvas.getHeight()/2){ dir++; case 2: if(rect.left > strokeWidth){ decreaseLeft(); } else { decreaseRight(); if(rect.right < canvas.getWidth()/2){ dir++; case 3: if(rect.top > strokeWidth){ decreaseTop(); } else { decreaseBottom();
private void rectifyBoundaries(Canvas canvas, Box box) { if(box.getLeft() < strokeWidth){ box.getrect().left = strokeWidth; } if(box.getTop() < strokeWidth){ box.getrect().top = strokeWidth; } if(box.getRight() > canvas.getWidth() - strokeWidth){ box.getrect().right = canvas.getWidth() - strokeWidth; } if(box.getBottom() > canvas.getHeight() - strokeWidth){ box.getrect().bottom = canvas.getHeight() - strokeWidth; } }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if(outBox == null){ outBox = new Box(left, top, right, bottom, strokeColor, 10); outBox.getPaint().setStrokeWidth(strokeWidth); } if(box == null){ box = new Box(left + strokeWidth, top + strokeWidth, right/2 - strokeWidth, bottom/2 - strokeWidth, loaderColor, 10); box.setDx(speed); box.setDy(speed); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLoader = (BoxLoaderView) findViewById(R.id.blv); mLoader.setSpeed(20); } }
public BoxLoaderView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); }