Created
January 4, 2020 16:26
-
-
Save tkuenneth/2fe3dbfd7969ed4960ac7b50d9c0c3da to your computer and use it in GitHub Desktop.
How to use the Magnifier ui component
This file contains hidden or 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 MagnifierDemoActivity extends Activity { | |
private Handler handler; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ViewGroup layout = findViewById(R.id.layout); | |
Magnifier magnifier = new Magnifier(layout); | |
layout.setOnTouchListener((view, motionEvent) -> { | |
switch (motionEvent.getAction()) { | |
case MotionEvent.ACTION_DOWN: | |
case MotionEvent.ACTION_MOVE: | |
if (handler == null) { | |
handler = new Handler(); | |
} | |
magnifier.show(motionEvent.getX(), motionEvent.getY()); | |
post(handler, magnifier); | |
return true; | |
case MotionEvent.ACTION_UP: | |
view.performClick(); | |
if (handler != null) { | |
handler = null; | |
} | |
magnifier.dismiss(); | |
return true; | |
default: | |
return false; | |
} | |
}); | |
} | |
private void post(Handler handler, Magnifier magnifier) { | |
handler.postDelayed(() -> { | |
magnifier.update(); | |
post(handler, magnifier); | |
}, 200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment