2017-02-14 3 views
1

5つの画像すべてを中央の白い回転時計で、時計回りに反時計回りに中央の位置に配置する方法。私は試しましたが、このようになっています(下の画像) enter image description here 今のところ、中心点では回転していません。すべての画像を中心点でどのように回転させるか - アンドロイド

ここに私のコードです。

public class StartRotatingActivity extends AppCompatActivity implements  Animation.AnimationListener { 
private Context mContext; 
private ImageView timeBackground_1, timeBackground_2, timeBackground_3, timeBackground_4, timeBackground_5; 
Animation animationRight, animationLeft; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_start); 
    if (getSupportActionBar() != null) 
     getSupportActionBar().hide(); 

    mContext = this; 
    timeBackground_1 = (ImageView) findViewById(R.id.bgTimer1); 
    timeBackground_2 = (ImageView) findViewById(R.id.bgTimer2); 
    timeBackground_3 = (ImageView) findViewById(R.id.bgTimer3); 
    timeBackground_4 = (ImageView) findViewById(R.id.bgTimer4); 
    timeBackground_5 = (ImageView) findViewById(R.id.bgTimer5); 

    animationRight = AnimationUtils.loadAnimation(this, R.anim.rotate_right); 
    animationRight.setDuration(12000); 
    animationLeft = AnimationUtils.loadAnimation(this, R.anim.rotate_left); 
    animationLeft.setDuration(12000); 

    timeBackground_1.startAnimation(animationRight); 
    timeBackground_2.startAnimation(animationLeft); 
    timeBackground_3.startAnimation(animationRight); 
    timeBackground_4.startAnimation(animationLeft); 
    timeBackground_5.startAnimation(animationRight); 

} 

@Override 
public void onAnimationStart(Animation animation) { 

} 

@Override 
public void onAnimationEnd(Animation animation) { 

} 

@Override 
public void onAnimationRepeat(Animation animation) { 

} 
} 

startActivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#000000" 
android:gravity="center" 
android:orientation="vertical"> 

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center"> 

    <ImageView 
     android:id="@+id/bgTimer1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     app:srcCompat="@mipmap/bg_time_1" /> 

    <ImageView 
     android:id="@+id/bgTimer2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     app:srcCompat="@mipmap/bg_time_2" /> 

    <ImageView 
     android:id="@+id/bgTimer3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     app:srcCompat="@mipmap/bg_time_3" /> 

    <ImageView 
     android:id="@+id/bgTimer4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     app:srcCompat="@mipmap/bg_time_4" /> 

    <ImageView 
     android:id="@+id/bgTimer5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     app:srcCompat="@mipmap/bg_time_5" /> 
</FrameLayout> 
</LinearLayout> 

Rorateleft.xml事前のための

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:fromDegrees="0" 
android:pivotX="50%" 
android:pivotY="50%" 
android:repeatCount="infinite" 
android:toDegrees="360" /> 

Rotateright.xml

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:fromDegrees="360" 
android:pivotX="50%" 
android:pivotY="50%" 
android:repeatCount="infinite" 
android:toDegrees="0" /> 

感謝。

+1

レイアウトXMLを投稿できますか? –

+0

@PratikPopatがレイアウトxmlを追加 – akk

答えて

1

使用目的アニメーション。アニメーションを以下のように設定します。

ObjectAnimator one = ObjectAnimator.ofFloat(timeBackground_1 , 
       "rotation", 0f, 360f); 
     one.setDuration(5000); 
     one.setRepeatCount(-1); 
     one.start(); 

     ObjectAnimator two = ObjectAnimator.ofFloat(timeBackground_2 , 
       "rotation", 360f, 0f); 
     two.setDuration(5000); 
     two.setRepeatCount(-1); 
     two.start(); 

     ObjectAnimator three = ObjectAnimator.ofFloat(timeBackground_3 , 
       "rotation", 0f, 360f); 
     three.setDuration(5000); 
     three.setRepeatCount(-1); 
     three.start(); 

     ObjectAnimator four = ObjectAnimator.ofFloat(timeBackground_4 , 
       "rotation", 360f, 0f); 
     four.setDuration(5000); 
     four.setRepeatCount(-1); 
     four.start(); 

     ObjectAnimator five = ObjectAnimator.ofFloat(timeBackground_5 , 
       "rotation", 0f, 360f); 
     five.setDuration(5000); 
     five.setRepeatCount(-1); 
     five.start(); 
+1

素晴らしいです。ありがとう – akk

0

問題が

android:pivotX="70%" 
android:pivotY="30%" 

はそれらを作るRotateleft.xml

であなたのピボットである:

android:pivotX="50%" 
android:pivotY="50%" 

あなたはまた、コードでそれを試みることができる:

Animation a = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
     a.setRepeatCount(Animation.INFINITE); 
     a.setDuration(12000); 
+0

私はそれを試しました。しかし、同じ問題 – akk

+0

あなたの問題はおそらく、あなたの画像が画像の真ん中に自分の中心を持っていないということです。 – l0v3

+0

私は静止画像を置くと、すべての中心点を示します。それを回転させながら。 – akk

関連する問題