Last active
February 21, 2020 06:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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