The main function of this article is to recognize the gesture in ImageView to control the image to enlarge or shrink, which has a certain reference value and share it to everyone.
public class Matriximageview extends ImageView {private Gesturedetector mgesturedetector;
Private Matrix Mmatrix = new Matrix ();
private float mimagewidth;
private float mimageheight;
private float Mscale;
Private Onmovinglistener Mmovelistener;
Private Onsingletaplistener Msingletaplistener;
Public Matriximageview (context, AttributeSet attrs) {Super (context, attrs);
Init ();
Public Matriximageview {Super (context, NULL);
Init ();
private void Init () {Matrixtouchlistener listener = new Matrixtouchlistener ();
Setontouchlistener (listener);
Mgesturedetector = new Gesturedetector (GetContext (), New Gesturelistener (listener));
SetBackgroundColor (Color.Black);
Setscaletype (Scaletype.fit_center);
public void Setonmovinglistener (Onmovinglistener listener) {Mmovelistener = listener; } public void Setonsingletaplistener (Onsingletaplistener onsingletaplistener) {This.msingletaplisTener = Onsingletaplistener;
@Override public void Setimagebitmap (Bitmap bm) {Super.setimagebitmap (BM);
if (getwidth () = = 0) {Viewtreeobserver vto = Getviewtreeobserver (); Vto.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () {public Boolean onpredraw () {INITDA
Ta ();
MatrixImageView.this.getViewTreeObserver (). Removeonpredrawlistener (this);
return true;
}
});
else {initdata ();
}} private void InitData () {Mmatrix.set (Getimagematrix ());
Float[] values = new float[9];
Mmatrix.getvalues (values);
Mimagewidth = GetWidth ()/values[matrix.mscale_x];
Mimageheight = (getheight ()-values[matrix.mtrans_y] * 2)/values[matrix.mscale_y];
Mscale = values[matrix.mscale_x];
} public class Matrixtouchlistener implements Ontouchlistener {private static final int mode_drag = 1;
private static final int mode_zoom = 2; PrivATE static final int mode_unable = 3;
Private static final float Max_scale = 6;
Private static final float double_click_sacle = 2;
private int mmode = 0;
private float Mstartdis;
Private Matrix Mcurrentmatrix = new Matrix ();
Private Boolean mleftdragable;
Private Boolean mrightdragable;
Private Boolean mfirstmove = false;
Private PointF Mstartpoint = new PointF (); @Override public boolean Ontouch (View v., motionevent event) {switch (event.getactionmasked ()) {Case Moti
OnEvent.ACTION_DOWN:mMode = Mode_drag;
Mstartpoint.set (Event.getx (), event.gety ());
Ismatrixenable ();
StartDrag ();
Checkdragable ();
Break
Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:reSetMatrix ();
Stopdrag ();
Break
Case MotionEvent.ACTION_MOVE:if (Mmode = = mode_zoom) {Setzoommatrix (event);
else if (Mmode = = Mode_drag) {Setdragmatrix (event);
else {Stopdrag ();
} break;
Case MotionEvent.ACTION_POINTER_DOWN:if (Mmode = = mode_unable) return true;
Mmode = Mode_zoom;
Mstartdis = Distance (event);
Break
Case MotionEvent.ACTION_POINTER_UP:break;
Default:break;
Return Mgesturedetector.ontouchevent (event);
private void StartDrag () {if (Mmovelistener!= null) Mmovelistener.startdrag ();
private void Stopdrag () {if (Mmovelistener!= null) Mmovelistener.stopdrag ();
private void Checkdragable () {mleftdragable = true;
Mrightdragable = true;
Mfirstmove = true;
Float[] values = new float[9];
Getimagematrix (). GetValues (values);
if (Values[matrix.mtrans_x] >= 0) mrightdragable = false; if ((mimagewidth) * values[matrix.mscale_x] + values[matrix.mtrans_X] <= getwidth ()) {mleftdragable = false; } public void Setdragmatrix (Motionevent event) {if (iszoomchanged ()) {Float dx = event.getx (
)-Mstartpoint.x;
float dy = event.gety ()-mstartpoint.y;
if (math.sqrt (DX * dx + dy * dy) > 10f) {mstartpoint.set (Event.getx (), event.gety ());
Mcurrentmatrix.set (Getimagematrix ());
Float[] values = new float[9];
Mcurrentmatrix.getvalues (values);
DY = Checkdybound (values, dy);
DX = checkdxbound (values, dx, dy);
Mcurrentmatrix.posttranslate (dx, dy);
Setimagematrix (Mcurrentmatrix);
} else {Stopdrag ();
} private Boolean iszoomchanged () {float[] values = new float[9];
Getimagematrix (). GetValues (values);
Float scale = values[matrix.mscale_x];
return scale!= Mscale;
Private float Checkdybound (float[] values, float dy) { float height = getheight ();
if (Mimageheight * values[matrix.mscale_y] < height) return 0;
if (values[matrix.mtrans_y] + dy > 0) dy =-values[matrix.mtrans_y]; else if (values[matrix.mtrans_y] + dy <-(Mimageheight * values[matrix.mscale_y]-height) dy =-(M
ImageHeight * values[matrix.mscale_y]-height)-values[matrix.mtrans_y];
return dy;
Private float Checkdxbound (float[] values, float dx, float dy) {Float width = getwidth ();
if (!mleftdragable && DX < 0) {if (Math.Abs (DX) * 0.4f > Math.Abs (dy) && mfirstmove) {
Stopdrag ();
return 0; } if (!mrightdragable && dx > 0) {if (Math.Abs (DX) * 0.4f > Math.Abs (dy) && Mfirstmov
e) {Stopdrag ();
return 0;
} mleftdragable = true;
Mrightdragable = true;
if (Mfirstmove) Mfirstmove = false;
if (Mimagewidth * values[matrix.mscale_x] < width) {return 0;
} if (Values[matrix.mtrans_x] + dx > 0) {dx =-values[matrix.mtrans_x]; else if (values[matrix.mtrans_x] + dx <-(Mimagewidth * values[matrix.mscale_x]-width) {dx =
-(Mimagewidth * values[matrix.mscale_x]-width)-values[matrix.mtrans_x];
} return DX;
private void Setzoommatrix (Motionevent event) {if (Event.getpointercount () < 2) return;
float Enddis = distance (event);
if (Enddis > 10f) {float scale = Enddis/mstartdis;
Mstartdis = Enddis;
Mcurrentmatrix.set (Getimagematrix ());
Float[] values = new float[9];
Mcurrentmatrix.getvalues (values);
Scale = Checkmaxscale (scale, values);
PointF Centerf = getcenter (scale, values);
Mcurrentmatrix.postscale (scale, scale, centerf.x, CENTERF.Y); Setimagematrix (Mcurrentmatrix); } private PointF Getcenter (float scale, float[] values) {if (scale * values[matrix.mscale_x] < Mscale ||
Scale >= 1) {Return to New PointF (GetWidth ()/2, GetHeight ()/2);
float cx = getwidth ()/2;
float cy = getheight ()/2;
if ((GetWidth ()/2-values[matrix.mtrans_x]) * Scale < GetWidth ()/2) cx = 0; if ((Mimagewidth * values[matrix.mscale_x] + values[matrix.mtrans_x]) * Scale < getwidth ()) CX = Get
Width ();
return new PointF (CX, CY); Private float Checkmaxscale (float scale, float[] values) {if (scale * values[matrix.mscale_x] > Max_scale
) scale = max_scale/values[matrix.mscale_x];
return scale;
private void Resetmatrix () {if (Checkrest ()) {mcurrentmatrix.set (Mmatrix);
Setimagematrix (Mcurrentmatrix);
else {float[] values = new float[9]; GetImagematrix (). GetValues (values);
float height = mimageheight * values[matrix.mscale_y];
if (Height < getheight ()) {Float TopMargin = (getheight ()-height)/2;
if (TopMargin!= values[matrix.mtrans_y]) {Mcurrentmatrix.set (Getimagematrix ());
Mcurrentmatrix.posttranslate (0, topmargin-values[matrix.mtrans_y]);
Setimagematrix (Mcurrentmatrix);
"}}} private Boolean checkrest () {float[] values = new float[9];
Getimagematrix (). GetValues (values);
Float scale = values[matrix.mscale_x];
return scale < Mscale; private void Ismatrixenable () {if (Getscaletype ()!= scaletype.center) {Setscaletype (Scaletype.matr
IX);
else {mmode = mode_unable;
} private float Distance (Motionevent event) {Float dx = event.getx (1)-event.getx (0);
float dy = event.gety (1)-event.gety (0);
return (float) math.sqrt (DX * dx + dy * dy);
public void OnDoubleClick () {Float scale = iszoomchanged ()? 1:double_click_sacle;
Mcurrentmatrix.set (Mmatrix);
Mcurrentmatrix.postscale (scale, scale, getwidth ()/2, GetHeight ()/2);
Setimagematrix (Mcurrentmatrix); } private class Gesturelistener extends Simpleongesturelistener {private final Matrixtouchlistener Mtouchlisten
Er
Public Gesturelistener (Matrixtouchlistener listener) {This.mtouchlistener = listener;
@Override public boolean Ondown (Motionevent e) {return true;
@Override public boolean Ondoubletap (Motionevent e) {Mtouchlistener.ondoubleclick ();
return true;
@Override public boolean onsingletapup (Motionevent e) {return super.onsingletapup (e);
@Override public void onlongpress (Motionevent e) {super.onlongpress (e); @Override public boolean onscroll (MOtionevent E1, motionevent E2, float Distancex, float distancey) {return Super.onscroll (E1, E2, Distancex,
Distancey);
@Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {
Return Super.onfling (E1, E2, Velocityx, Velocityy);
@Override public void onshowpress (Motionevent e) {super.onshowpress (e);
@Override public boolean ondoubletapevent (Motionevent e) {return super.ondoubletapevent (e); @Override public boolean onsingletapconfirmed (Motionevent e) {if (Msingletaplistener!= null) mSi
Ngletaplistener.onsingletap (e);
Return super.onsingletapconfirmed (e);
} public interface Onmovinglistener {public void StartDrag ();
public void Stopdrag ();
Public interface Onsingletaplistener {public void Onsingletap (Motionevent e);
}
}
I have slightly modified the method of defining the Onsingletaplistener interface, and added the parameters of motionevent type to the Onsingletap callback method, so that we can make corresponding control according to the user's specific event content.
This is the entire content of this article, I hope to learn more about Android software programming help.