仿win8的磁贴效果
package com.dreamlive.testrolatea;
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.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
/**
* 自定义ImageView
*
* @author dreamlive
*
*/
public class MyImageView1 extends ImageView {
private boolean onAnimation = true;
private int rotateDegree = 10;
private boolean isFirst = true;
private float minScale = 0.95f;
private int vWidth;
private int vHeight;
private boolean isFinish = true, isActionMove = false, isScale = false;
private Camera camera;
boolean XBigY = false;
float RolateX = 0;
float RolateY = 0;
OnViewClick onclick = null;
public MyImageView1(Context context) {
super(context);
camera = new Camera();
}
public MyImageView1(Context context, AttributeSet attrs) {
super(context, attrs);
camera = new Camera();
}
public MyImageView1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
camera = new Camera();
}
public void SetAnimationOnOff(boolean isAnimation) {
onAnimation = isAnimation;
}
public void setOnClickIntent(OnViewClick onclick) {
this.onclick = onclick;
}
@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();
BitmapDrawable bd = (BitmapDrawable) getDrawable();
bd.setAntiAlias(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!onAnimation) {
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) {
handler.sendEmptyMessage(1);
} else {
rolateHandler.sendEmptyMessage(1);
}
break;
case MotionEvent.ACTION_MOVE:
float x1 = event.getX();
float y1 = event.getY();
if (x1 > vWidth || y1 > vHeight || x1 < 0 || y1 < 0) {
isActionMove = true;
} else {
isActionMove = false;
}
break;
case MotionEvent.ACTION_UP:
if (isScale) {
handler.sendEmptyMessage(6);
} else {
rolateHandler.sendEmptyMessage(6);
}
break;
default:
break;
}
return true;
}
public interface OnViewClick {
public void onClick();
}
private Handler rolateHandler = new Handler() {
private Matrix matrix = new Matrix();
private float count = 0;
public void handleMessage(android.os.Message msg) {
matrix.set(getImageMatrix());
switch (msg.what) {
case 1:
count = 0;
BeginRolate(matrix, (XBigY ? count : 0), (XBigY ? 0 : count));
rolateHandler.sendEmptyMessage(2);
break;
case 2:
BeginRolate(matrix, (XBigY ? count : 0), (XBigY ? 0 : count));
if (count < getDegree()) {
rolateHandler.sendEmptyMessage(2);
} else {
isFinish = true;
}
count++;
count++;
break;
case 3:
BeginRolate(matrix, (XBigY ? count : 0), (XBigY ? 0 : count));
if (count > 0) {
rolateHandler.sendEmptyMessage(3);
} else {
isFinish = true;
if (!isActionMove && onclick != null) {
onclick.onClick();
}
}
count--;
count--;
break;
case 6:
count = getDegree();
BeginRolate(matrix, (XBigY ? count : 0), (XBigY ? 0 : count));
rolateHandler.sendEmptyMessage(3);
break;
default:
break;
}
};
};
private synchronized void BeginRolate(Matrix matrix, float rolateX,
float rolateY) {
// Bitmap bm = getImageBitmap();
int scaleX = (int) (vWidth * 0.5f);
int scaleY = (int) (vHeight * 0.5f);
camera.save();
camera.rotateX(RolateY > 0 ? rolateY : -rolateY);
camera.rotateY(RolateX < 0 ? rolateX : -rolateX);
camera.getMatrix(matrix);
camera.restore();
// 控制中心点
if (RolateX > 0 && rolateX != 0) {
matrix.preTranslate(-vWidth, -scaleY);
matrix.postTranslate(vWidth, scaleY);
} else if (RolateY > 0 && rolateY != 0) {
matrix.preTranslate(-scaleX, -vHeight);
matrix.postTranslate(scaleX, vHeight);
} else if (RolateX < 0 && rolateX != 0) {
matrix.preTranslate(-0, -scaleY);
matrix.postTranslate(0, scaleY);
} else if (RolateY < 0 && rolateY != 0) {
matrix.preTranslate(-scaleX, -0);
matrix.postTranslate(scaleX, 0);
}
setImageMatrix(matrix);
}
private Handler handler = new Handler() {
private Matrix matrix = new Matrix();
private float s;
int count = 0;
public void handleMessage(android.os.Message msg) {
matrix.set(getImageMatrix());
switch (msg.what) {
case 1:
if (!isFinish) {
return;
} else {
isFinish = false;
count = 0;
s = (float) Math.sqrt(Math.sqrt(minScale));
BeginScale(matrix, s);
handler.sendEmptyMessage(2);
}
break;
case 2:
BeginScale(matrix, s);
if (count < 4) {
handler.sendEmptyMessage(2);
} else {
isFinish = true;
if (isActionMove && onclick != null) {
onclick.onClick();
}
}
count++;
break;
case 6:
if (!isFinish) {
handler.sendEmptyMessage(6);
} else {
isFinish = false;
count = 0;
s = (float) Math.sqrt(Math.sqrt(1.0f / minScale));
BeginScale(matrix, s);
handler.sendEmptyMessage(2);
}
break;
default:
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;
}
public void setDegree(int degree) {
rotateDegree = degree;
}
public float getScale() {
return minScale;
}
public void setScale(float scale) {
minScale = scale;
}
}
2、
算角度的
package com.dreamlive.testrolatea;
/**
* 计算伸缩的范围角度
*
* @author dreamlive
*
*/
public class Record {
public int getDegrees(float a, float b) {
float dx = a;
float x = b / 4;
float x2 = 3 * b / 4;
if (dx < x / 10) {
return 10;
} else if (dx < x / 5) {
return 9;
} else if (dx < x * 3 / 10) {
return 8;
} else if (dx < x * 2 / 5) {
return 7;
} else if (dx < x * 5 / 10) {
return 6;
} else if (dx < x * 3 / 5) {
return 5;
} else if (dx < x * 7 / 10) {
return 4;
} else if (dx < x * 4 / 5) {
return 3;
} else if (dx < x * 9 / 10) {
return 2;
} else if (dx < x) {
return 2;
} else if (dx - x2 > x * 9 / 10) {
return 10;
} else if (dx - x2 > x * 4 / 5) {
return 9;
} else if (dx - x2 > x * 7 / 10) {
return 8;
} else if (dx - x2 > x * 3 / 5) {
return 7;
} else if (dx - x2 > x * 5 / 10) {
return 6;
} else if (dx - x2 > x * 2 / 5) {
return 5;
} else if (dx - x2 > x * 3 / 10) {
return 4;
} else if (dx - x2 > x / 5) {
return 3;
} else if (dx - x2 > x / 10) {
return 2;
}
return 10;
}
}