2015-05-11 14 views
10

Tôi có một Framelayout mà bổ sung bốn ImageView khi chạy cũng như trong trung tâm nó có chứa hình ảnh chính mà người dùng có thể thực hiện thao tác khác nhau nhưng tôi phải đối mặt với vấn đề với quan điểm bố trí xoayXoay Khung bố trí, trong đó có các nút động

hiện trên liên lạc của nút xoay tôi đang làm điều này

public void setRotateListener() { 
    mRotateImage.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      float x = event.getX(0); 
      float y = event.getY(0); 
      float theta = getTheta(x, y); 

      switch (event.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_POINTER_DOWN: 
       theta_old = theta; 
       break; 
      case MotionEvent.ACTION_MOVE: 
       float delta_theta = theta - theta_old; 
       theta_old = theta; 
       int direction = (delta_theta > 0) ? 1 : -1; 
       angle += 3 * direction; 

       Log.d("Tag", "rotate angle : " + obj.getHeight()); 
       obj.setRotation(angle); 
       notifyListener(direction); 
       break; 
      } 
      return true; 
     } 
    }); 
} 

private float getTheta(float x, float y) { 
    float sx = x - (obj.getWidth()/2.0f); 
    float sy = y - (obj.getHeight()/2.0f); 

    float length = (float) Math.sqrt(sx * sx + sy * sy); 
    float nx = sx/length; 
    float ny = sy/length; 
    float theta = (float) Math.atan2(ny, nx); 

    final float rad2deg = (float) (180.0/Math.PI); 
    float thetaDeg = theta * rad2deg; 

    return (thetaDeg < 0) ? thetaDeg + 360.0f : thetaDeg; 
} 

nhưng tôi không thể có được kết quả mong đợi tôi đã tham khảo liên kết này cũng https://github.com/rprouse/XkcdClock cũng như cố gắng xoay bằng cử chỉ và hình ảnh động quá nhưng có vẻ như không làm việc theo di chuyển của tôi trên màn hình while on touch the rotate button wants to rotate whole view in both direction clock and anticlockwise

+0

tôi không thể tìm ra cách bạn wa nt để xoay và vấn đề hiện tại của bạn là gì. – mmlooloo

+0

từ ảnh trên, bạn có thể thấy nút xoay trên ảnh thực tế nút đó được thêm vào khung động nên tôi muốn xoay đồng hồ cũng như bố cục toàn khung ngược, vì vậy trong bất kỳ hành động nào của người dùng sẽ di chuyển trên bố cục, toàn bộ khung và bên trong hình ảnh cũng được xoay theo hướng đó.Và vấn đề hiện tại của tôi là nó không thể xoay mịn ngay bây giờ theo bất kỳ hướng nào. @ mmlooloo –

+0

Bố cục khung của bạn ở đâu trên ảnh chụp màn hình này? nó có chụp toàn bộ màn hình hay chỉ là hình vuông màu xanh? chính xác thì vấn đề của bạn là gì? bạn có xoay xở nhưng nó không đủ mượt mà không? hoặc bạn thậm chí không có bất kỳ vòng quay nào? – user2641570

Trả lời

6

Tôi có thiết kế Bố cục có thể hoạt động theo nhu cầu của bạn. Tải Demo here

Java file

import android.annotation.SuppressLint; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.ColorMatrixColorFilter; 
import android.graphics.Paint; 
import android.view.GestureDetector; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 

public class ClipArt extends RelativeLayout { 
    int baseh; 
    int basew; 
    int basex; 
    int basey; 
    ImageButton btndel; 
    ImageButton btnrot; 
    ImageButton btnscl; 
    RelativeLayout clip; 
    Context cntx; 
    boolean freeze = false; 
    int h; 
    int i; 
    ImageView image; 
    String imageUri; 
    boolean isShadow; 
    int iv; 
    RelativeLayout layBg; 
    RelativeLayout layGroup; 
    RelativeLayout.LayoutParams layoutParams; 
    public LayoutInflater mInflater; 
    int margl; 
    int margt; 
    float opacity = 1.0F; 
    Bitmap originalBitmap; 
    int pivx; 
    int pivy; 
    int pos; 
    Bitmap shadowBitmap; 
    float startDegree; 
    String[] v; 

    public ClipArt(Context paramContext) { 
     super(paramContext); 
     cntx = paramContext; 
     layGroup = this; 

     basex = 0; 
     basey = 0; 
     pivx = 0; 
     pivy = 0; 

     mInflater = ((LayoutInflater) paramContext.getSystemService("layout_inflater")); 
     mInflater.inflate(R.layout.clipart, this, true); 
     btndel = ((ImageButton) findViewById(R.id.del)); 
     btnrot = ((ImageButton) findViewById(R.id.rotate)); 
     btnscl = ((ImageButton) findViewById(R.id.sacle)); 

     layoutParams = new RelativeLayout.LayoutParams(250, 250); 
     layGroup.setLayoutParams(layoutParams); 
     image = ((ImageView) findViewById(R.id.clipart)); 
     image.setImageResource(R.drawable.ic_launcher); 

     setOnTouchListener(new View.OnTouchListener() { 
      final GestureDetector gestureDetector = new GestureDetector(ClipArt.this.cntx, 
        new GestureDetector.SimpleOnGestureListener() { 
       public boolean onDoubleTap(MotionEvent paramAnonymous2MotionEvent) { 
        return false; 
       } 
      }); 

      public boolean onTouch(View paramAnonymousView, MotionEvent event) { 
       if (!ClipArt.this.freeze) { 
        switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         layGroup.invalidate(); 
         gestureDetector.onTouchEvent(event); 

         layGroup.performClick(); 
         basex = ((int) (event.getRawX() - layoutParams.leftMargin)); 
         basey = ((int) (event.getRawY() - layoutParams.topMargin)); 
         break; 
        case MotionEvent.ACTION_MOVE: 
         int i = (int) event.getRawX(); 
         int j = (int) event.getRawY(); 
         layBg = ((RelativeLayout) getParent()); 
         if ((i - basex > -(layGroup.getWidth() * 2/3)) 
           && (i - basex < layBg.getWidth() - layGroup.getWidth()/3)) { 
          layoutParams.leftMargin = (i - basex); 
         } 
         if ((j - basey > -(layGroup.getHeight() * 2/3)) 
           && (j - basey < layBg.getHeight() - layGroup.getHeight()/3)) { 
          layoutParams.topMargin = (j - basey); 
         } 
         layoutParams.rightMargin = -1000; 
         layoutParams.bottomMargin = -1000; 
         layGroup.setLayoutParams(layoutParams); 
         break; 

        } 

        return true; 
       } 
       return true; 
      } 
     }); 
     this.btnscl.setOnTouchListener(new View.OnTouchListener() { 
      @SuppressLint({ "NewApi" }) 
      public boolean onTouch(View paramAnonymousView, MotionEvent event) { 
       if (!ClipArt.this.freeze) { 
        int j = (int) event.getRawX(); 
        int i = (int) event.getRawY(); 
        layoutParams = (RelativeLayout.LayoutParams) layGroup.getLayoutParams(); 
        switch (event.getAction()) { 

        case MotionEvent.ACTION_DOWN: 
         ClipArt.this.layGroup.invalidate(); 
         ClipArt.this.basex = j; 
         ClipArt.this.basey = i; 
         ClipArt.this.basew = ClipArt.this.layGroup.getWidth(); 
         ClipArt.this.baseh = ClipArt.this.layGroup.getHeight(); 
         int[] loaction = new int[2]; 
         layGroup.getLocationOnScreen(loaction); 
         margl = layoutParams.leftMargin; 
         margt = layoutParams.topMargin; 
         break; 
        case MotionEvent.ACTION_MOVE: 

         float f2 = (float) Math.toDegrees(Math.atan2(i - ClipArt.this.basey, j - ClipArt.this.basex)); 
         float f1 = f2; 
         if (f2 < 0.0F) { 
          f1 = f2 + 360.0F; 
         } 
         j -= ClipArt.this.basex; 
         int k = i - ClipArt.this.basey; 
         i = (int) (Math.sqrt(j * j + k * k) 
           * Math.cos(Math.toRadians(f1 - ClipArt.this.layGroup.getRotation()))); 
         j = (int) (Math.sqrt(i * i + k * k) 
           * Math.sin(Math.toRadians(f1 - ClipArt.this.layGroup.getRotation()))); 
         k = i * 2 + ClipArt.this.basew; 
         int m = j * 2 + ClipArt.this.baseh; 
         if (k > 150) { 
          layoutParams.width = k; 
          layoutParams.leftMargin = (ClipArt.this.margl - i); 
         } 
         if (m > 150) { 
          layoutParams.height = m; 
          layoutParams.topMargin = (ClipArt.this.margt - j); 
         } 
         ClipArt.this.layGroup.setLayoutParams(layoutParams); 
         ClipArt.this.layGroup.performLongClick(); 
         break; 
        } 
        return true; 

       } 
       return ClipArt.this.freeze; 
      } 
     }); 
     this.btnrot.setOnTouchListener(new View.OnTouchListener() { 
      @SuppressLint({ "NewApi" }) 
      public boolean onTouch(View paramAnonymousView, MotionEvent event) { 
       if (!ClipArt.this.freeze) { 
        layoutParams = (RelativeLayout.LayoutParams) ClipArt.this.layGroup.getLayoutParams(); 
        ClipArt.this.layBg = ((RelativeLayout) ClipArt.this.getParent()); 
        int[] arrayOfInt = new int[2]; 
        layBg.getLocationOnScreen(arrayOfInt); 
        int i = (int) event.getRawX() - arrayOfInt[0]; 
        int j = (int) event.getRawY() - arrayOfInt[1]; 
        switch (event.getAction()) { 

        case MotionEvent.ACTION_DOWN: 
         ClipArt.this.layGroup.invalidate(); 
         ClipArt.this.startDegree = layGroup.getRotation(); 
         ClipArt.this.pivx = (layoutParams.leftMargin + ClipArt.this.getWidth()/2); 
         ClipArt.this.pivy = (layoutParams.topMargin + ClipArt.this.getHeight()/2); 
         ClipArt.this.basex = (i - ClipArt.this.pivx); 
         ClipArt.this.basey = (ClipArt.this.pivy - j); 
         break; 

        case MotionEvent.ACTION_MOVE: 
         int k = ClipArt.this.pivx; 
         int m = ClipArt.this.pivy; 
         j = (int) (Math.toDegrees(Math.atan2(ClipArt.this.basey, ClipArt.this.basex)) 
           - Math.toDegrees(Math.atan2(m - j, i - k))); 
         i = j; 
         if (j < 0) { 
          i = j + 360; 
         } 
         ClipArt.this.layGroup.setRotation((ClipArt.this.startDegree + i) % 360.0F); 
         break; 
        } 

        return true; 
       } 
       return ClipArt.this.freeze; 
      } 
     }); 
     this.btndel.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View paramAnonymousView) { 
       if (!ClipArt.this.freeze) { 
        layBg = ((RelativeLayout) ClipArt.this.getParent()); 
        layBg.performClick(); 
        layBg.removeView(ClipArt.this.layGroup); 
       } 
      } 
     }); 
    } 


    public void disableAll() { 
     this.btndel.setVisibility(4); 
     this.btnrot.setVisibility(4); 
     this.btnscl.setVisibility(4); 
    } 

    public ImageView getImageView() { 
     return this.image; 
    } 

    public void setFreeze(boolean paramBoolean) { 
     this.freeze = paramBoolean; 
    } 
} 

tập tin Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> 
    <ImageButton android:id="@+id/rotate" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/rotation"/> 
    <ImageButton android:id="@+id/sacle" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/pointer"/> 
    <ImageButton android:id="@+id/del" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:adjustViewBounds="true" android:background="@android:color/transparent" android:scaleType="fitCenter" android:src="@drawable/close"/> 
    <ImageView android:id="@+id/clipart" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"/> 
    </RelativeLayout> 

và hình ảnh đưa vào drawable

enter image description hereenter image description hereenter image description here

+0

Không thể giải quyết phương thức visiball(); Trên thực tế phương pháp này không được định nghĩa ở bất kỳ đâu. Vì vậy, ... – Khan

+0

bạn muốn gì theo phương pháp này? @KhanSquare – RBK

+1

Tôi muốn làm một cái gì đó giống như bạn đăng ở đây trong câu trả lời của bạn. Vì vậy, tôi đã sao chép điều khiển tùy chỉnh của bạn trong dự án của tôi như bạn đã đề xuất. Nhưng phương thức visiball() không được định nghĩa ở bất kỳ đâu, đó là lý do tại sao tôi gặp lỗi "Không thể giải quyết phương thức". và phương thức này được gọi trong onTouch (Xem paramAnonymousView, sự kiện MotionEvent) trong lớp ClipArt của bạn. – Khan

Các vấn đề liên quan