2016-04-01 7 views
0

円を中心に回転させることはできません。ここでAndroidサークルがセンターから回転していませんか?

は私のxmlです:ここでは

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:gravity="center" 
    android:layout_gravity="center" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="15.0dip" 

    <FrameLayout 
     android:padding="0.0dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0.0dip"> 

     <ImageView 
      android:id="@+id/wheel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/wheel" /> 

     <ImageView 
      android:id="@+id/centre" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/centre" /> 

    </FrameLayout> 

</LinearLayout> 

は何が起こるかのスクリーンショットです:

screenshot

あなたがイメージから見ることができるように、円が中央ではなく、それはdoesnの中心を中心に回転する。円はまた

ofscreen押され、ここに私のjavaで取得します。

私はあなたのXMLで考える
public class MainActivity 
    extends Activity 
    implements View.OnTouchListener { 
    private static final String TAG = "MainActivity"; 
    ImageView circleView; 
    double currentRotationDegrees; 
    double cx; 
    double cy; 
    float mPreviousX; 
    float mPreviousY; 
    private Matrix rotationMatrix; 

    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     this.setContentView(R.layout.activity_main); 
     this.circleView = (ImageView)this.findViewById(R.id.wheel); 
     this.circleView.setOnTouchListener((View.OnTouchListener) this); 
     this.circleView.setScaleType(ImageView.ScaleType.MATRIX); 
     if (this.rotationMatrix == null) { 
     this.rotationMatrix = new Matrix(); 
     } 
     this.rotationMatrix.reset(); 
     this.currentRotationDegrees = 0.0; 
    } 

/* 
* Enabled aggressive block sorting 
*/ 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     this.cx = (double)this.circleView.getWidth()/2.0; 
     this.cy = (double)this.circleView.getHeight()/2.0; 
     float f = motionEvent.getX(); 
     float f2 = motionEvent.getY(); 

     Log.d((String)"MainActivity", (String)("Got x: " + f + " and y: " + f2)); 
     switch (motionEvent.getAction()) { 

      case 1: { 
       Log.d((String)"MainActivity", (String)("Current rotation degrees = " + this.currentRotationDegrees)); 
       double d = this.currentRotationDegrees % 30.0; 
       Log.d((String)"MainActivity", (String)("Rotation modulus = " + d)); 
       if (d == 0.0) break; 
       double d5 = d < 15.0 ? -1.0 * d : 30.0 - d; 
       this.rotationMatrix.postRotate((float)d5, (float)this.cx, (float)this.cy); 
       this.circleView.setImageMatrix(this.rotationMatrix); 
       this.currentRotationDegrees -= d; 
      } 

      case 2: { 
       Log.d((String)"MainActivity", (String)"Calculating rotation.."); 
       double d = (double)f - this.cx; 
       double d2 = Math.atan2((double)f2 - this.cy, d); 
       double d3 = (double)this.mPreviousX - this.cx; 
       double d4 = Math.toDegrees(d2 - Math.atan2((double)this.mPreviousY - this.cy, d3)); 
       Log.d((String)"MainActivity", (String)("Rotation degrees: " + d4)); 
       this.rotationMatrix.postRotate((float) d4, (float) this.cx, (float) this.cy); 
       this.circleView.setImageMatrix(this.rotationMatrix); 
       this.currentRotationDegrees = d4 + this.currentRotationDegrees; 
      } 
      default: { 
       break; 
      } 

     } 

     this.mPreviousX = f; 
     this.mPreviousY = f2; 
     return true; 
    } 
} 

答えて

0

このコードを置き換えます。

 android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0.0dip"> 

     <ImageView 
      android:id="@+id/image_camelot_wheel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@drawable/wheel" /> 

     <ImageView 
      android:id="@+id/image_camelot_arrows" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:src="@drawable/centre" /> 
0

、のLinearLayoutは外のサークルであり、あなたはのLinearLayoutのlayout_marginを設定します。円を中心に回転させたい場合は、LinearLayout.sの幅と余白を2で割ります。

+0

回答をより明確にするためのコードを入力してください。 –

関連する問題