2017-03-24 6 views
0

私はオブジェクトを別の点の周りに回転(アニメーション)したいですが、オブジェクトを回転させながら同じ向きにします。私はそれをどうやって行うことができますか?Androidの関数を呼び出すだけで簡単にやることができますか?または数式を使用する必要がありますか?同じ方向を保って別の点をどのように回転させるか - アニメーションAndroid

I want this result

マイコード:

public class MainActivity extends AppCompatActivity { 

    Button button; 
    float radius = 195.0f; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button = (Button) findViewById(R.id.t); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       rotate() 
      } 
     }); 

     button.setX(button.getX() + radius); 

    } 
} 

マイレイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <Button 
     android:id="@+id/t" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:text="R" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 
+0

あなたはトライしていますこれまでのEd? – rckrd

+0

ビュー自体を回転させているので、円形のパスで移動する必要があります。これを見て:http://stackoverflow.com/questions/20281265/move-an-image-in-circular-path-in-android – rckrd

+0

あなたの助けをありがとう、私のfauft私はそのような種類の理解していない回転は循環経路における「平行移動」である。 – DeveloperBeginner

答えて

0

ソリューション:回転のこの種のは、単純な "円形経路で翻訳" である:

private void rotate(){ 

      final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 360.0f); 
      animator.setDuration(5000); 
      animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
        @Override 
        public void onAnimationUpdate(ValueAnimator valueAnimator) { 
        float value = (Float) animator.getAnimatedValue(); 

        DisplayMetrics metrics = new DisplayMetrics(); 
        getWindowManager().getDefaultDisplay().getMetrics(metrics); 

        float cx = metrics.widthPixels/2; 
        float cy = metrics.heightPixels/2; 

        float x = (float) (cx + radius * Math.cos((float) Math.toRadians(value))); // center x in arc 
        float y = (float) (cy + radius * Math.sin((float) Math.toRadians(value))); // center y in arc 

        x -= (button.getWidth()/2); 
        y -= (button.getHeight()/2); 

        button.setX(x); 
        button.setY(y); 
       } 
      }); 

      animator.start(); 
} 
関連する問題