Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of magnifying glass in Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report