SoFunction
Updated on 2025-03-01

Android realizes the function of image rotation at your fingers

This article shares the specific code for Android to implement the rotation function of the image with your finger for your reference. The specific content is as follows

Repainting in View is mainly achieved by calculating angles and distances. The implementation class code is as follows:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class RotateView extends View { 
  private Paint mPaint = new Paint(); 
  private Bitmap bitmaplittele;// Pictures that are still moving in the middle  private Bitmap bitmapBig;// Pictures of finger rotation  private Bitmap bitmapOut;//Pictures that are not moving outside  // Center coordinates  private float mPointX = 0, mPointY = 0; 
 
  private int flag = 0; 
  // Radius  private int mRadius = 0; 
  // Rotation angle  private int mAngle = 0; 
  private int beginAngle = 0, currentAngle = 0; 
  private String TAG = "NewView"; 
  int bitMap[] = { .circle0, .circle1, .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; 
     = listener; 
    mPointX = px; 
    mPointY = py; 
    mRadius = radius; 
    bitmaplittele = (getResources(), 
        .a1_pointer).copy(.ARGB_8888, true); 
    bitmapBig = (getResources(), bitMap[0]) 
        .copy(.ARGB_8888, true); 
    bitmapOut = (getResources(), 
        ).copy(.ARGB_8888, true); 
    setBackgroundResource(); 
    (TAG, "RotateViewBegin");    
  } 
 
  @Override 
  public boolean dispatchTouchEvent(MotionEvent e) { 
    switch (() & MotionEvent.ACTION_MASK) { 
    case MotionEvent.ACTION_DOWN: 
      now = (); 
      beginTime = (); 
      beginAngle = computeCurrentAngle((), ()); 
      isUp = false; 
      //If the click touch range is outside the circle, it will not be processed      if (getDistance((), ())>()/2) { 
        isTouch=false; 
      }else { 
        isTouch=true; 
      } 
      return true; 
    case MotionEvent.ACTION_MOVE: 
      if (!isTouch) { 
        return true; 
      } 
      currentAngle = computeCurrentAngle((), ()); 
      invalidate(); 
      return true; 
    case MotionEvent.ACTION_UP: 
      isUp = true; 
      if (!isTouch) { 
        return true; 
      } 
      now = (); 
      endTime = (); 
      if (SetClick((), ())) { 
        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 = (getResources(), 
          bitMap[imageIndex]).copy(.ARGB_8888, true); 
      bitmapBig = adjustPhotoRotation(bitmapBig, imageIndex * 120); 
      invalidate(); 
      if (mAngle >= 60) { 
        (imageIndex); 
      }       
      return true; 
    } 
 
    return false; 
  } 
 
  @Override 
  public void onDraw(Canvas canvas) { 
    // (TAG, "onDraw"); 
    // Big circle    drawInCenter(canvas, bitmapOut, mPointX, mPointY, TAG); 
    // Outer ring    if (isUp) { 
      mAngle = 0; 
    } else { 
      mAngle = currentAngle - beginAngle; 
    } 
 
    Bitmap tempBig = adjustPhotoRotation(bitmapBig, mAngle); 
    // (TAG, "mAngle:"+mAngle); 
    drawInCenter(canvas, tempBig, mPointX, mPointY + 10, TAG); 
    // Small circle (center of the middle)    drawInCenter(canvas, bitmaplittele, mPointX, mPointY - 10, TAG); 
  } 
 
  Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) { 
    if (orientationDegree == 0) { 
      return bm; 
    } 
    Matrix m = new Matrix(); 
    (orientationDegree, (float) () / 2, 
        (float) () / 2); 
 
    try { 
      Bitmap bm1 = (bm, 0, 0, (), 
          (), m, true); 
 
      return bm1; 
 
    } catch (OutOfMemoryError ex) { 
    } 
 
    return null; 
 
  } 
 
  private void drawInCenter(Canvas canvas, Bitmap bitmap, float left, 
      float top, String text) { 
    (bitmap, left - () / 2, 
        top - () / 2, null); 
  } 
 
  // Change the position of the child control and recalculate the angle  private int computeCurrentAngle(float x, float y) { 
    // Calculate the angle based on the center coordinates    float distance = (float) Math 
        .sqrt(((x - mPointX) * (x - mPointX) + (y - mPointY) 
            * (y - mPointY))); 
    int degree = (int) (((x - mPointX) / distance) * 180 / ); 
    if (y < mPointY) { 
      degree = -degree; 
    } 
    if (degree < 0) { 
      degree += 360; 
    } 
 
    // ("RoundSpinView", "x:" + x + ",y:" + y + ",degree:" + degree); 
    return degree; 
  } 
 
  // Get the distance from the center of the circle  private float getDistance(float x, float y) { 
    // Calculate the angle based on the center coordinates    float distance = (float) Math 
        .sqrt(((x - mPointX) * (x - mPointX) + (y - mPointY) 
            * (y - mPointY))); 
    return distance; 
  } 
 
  //Click  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 < () / 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;         
      } 
      //Callback to the main interface for processing.      (mod); 
    } 
    return true; 
  } 
   
  public interface RotateViewListener {     
    void onModClick(int mode); 
    void onModChange(int mode); 
  } 
}

Calling code in Activity:

package ; 
 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class MainActivity extends Activity implements RotateViewListener{ 
 
  RotateView rotateView; 
  String TAG="MainActivity"; 
  Context mContext; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    mContext = this; 
    int height,width; 
    DisplayMetrics displayMetrics = new DisplayMetrics();  
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
    height = ; 
    width = ; 
    (TAG, "height:"+height); 
    (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 was clicked", "2 was clicked","3 clicked" }; 
    (mContext, clickStrings[mode], 0).show(); 
     
  } 
  @Override 
  public void onModChange(int mode) { 
    String[] clickStrings = new String[] { "Switch to 1", "Switch to 2","Switch to 3" }; 
    (mContext, clickStrings[mode], 0).show();  
  } 
 
 
} 

The layout file is the default.

In addition to implementing image rotation, it also realizes cutting the image into 3 parts, each part is 120 degrees, and switching a mode for each part of 120 degrees. Clicking on each part will have a response event callback to the main interface. Confidentiality is involved, and the pictures are not given here.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.