This article will imitate a design of the Win8 interface, one of the squares. The method is simple. Here itself to change the picture can become the interface you want.
1, first take a look at the custom Myimageview:
Package com.example.win8test;
Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import Android.graphics.Camera;
Import Android.graphics.Canvas;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.PaintFlagsDrawFilter;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.Drawable;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.widget.ImageView; @SuppressLint ("Handlerleak") public class Myimageview extends ImageView {public static final int Rotate_handler_messag
E_start = 1;
public static final int rotate_handler_message_turning = 2;
public static final int rotate_handler_message_turned = 3;
public static final int rotate_handler_message_reverse = 6;
public static final int scale_handler_message_start = 1;
public static final int scale_handler_message_turning = 2; public static finAl int scale_handler_message_turned = 3;
public static final int scale_handler_message_reverse = 6;
Private Boolean Isantialias = true;
Private Boolean scaleonly = false;
Private Boolean issizechanged = false;
Private Boolean isshowanimation = true;
private int rotatedegree = 10;
Private Boolean Isfirst = true;
private float Minscale = 0.95f;
private int vwidth;
private int vheight;
Private Boolean Isanimationfinish = True, Isactionmove = False, Isscale = false;
Private Camera Camera;
Boolean xbigy = false;
float Rolatex = 0;
float Rolatey = 0;
Onviewclick onclick = null;
Public Myimageview {Super (context);
Camera = new camera ();
Public Myimageview (context, AttributeSet attrs) {Super (context, attrs);
Camera = new camera ();
public void Setanimationonoff (Boolean oo) {isshowanimation = oo;
public void Setonclickintent (Onviewclick onclick) {this.onclick = onclick; } @SupprEsslint ("drawallocation") @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
if (isfirst) {Isfirst = false;
Init (); } canvas.setdrawfilter (New Paintflagsdrawfilter (0, Paint.anti_alias_flag |
Paint.filter_bitmap_flag));
public void init () {vwidth = GetWidth ()-Getpaddingleft ()-getpaddingright ();
Vheight = GetHeight ()-Getpaddingtop ()-Getpaddingbottom ();
drawable drawable = getdrawable ();
bitmapdrawable BD = (bitmapdrawable) drawable;
Bd.setantialias (Isantialias);
@Override public boolean ontouchevent (Motionevent event) {super.ontouchevent (event);
if (!isshowanimation) return true;
Switch (event.getaction () & Motionevent.action_mask) {case MotionEvent.ACTION_DOWN:float X = Event.getx ();
float Y = event.gety ();
Rolatex = VWIDTH/2-X;
Rolatey = VHEIGHT/2-Y; Xbigy = Math.Abs (Rolatex) > Math.Abs (rolatey)?
True:false; Isscale = X > Vwidth /3 && X < Vwidth * 2/3 && Y > VHEIGHT/3 && Y < vheight * 2/3;
Isactionmove = false;
if (Isscale) {if (isanimationfinish &&!issizechanged) {issizechanged = true;
Scale_handler.sendemptymessage (Scale_handler_message_start);
} else {if (scaleonly) {scale_handler.sendemptymessage (Scale_handler_message_start);
else {Rotate_handler. Sendemptymessage (Rotate_handler_message_start);
}} break;
Case MotionEvent.ACTION_MOVE:float x = Event.getx ();
Float y = event.gety ();
if (x > Vwidth | | | y > Vheight | | | x < 0 | | Y < 0) {Isactionmove = true;
else {isactionmove = false;
} break; Case MotionEvent.ACTION_UP:if (Isscale) {if (issizechanged) Scale_handler. Sendemptymessage (Scale
_handler_message_reverse); else {rotate_handler.sendemptymessage (rotate_handler_message_reverse);
} break;
return true;
Public interface Onviewclick {public void OnClick (); @SuppressLint ("Handlerleak") private Handler Rotate_handler = new Handler () {private Matrix matrix = new Matri
X ();
Private float count = 0;
Private Boolean clickguolv = false;
@Override public void Handlemessage (msg) {super.handlemessage);
Matrix.set (Getimagematrix ());
Switch (msg.what) {case rotate_handler_message_start:count = 0;
Beginrotate (Matrix, (Xbigy count:0), (Xbigy. 0:count));
Rotate_handler.sendemptymessage (rotate_handler_message_turning);
Break
Case Rotate_handler_message_turning:beginrotate (Matrix, (Xbigy count:0), (Xbigy. 0:count));
count++;
if (Count < Getdegree ()) {Rotate_handler. Sendemptymessage (rotate_handler_message_turning);
else {isanimationfinish = true;
} break; Case Rotate_handler_message_turNed:beginrotate (Matrix, (Xbigy count:0), (Xbigy. 0:count));
if (Count > 0) {rotate_handler. Sendemptymessage (rotate_handler_message_turned);
else {isanimationfinish = true;
if (!isactionmove && onclick!= null) {Onclick.onclick ();
}} count--;
count--;
Break
Case rotate_handler_message_reverse:count = Getdegree ();
Beginrotate (Matrix, (Xbigy count:0), (Xbigy. 0:count));
Rotate_handler.sendemptymessage (rotate_handler_message_turned);
Break
}
}
}; Private synchronized void beginrotate (Matrix matrix, float Rotatex, float rotatey) {//Bitmap BM = Getimagebitmap (
);
int scaleX = (int) (vwidth * 0.5f);
int scaleY = (int) (vheight * 0.5f);
Camera.save ();
Camera.rotatex (Rolatey > 0 Rotatey:-rotatey);
Camera.rotatey (Rolatex < 0 Rotatex:-rotatex);
Camera.getmatrix (matrix);
Camera.restore (); Control center Point if (RolAteX > 0 && rotatex!= 0) {matrix.pretranslate (-vwidth,-scaley);
Matrix.posttranslate (Vwidth, ScaleY);
else if (Rolatey > 0 && rotatey!= 0) {matrix.pretranslate (-scalex,-vheight);
Matrix.posttranslate (ScaleX, vheight);
else if (Rolatex < 0 && Rotatex!= 0) {matrix.pretranslate ( -0,-scaley);
Matrix.posttranslate (0, ScaleY);
else if (Rolatey < 0 && rotatey!= 0) {matrix.pretranslate (-scalex,-0);
Matrix.posttranslate (ScaleX, 0);
} setimagematrix (matrix);
Private Handler Scale_handler = new Handler () {private Matrix Matrix = new Matrix ();
private float S;
int count = 0;
@Override public void Handlemessage (msg) {super.handlemessage);
Matrix.set (Getimagematrix ());
Switch (msg.what) {case Scale_handler_message_start:if (!isanimationfinish) {return;
else {isanimationfinish = false; Issizechanged = true;
Count = 0;
s = (float) math.sqrt (math.sqrt (Minscale));
Beginscale (Matrix, s);
Scale_handler. Sendemptymessage (scale_handler_message_turning);
} break;
Case Scale_handler_message_turning:beginscale (Matrix, s);
if (Count < 4) {Scale_handler. Sendemptymessage (scale_handler_message_turning);
else {isanimationfinish = true;
if (!issizechanged &&!isactionmove && onclick!= null) {Onclick.onclick ();
}} count++;
Break Case Scale_handler_message_reverse:if (!isanimationfinish) {Scale_handler. Sendemptymessage (Scale_Han
Dler_message_reverse);
else {isanimationfinish = false;
Count = 0;
s = (float) math.sqrt (math.sqrt (1.0f/minscale));
Beginscale (Matrix, s);
Scale_handler. Sendemptymessage (scale_handler_message_turning);
Issizechanged = false;
} break;
}
}
};
Private synchronized void Beginscale (matrix matrix, float scale) {int scaleX = (int) (vwidth * 0.5f);
int scaleY = (int) (vheight * 0.5f);
Matrix.postscale (scale, scale, ScaleX, ScaleY);
Setimagematrix (matrix);
public int Getdegree () {return rotatedegree;
The public void Setdegree (int degree) {rotatedegree = degree;
public float Getscale () {return minscale;
public void Setscale (float scale) {Minscale = scale;
}
}
2, down, to see the layout
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android: Background= "@drawable/d" android:orientation= "vertical" tools:context= ".
Mainactivity "> <linearlayout android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:orientation= "Horizontal" > <com.example.win8test.myimageview android:id= "@+id/MyImageView01" an Droid:layout_width= "108DP" android:layout_height= "108DP" android:layout_margin= "16DP" android:scaletype= "Matri
X "android:src=" @drawable/fen "/> <com.example.win8test.myimageview android:id=" @+id/myimageview02 " Android:layout_width= "108DP" android:layout_height= "108DP" android:layout_margin= "16DP" android:scaletype= "Ma Trix "android:src=" @drawable/fen "/> </LinearLayout> <linearlayout android:lAyout_width= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <COM.E Xample.win8test.MyImageView android:id= "@+id/myimageview03" android:layout_width= "108DP" android:layout_height = "108DP" android:layout_margin= "16DP" android:scaletype= "Matrix" android:src= "@drawable/fen"/> <co M.example.win8test.myimageview android:id= "@+id/myimageview04" android:layout_width= "108DP" Android:layout_hei ght= "108DP" android:layout_margin= "16DP" android:scaletype= "Matrix" android:src= "@drawable/fen"/> </l inearlayout> <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and roid:orientation= "Horizontal" > <com.example.win8test.myimageview android:id= "@+id/myimageview05" Andro
Id:layout_width= "108DP" android:layout_height= "108DP" android:layout_margin= "16DP" android:scaletype= "Matrix" Android:src= "@drawabLe/fen "/> <com.example.win8test.myimageview android:id=" @+id/myimageview06 "android:layout_width=" 108d P "android:layout_height=" 108DP "android:layout_margin=" 16DP "android:scaletype=" Matrix "android:src=" @dra Wable/fen "/> </LinearLayout> </LinearLayout>
3, the above Picture button usage, here only to a Picture button added event:
Myimageview image_3d_1 = (myimageview) Findviewbyid (r.id.myimageview01);
Image_3d_1.setonclickintent (New Myimageview.onviewclick () {
@Override public
void OnClick () {
Toast.maketext (Mainactivity.this, "clicked"). Show
();
4, the effect
the finger presses in the middle is shrinks, the finger presses on the edge is has the angle to scroll
When the finger is pressed, the image that is pressed is smaller and the finger keeps moving, and the other picture buttons do not respond.
I hope this article will help you learn about Android software programming.