Android magnifying glass effect implementation

Android magnifying glass effect implementation

Overview

I believe that many students who have used English applications have seen the effect of a magnifying glass. After selecting a piece of text, there will be a magnifying glass. How is this achieved? Let's analyze it today.

Source code analysis

  1. public class ShaderView extends View {
  2. private final Bitmap bitmap;
  3. private final ShapeDrawable drawable;
  4. // The radius of the magnifying glass
  5. private static final int RADIUS = 80;
  6. // Magnification
  7. private static final int FACTOR = 3;
  8. private final Matrix matrix = new Matrix();
  9.   
  10. public ShaderView(Context context) {
  11. super(context);
  12. Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.demo);
  13. bitmap = bmp;
  14. BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(bmp,
  15. bmp.getWidth() * FACTOR, bmp.getHeight() * FACTOR, true ), TileMode.CLAMP, TileMode.CLAMP);
  16.   
  17. //Circular drawable
  18. drawable = new ShapeDrawable(new OvalShape());
  19. drawable.getPaint().setShader(shader);
  20. drawable.setBounds(0, 0, RADIUS * 2, RADIUS * 2);
  21. }
  22.   
  23. @Override
  24. public boolean onTouchEvent(MotionEvent event) {
  25. final int x = ( int ) event.getX();
  26. final int y = ( int ) event.getY();
  27.   
  28. // This position indicates the starting position of the shader
  29. matrix.setTranslate(RADIUS - x * FACTOR, RADIUS - y * FACTOR);
  30. drawable.getPaint().getShader().setLocalMatrix(matrix);
  31.   
  32. // bounds, that is the circumscribed rectangle of the circle
  33. drawable.setBounds(x - RADIUS, y - RADIUS, x + RADIUS, y + RADIUS);
  34. invalidate();
  35. return   true ;
  36. }
  37.   
  38. @Override
  39. public void onDraw(Canvas canvas) {
  40. super.onDraw(canvas);
  41. canvas.drawBitmap(bitmap, 0, 0, null );
  42. drawable.draw(canvas);
  43. }
  44. }

The basic principle is to use ShapeDrawable to construct a circular drawable, and then set its paint shader to the image to be magnified, and then it is a simple matter of moving the position. The radius and magnification of the magnifying glass can be modified in the code, and the code is commented, so it should be easy to understand.

However, if there is only one solution to a problem, it is a bit frustrating. You can't even try something different. You have to play with personality and passion when playing with programs. Haha, too much nonsense, let's get back to the topic. Let's take a look at another implementation of the magnifying glass

  1. public class PathView extends View {
  2. private final Path mPath = new Path();
  3. private final Matrix matrix = new Matrix();
  4. private final Bitmap bitmap;
  5.   
  6. // The radius of the magnifying glass
  7. private static final int RADIUS = 80;
  8.   
  9. // Magnification
  10. private static final int FACTOR = 2;
  11. private int mCurrentX, mCurrentY;
  12.   
  13. public PathView(Context context) {
  14. super(context);
  15. mPath.addCircle(RADIUS, RADIUS, RADIUS, Direction.CW);
  16. matrix.setScale(FACTOR, FACTOR);
  17.   
  18. bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.demo);
  19. }
  20.   
  21. @Override
  22. public boolean onTouchEvent(MotionEvent event) {
  23. mCurrentX = ( int ) event.getX();
  24. mCurrentY = ( int ) event.getY();
  25.   
  26. invalidate();
  27. return   true ;
  28. }
  29.   
  30. @Override
  31. public void onDraw(Canvas canvas) {
  32. super.onDraw(canvas);
  33.   
  34. // Basemap
  35. canvas.drawBitmap(bitmap, 0, 0, null );
  36.   
  37. // Cut
  38. canvas.translate(mCurrentX - RADIUS, mCurrentY - RADIUS);
  39. canvas.clipPath(mPath);
  40.   
  41. // Draw the enlarged image
  42. canvas.translate(RADIUS - mCurrentX * FACTOR, RADIUS - mCurrentY * FACTOR);
  43. canvas.drawBitmap(bitmap, matrix, null );
  44. }
  45. }

The Path class is used here to cut out a circular area from the canvas and draw the enlarged part on it.

Project download address http://download.csdn.net/detail/hustpzb/4523274

<<:  Android keyboard panel conflict layout flashing solution

>>:  Android imitates the layout architecture of the product details page of JD.com and Tmall app, as well as its functional implementation

Recommend

The "stumbling" robot dog died in just one hour...

To avoid predators, animals such as newborn giraf...

"Perfect Diary" community operation method!

From January to December 2019, Perfect Diary ’s c...

What is the algorithm of Tik Tok’s recommendation mechanism?

The reason why Tik Tok is so popular is definitel...

It’s scary! Windows 10 can collect so much personal privacy

There are many controversies about Windows 10, one...

Can medication be taken with coffee or milk? The truth is…

Coffee and medicine For China, a country with a l...

How does Li Jiaqi do marketing on Xiaohongshu?

Keywords of this article: Li Jiaqi , Double Eleve...

How to carry out a more complex “S-level content” operation? ?

Content-based platforms usually produce or introd...

How was the deepest well on land in Asia created?

Workers at Tarim Oilfield are working on the proj...

React Native Introduction and Getting Started

1. Introduction [[169817]] ReactNative.png React ...

The VR Fund: AR company industry map in the second quarter of 2017

The Venture Reality Fund, which focuses on VR/AR ...