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

Automobile brand event marketing strategy!

On the evening of April 15, the first online conc...

Is it possible for the Mu Us Desert to turn into grassland or forest?

The Maowusu Desert is one of the four major sandy...

Online sales increased by 170%, Perfect Diary’s promotion strategy!

In the world of brand marketing, new "market...

A picture to understand the front-end performance optimization of Html5

Overview 1. PC optimization methods are also appl...

The three major hacker groups that keep Americans awake at night

[[122276]] In light of recent hacker attacks, the...

Code to handle iOS horizontal and vertical screen rotation

1. Monitor screen rotation direction When dealing...

Can advertising on TikTok bring in traffic?

TikTok has become popular, and many marketers are...

Kang Ge: Build a small money-making machine in 30 days

Kang Ge's resource introduction of "Build...

Creating QR codes using Google's Web API

Google Charts can be queried via POST (see here f...