当前位置:操作系统 > 安卓/Android >>

Android实现图片随手指旋转功能

在View中进行重绘,主要是通过计算角度及距离来实现。实现类代码如下:


[java]
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;//随手指转动的图片  
    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 = BitmapFactor

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,