Skip to content

Instantly share code, notes, and snippets.

@kunmi
Last active February 21, 2020 06:14
Show Gist options
  • Save kunmi/b9c724bacbacbabb7123459ee5343435 to your computer and use it in GitHub Desktop.
Save kunmi/b9c724bacbacbabb7123459ee5343435 to your computer and use it in GitHub Desktop.
kunmii.blogspot.com snippet for Building an Animated QR Scanner Viewfinder - Like Whatsapp web scanner view Raw
public class MainActivity extends AppCompatActivity {
ObjectAnimator animator;
View scannerLayout;
View scannerBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Scanner overlay
scannerLayout = findViewById(R.id.scannerLayout);
scannerBar = findViewById(R.id.scannerBar);
animator = null;
ViewTreeObserver vto = scannerLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
scannerLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
scannerLayout.getViewTreeObserver().
removeGlobalOnLayoutListener(this);
} else {
scannerLayout.getViewTreeObserver().
removeOnGlobalLayoutListener(this);
}
float destination = (float)(scannerLayout.getY() +
scannerLayout.getHeight());
animator = ObjectAnimator.ofFloat(scannerBar, "translationY",
scannerLayout.getY(),
destination);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.setDuration(3000);
animator.start();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment