話不多少亥贸,先上效果圖過(guò)一下癮。
經(jīng)我網(wǎng)上一搜捞高,沒(méi)有很好的實(shí)例來(lái)單獨(dú)實(shí)現(xiàn)以上效果氯材,所以只能老司機(jī)自己出馬了。為了實(shí)現(xiàn)以上效果硝岗,當(dāng)然離不開(kāi)我們對(duì)onTouchEvent
事件的處理了氢哮,老司機(jī)們應(yīng)該都懂的,開(kāi)始開(kāi)車型檀,嘟嘟~
下面來(lái)說(shuō)一下思路冗尤,首先上一張圖,讓我更方便地來(lái)為乘客們講解胀溺。
好了裂七,重要的都已經(jīng)在圖中說(shuō)了,下面只需要一個(gè)回調(diào)便可在外部獲取到旋轉(zhuǎn)的角度仓坞,獻(xiàn)上相關(guān)重要代碼背零,相信根據(jù)我的注釋,大家也都應(yīng)該明白了无埃。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/11/8
* desc :
* </pre>
*/
public class RotateImageView extends ImageView {
// 點(diǎn)擊在view中所在坐標(biāo)
private float x, y;
// view的中心點(diǎn)坐標(biāo)
private float ox, oy;
// view的寬徙瓶、高
private int w, h;
private Matrix matrix;
// ImageView的資源圖片
Bitmap mBitmap;
// 旋轉(zhuǎn)過(guò)的角度
public float totalDegree = 0;
// 是否初始化過(guò)
private boolean isInit = false;
// 旋轉(zhuǎn)角度改變的監(jiān)聽(tīng)器
private OnRotationChangeListener mListener;
public void setOnRotationChangeListener(OnRotationChangeListener listener) {
mListener = listener;
}
public RotateImageView(Context context) {
this(context, null);
}
public RotateImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RotateImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 已初始化完畢的話那就直接滾回老家去吧
if (isInit) return;
int measureWidth = getWidth();
int measureHeight = getHeight();
Log.d("blankj", "onMeasure: w: " + measureWidth + ", h: " + measureHeight);
// 如果沒(méi)有初始化過(guò),就初始化
if (measureWidth != 0 && measureHeight != 0 && !isInit) {
isInit = true;
mBitmap = ((BitmapDrawable) this.getDrawable()).getBitmap();
w = measureWidth;
h = measureHeight;
ox = w >> 1;
oy = h >> 1;
matrix = new Matrix();
// 獲取適配于ImageView的bitmap
mBitmap = Bitmap.createScaledBitmap(mBitmap, w, h, true);
}
}
@Override
protected void onDraw(Canvas canvas) {
Log.d("blankj", "onDraw");
// 判空以防崩潰
if (mBitmap == null) return;
canvas.save();
canvas.drawBitmap(mBitmap, matrix, null);
canvas.restore();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float nowX = event.getX();
float nowY = event.getY();
// 計(jì)算三邊的平方
float ab2 = (x - nowX) * (x - nowX) + (y - nowY) * (y - nowY);
float oa2 = (x - ox) * (x - ox) + (y - oy) * (y - oy);
float ob2 = (nowX - ox) * (nowX - ox) + (nowY - oy) * (nowY - oy);
// 根據(jù)兩向量的叉乘來(lái)判斷順逆時(shí)針
boolean isClockwise = ((x - ox) * (nowY - oy) - (y - oy) * (nowX - ox)) > 0;
// 根據(jù)余弦定理計(jì)算旋轉(zhuǎn)角的余弦值
double cosDegree = (oa2 + ob2 - ab2) / (2 * Math.sqrt(oa2) * Math.sqrt(ob2));
// 異常處理录语,因?yàn)樗愠鰜?lái)會(huì)有誤差絕對(duì)值可能會(huì)超過(guò)一倍啥,所以需要處理一下
if (cosDegree > 1) {
cosDegree = 1;
} else if (cosDegree < -1) {
cosDegree = -1;
}
// 計(jì)算弧度
double radian = Math.acos(cosDegree);
// 計(jì)算旋轉(zhuǎn)過(guò)的角度禾乘,順時(shí)針為正澎埠,逆時(shí)針為負(fù)
float degree = (float) (isClockwise ? Math.toDegrees(radian) : -Math.toDegrees(radian));
// 累加角度
totalDegree += degree;
matrix.setRotate(totalDegree, ox, oy);
// 更新觸摸點(diǎn)
x = nowX;
y = nowY;
// 回調(diào)把角度拋出
if (mListener != null) {
mListener.getRotation((int) totalDegree);
}
invalidate();
break;
case MotionEvent.ACTION_UP:
// 如果圖片需要復(fù)原原來(lái)角度,調(diào)用下方代碼
// matrix.setRotate(totalDegree, ox, oy);
// invalidate();
break;
}
return true;
}
public void reset() {
totalDegree = 0;
matrix.setRotate(totalDegree, ox, oy);
invalidate();
}
public interface OnRotationChangeListener {
void getRotation(int degree);
}
}
好了始藕,乘客們蒲稳,終點(diǎn)站已到,謝謝你們旅途中的陪伴伍派,老司機(jī)馬上要下班了江耀,啊哈哈哈,還是那句話:“你不買東西也是沒(méi)錢诉植,買了東西也是沒(méi)錢祥国,那就說(shuō)明買東西它不要錢啊”,祝大家雙十一購(gòu)物愉快晾腔。
最后獻(xiàn)上源碼地址:GetRotateDegree舌稀,歡迎大家star和fork。