Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the effect of magnifying glass in Android

Shulou Source: shulou.com Published: 2022-05-31 22:01:58 09月24日 Update

In this article, the editor introduces in detail "how to achieve magnifying glass effect in Android". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve magnifying glass effect in Android" can help you solve your doubts.

Source code analysis

Public class ShaderView extends View {private final Bitmap bitmap; private final ShapeDrawable drawable; / / magnifying glass radius private static final int RADIUS = 80; / / magnification private static final int FACTOR = 3; private final Matrix matrix = new Matrix (); public ShaderView (Context context) {super (context); Bitmap bmp = BitmapFactory.decodeResource (getResources (), R.drawable.demo) Bitmap = bmp; BitmapShader shader = new BitmapShader (Bitmap.createScaledBitmap (bmp, bmp.getWidth () * FACTOR, bmp.getHeight () * FACTOR, true), TileMode.CLAMP, TileMode.CLAMP); / / Circular drawable drawable = new ShapeDrawable (new OvalShape ()); drawable.getPaint (). SetShader (shader); drawable.setBounds (0,0, RADIUS * 2, RADIUS * 2) } @ Override public boolean onTouchEvent (MotionEvent event) {final int x = (int) event.getX (); final int y = (int) event.getY (); / / this position represents the starting position of the drawing shader matrix.setTranslate (RADIUS-x * FACTOR, RADIUS-y * FACTOR); drawable.getPaint (). GetShader (). SetLocalMatrix (matrix) / / bounds, which is the circumscribed rectangle drawable.setBounds (x-RADIUS, y-RADIUS, x + RADIUS, y + RADIUS); invalidate (); return true;} @ Override public void onDraw (Canvas canvas) {super.onDraw (canvas); canvas.drawBitmap (bitmap, 0,0, null); drawable.draw (canvas);}}

The basic principle is to use ShapeDrawable to construct a circular drawable, and then the shader of its paint is set to the image to be enlarged, and then there is a simple problem of position movement. The radius and magnification of the magnifying glass can be modified in the code, and the code has comments, which should be easy to understand.

However, if there is only one solution to a problem, it is a bit frustrating, even if you want to play something different. Let's take a look at another implementation of the magnifying glass.

Public class PathView extends View {private final Path mPath = new Path (); private final Matrix matrix = new Matrix (); radius of private final Bitmap bitmap; / / magnifying glass private static final int RADIUS = 80; / / magnification private static final int FACTOR = 2; private int mCurrentX, mCurrentY; public PathView (Context context) {super (context) MPath.addCircle (RADIUS, Direction.CW); matrix.setScale (FACTOR, FACTOR); bitmap = BitmapFactory.decodeResource (getResources (), R.drawable.demo);} @ Override public boolean onTouchEvent (MotionEvent event) {mCurrentX = (int) event.getX (); mCurrentY = (int) event.getY (); invalidate (); return true } @ Override public void onDraw (Canvas canvas) {super.onDraw (canvas); / / Base canvas.drawBitmap (bitmap, 0,0, null); / / cut canvas.translate (mCurrentX-RADIUS, mCurrentY-RADIUS); canvas.clipPath (mPath) / / draw the enlarged picture canvas.translate (RADIUS-mCurrentX * FACTOR, RADIUS-mCurrentY * FACTOR); canvas.drawBitmap (bitmap, matrix, null) }} here, the article "how to achieve the magnifying glass effect in Android" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Tags: Magnifying glass effect position multiple radius that is article code content circle problem no proper frustrated principle alternative only pictures ideas new knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB MySQL Docker macOS