Android magnifiers are implemented in two ways:

Source: Internet
Author: User
Tags gety

Public class shaderview extends view {private final Bitmap bitmap; private final shapedrawable drawable; // the radius of the magnifier is Private Static final int radius = 80; // magnification: Private Static final int factor = 3; private final matrix = new matrix (); Public shaderview (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 drawabledrawable = new shapedrawable (New ovalshape (); drawable. getpaint (). setshader (shader); drawable. setbounds (0, 0, radius * 2, radius * 2);} @ overridepublic Boolean ontouchevent (motionevent event) {final int x = (INT) event. getx (); Final int y = (INT) event. gety (); // This position indicates the starting position of the shader matrix. settranslate (radius-x * factor, radius-y * factor); drawable. getpaint (). getshader (). setlocalmatrix (matrix); // bounds, that is, the drawable rectangle of the circle. setbounds (X-radius, Y-radius, x + radius, Y + radius); invalidate (); Return true ;}@ overridepublic void ondraw (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 set the shader of its paint to the image to be enlarged, which is then a simple location movement problem. The radius and magnification of the magnifier can be modified in the Code. The Code is annotated and should be well understood.

However, if there is only one solution to a problem, it will be a bit frustrating and it will not work if you want to try something different.
When playing a program, you have to play with your personality and passion. Haha, there are too many nonsense. Let's go back to the question.


Let's take a look at another Implementation of magnifiers.

Public class pathview extends view {private final path mpath = New Path (); private final matrix = new matrix (); private final Bitmap bitmap; // The radius of the magnifier is Private Static final int radius = 80; // The magnification is Private Static final int factor = 2; private int mcurrentx, mcurrenty; Public pathview (context) {super (context); mpath. addcircle (radius, direction. CW); matrix. setscale (factor, factor); bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. demo) ;}@ overridepublic Boolean ontouchevent (motionevent event) {mcurrentx = (INT) event. getx (); mcurrenty = (INT) event. gety (); invalidate (); Return true ;}@ overridepublic void ondraw (canvas) {super. ondraw (canvas); // specifies the basemap canvas. drawbitmap (bitmap, 0, 0, null); // cut the canvas. translate (mcurrentx-radius, mcurrenty-radius); canvas. clippath (mpath); // draw the enlarged image canvas. translate (radius-mcurrentx * factor, radius-mcurrenty * factor); canvas. drawbitmap (bitmap, matrix, null );}}


The path class is used to cut the canvas out of a circular area and draw the enlarged area on it.
Below we use this tool to view a set of PC configuration, let you see more clearly, engineering http://download.csdn.net/detail/hustpzb/4523274



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.