這篇文章主要為大家詳細介紹了怎么在Android中實現(xiàn)一個圖片隨手指旋轉(zhuǎn)功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,發(fā)現(xiàn)的小伙伴們可以參考一下:
成都創(chuàng)新互聯(lián)公司始終堅持【策劃先行,效果至上】的經(jīng)營理念,通過多達10年累計超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)營銷推廣解決方案,現(xiàn)已廣泛運用于各行各業(yè)的客戶,其中包括:柴油發(fā)電機等企業(yè),備受客戶稱贊。在View中進行重繪,主要是通過計算角度及距離來實現(xiàn)。實現(xiàn)類代碼如下:
package com.example.roatedemo; import java.util.Calendar; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class RotateView extends View { private Paint mPaint = new Paint(); private Bitmap bitmaplittele;//中間不動的圖片 private Bitmap bitmapBig;//隨手指轉(zhuǎn)動的圖片 private Bitmap bitmapOut;//外圍不動的圖片 // 圓心坐標 private float mPointX = 0, mPointY = 0; private int flag = 0; // 半徑 private int mRadius = 0; // 旋轉(zhuǎn)角度 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() / 2); try { Bitmap bm1 = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true); return bm1; } catch (OutOfMemoryError ex) { } return null; } private void drawInCenter(Canvas canvas, Bitmap bitmap, float left, float top, String text) { canvas.drawBitmap(bitmap, left - bitmap.getWidth() / 2, top - bitmap.getHeight() / 2, null); } // 子控件位置改變重新計算角度 private int computeCurrentAngle(float x, float y) { // 根據(jù)圓心坐標計算角度 float distance = (float) Math .sqrt(((x - mPointX) * (x - mPointX) + (y - mPointY) * (y - mPointY))); int degree = (int) (Math.acos((x - mPointX) / distance) * 180 / Math.PI); if (y < mPointY) { degree = -degree; } if (degree < 0) { degree += 360; } // Log.i("RoundSpinView", "x:" + x + ",y:" + y + ",degree:" + degree); return degree; } // 獲取距離圓心的距離 private float getDistance(float x, float y) { // 根據(jù)圓心坐標計算角度 float distance = (float) Math .sqrt(((x - mPointX) * (x - mPointX) + (y - mPointY) * (y - mPointY))); return distance; } //點擊 private boolean SetClick(float x, float y) { float distance = getDistance(x, y); if (mAngle>10||mAngle<-10) { return false; }else if(endTime-beginTime>1000){ return false; } if (distance < bitmapBig.getWidth() / 2) { int mod = 0; if (beginAngle < 90 || 330 < beginAngle) { mod = (imageIndex+3-1)%3; } else if (90 < beginAngle && 210 > beginAngle) { mod = (imageIndex+3-2)%3; } else{ mod = imageIndex; } //回調(diào)到主界面進行處理。 listener.onModClick(mod); } return true; } public interface RotateViewListener { void onModClick(int mode); void onModChange(int mode); } }
Activity中調(diào)用代碼:
package com.example.roatedemo; import com.example.roatedemo.RotateView.RotateViewListener; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.widget.Toast; public class MainActivity extends Activity implements RotateViewListener{ RotateView rotateView; String TAG="MainActivity"; Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; int height,width; DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); height = displayMetrics.heightPixels; width = displayMetrics.widthPixels; Log.i(TAG, "height:"+height); Log.i(TAG, "width:"+width); rotateView = new RotateView(getApplicationContext(), width/2, height/3, 150,this); setContentView(rotateView); } @Override public void onModClick(int mode) { String[] clickStrings = new String[] { "1被點擊", "2被點擊","3被點擊" }; Toast.makeText(mContext, clickStrings[mode], 0).show(); } @Override public void onModChange(int mode) { String[] clickStrings = new String[] { "切換到1", "切換到2","切換到3" }; Toast.makeText(mContext, clickStrings[mode], 0).show(); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當前名稱:怎么在Android中實現(xiàn)一個圖片隨手指旋轉(zhuǎn)功能-創(chuàng)新互聯(lián)
文章路徑:http://jinyejixie.com/article40/egjeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、靜態(tài)網(wǎng)站、網(wǎng)站策劃、全網(wǎng)營銷推廣、軟件開發(fā)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容