ÔÚViewÖнøÐÐÖØ»æ£¬Ö÷ÒªÊÇͨ¹ý¼ÆËã½Ç¶È¼°¾àÀëÀ´ÊµÏÖ¡£ÊµÏÖÀà´úÂëÈçÏ£º
package com.example.roatedemo;
public class RotateView extends View {
private Paint mPaint = new Paint();
private Bitmap bitmaplittele;//Öм䲻¶¯µÄͼƬ
private Bitmap bitmapBig;//ËæÊÖָת¶¯µÄͼƬ
private Bitmap bitmapOut;//ÍâΧ²»¶¯µÄͼƬ
// Ô²ÐÄ×ø±ê
private float mPointX = 0, mPointY = 0;
private int flag = 0;
// °ë¾¶
private int mRadius = 0;
// Ðýת½Ç¶È
private int mAngle = 0;
private int beginAngle = 0, currentAngle = 0;
private String TAG = "NewView";
int bitMap[] = { R.drawable.circle0, R.drawable.circle1, R.drawable.circle2 };
int imageIndex = 0;
boolean isUp = false,isTouch=false;
Context mContext;
RotateViewListener listener;
long beginTime,endTime;
Calendar now;
public RotateView(Context context, int px, int py, int radius,RotateViewListener listener) {
super(context);
mContext = context;
this.listener = listener;
mPointX = px;
mPointY = py;
mRadius = radius;
bitmaplittele = BitmapFactory.decodeResource(getResources(),
R.drawable.a1_pointer).copy(Bitmap.Config.ARGB_8888, true);
bitmapBig = BitmapFactory.decodeResource(getResources(), bitMap[0])
.copy(Bitmap.Config.ARGB_8888, true);
bitmapOut = BitmapFactory.decodeResource(getResources(),
R.drawable.bigcir).copy(Bitmap.Config.ARGB_8888, true);
setBackgroundResource(R.drawable.back);
Log.e(TAG, "RotateViewBegin");
}
@Override
public boolean dispatchTouchEvent(MotionEvent e) {
switch (e.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
now = Calendar.getInstance();
beginTime = now.getTimeInMillis();
beginAngle = computeCurrentAngle(e.getX(), e.getY());
isUp = false;
//Èç¹ûµã»÷´¥Ãþ·¶Î§ÔÚȦÍ⣬Ôò²»´¦Àí
if (getDistance(e.getX(), e.getY())>bitmapOut.getWidth()/2) {
isTouch=false;
}else {
isTouch=true;
}
return true;
case MotionEvent.ACTION_MOVE:
if (!isTouch) {
return true;
}
currentAngle = computeCurrentAngle(e.getX(), e.getY());
invalidate();
return true;
case MotionEvent.ACTION_UP:
isUp = true;
if (!isTouch) {
return true;
}
now = Calendar.getInstance();
endTime = now.getTimeInMillis();
if (SetClick(e.getX(), e.getY())) {
return true;
}
if (mAngle > 0) {
int count = mAngle / 120 + (mAngle % 120 > 60 1 : 0);
imageIndex = (imageIndex + count) % 3;
} else if (mAngle < 0) {
mAngle = -mAngle;
int count = mAngle / 120 + (mAngle % 120 > 60 1 : 0);
imageIndex = (imageIndex + 3 - count) % 3;
}
bitmapBig = BitmapFactory.decodeResource(getResources(),
bitMap[imageIndex]).copy(Bitmap.Config.ARGB_8888, true);
bitmapBig = adjustPhotoRotation(bitmapBig, imageIndex * 120);
invalidate();
if (mAngle >= 60) {
listener.onModChange(imageIndex);
}
return true;
}
return false;
}
@Override
public void onDraw(Canvas canvas) {
// Log.i(TAG, "onDraw");
// ´óÔ²
drawInCenter(canvas, bitmapOut, mPointX, mPointY, TAG);
// ÍâȦ
if (isUp) {
mAngle = 0;
} else {
mAngle = currentAngle - beginAngle;
}
Bitmap tempBig = adjustPhotoRotation(bitmapBig, mAngle);
// Log.i(TAG, "mAngle:"+mAngle);
drawInCenter(canvas, tempBig, mPointX, mPointY + 10, TAG);
// СԲ(ÖмäµÄÔ²ÐÄ)
drawInCenter(canvas, bitmaplittele, mPointX, mPointY - 10, TAG);
}
Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
if (orientationDegree == 0) {
return bm;
}
Matrix m = new Matrix();
m.setRotate(orientationDegree, (float) bm.getWidth() / 2,
(float) bm.getHeight()