Nearly three months did not write a blog, because of work transfer, a lot of experience, ideas have no time to record down. Now the time is a little abundance, I will try my best to take the time to make up the things I want to write without writing. Get to the point.
Sometime last year, I came across an article about the realization of a magnifying glass inside Android. The article is very messy, there is no format, basically belong to see the kind that does not go down. Although the genre is very interesting, but I do not have enough internal force to read it. But see a key word, say is to use with round drawable. This sentence is enough, he wrote a pile of things I also don't bother to see, so I began to try, and then made it. Now the code is posted out to share.
Java code
Copy Code code as follows:
Package chroya.demo.magnifier;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.BitmapShader;
Import Android.graphics.Canvas;
Import Android.graphics.Matrix;
Import Android.graphics.Shader.TileMode;
Import android.graphics.drawable.ShapeDrawable;
Import Android.graphics.drawable.shapes.OvalShape;
Import android.view.MotionEvent;
Import Android.view.View;
/**
* Magnifier Implementation Mode 1
* @author Chroya
*
*/
public class Shaderview extends view{
Private Bitmap Bitmap;
Private Shapedrawable drawable;
The radius of the magnifying glass
private static final int RADIUS = 80;
Magnification ratio
private static final int FACTOR = 3;
Private Matrix matrix = new Matrix ();
Public Shaderview {
Super (context);
Bitmap bmp = Bitmapfactory.decoderesource (Getresources (), r.drawable.show);
bitmap = BMP;
Bitmapshader shader = new Bitmapshader (
Bitmap.createscaledbitmap (BMP, Bmp.getwidth () *factor,
Bmp.getheight () *factor, True), Tilemode.clamp, Tilemode.clamp);
Round 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, that's the rectangle with the circle.
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 its paint shader set to the picture to be enlarged, and then the simple position movement problem. Magnifying glass radius and magnification can be modified in the code, the code has comments, should be well understood.
However, if there is only one solution to a problem, it would be a bit frustrating to play something different.
Play the program will have to play a personality, play a passion. Haha, too much nonsense, cut back to the subject.
Let's look at another realization of the magnifying glass ^-^
Java code
Copy Code code as follows:
Package chroya.demo.magnifier;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Matrix;
Import Android.graphics.Path;
Import android.graphics.Path.Direction;
Import android.view.MotionEvent;
Import Android.view.View;
/**
* Magnifier Implementation Mode 2
* @author Chroya
*
*/
public class Pathview extends view{
Private Path MPath = new Path ();
Private Matrix matrix = new Matrix ();
Private Bitmap Bitmap;
The radius of the magnifying glass
private static final int RADIUS = 80;
Magnification ratio
private static final int FACTOR = 2;
private int mcurrentx, mcurrenty;
Public Pathview {
Super (context);
Mpath.addcircle (RADIUS, radius, radius, DIRECTION.CW);
Matrix.setscale (FACTOR, FACTOR);
Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.show);
}
@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);
Bottom chart
Canvas.drawbitmap (bitmap, 0, 0, NULL);
Shear
Canvas.translate (Mcurrentx-radius, Mcurrenty-radius);
Canvas.clippath (MPath);
Picture enlarged
Canvas.translate (Radius-mcurrentx*factor, radius-mcurrenty*factor);
Canvas.drawbitmap (bitmap, matrix, null);
}
}
The path class is used here to cut the canvas out of a circular region and draw the enlarged part on it.
The effects are the same in both ways, as shown in the figure:
Magnified twice times the effect.
Magnified three times times
It seems to be a disadvantage, isn't it? Hey, the magnifying glass is missing a box outside. That thing, I have no resources, so lazy to get, interested in their own add it.