2012-10-14 48 views
17

Tôi đang cố gắng thực hiện hoạt ảnh xoay. Tôi cần phải xoay một biểu tượng xung quanh chính nó giống như chúng thực hiện trên thanh tiến trình, nhưng những gì tôi nhận được là hình ảnh xoay quanh một vòng tròn. Đây là mã hoạt hình của tôiXoay hoạt ảnh android

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

đâu tôi đi sai ở đây? Cảm ơn

Trả lời

5

Vâng tôi nơi tôi đã sai. Tôi đã đệm vào hình ảnh của tôi khiến nó di chuyển sang một bên một chút mỗi khi nó xoay. Nhưng dù sao tôi đã xóa phần đệm và bây giờ nó hoạt động tốt.

59

Đây là mã nguồn cho các main.xml bố trí:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <Button 
     android:id="@+id/testButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="animationText" 
     android:onClick="AnimClick"/> 

    <ImageView 
     android:id="@+id/testImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:contentDescription="image_desc" 
     android:scaleType="fitCenter" 
     android:src="@drawable/cat2" /> 

</RelativeLayout> 

Để thực hiện các hình ảnh động luân chuyển, chúng ta có thể xác định các hình ảnh động bằng XML hoặc mã Java. Nếu chúng ta muốn viết animation trong xml, chúng ta cần tạo một file xml hoạt hình trong thư mục/res/anim. Ở đây, chúng ta tạo ra một tập tin xml tên rotate_around_center_point.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false" > 

    <rotate 
     android:duration="2500" 
     android:interpolator="@android:anim/linear_interpolator" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:repeatCount="infinite" 
     android:repeatMode="restart" 
     android:toDegrees="360" /> 

</set> 

và đây là Hoạt động của tôi:

public class MainActivity extends Activity implements OnClickListener { 

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

     Button btn = (Button) findViewById(R.id.testButton); 
     btn.setOnClickListener(this); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage); 

     Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point); 
     animationTarget.startAnimation(animation); 

    } 


} 
+0

Cảm ơn bạn. Tôi đã cố gắng để thêm scaletype vào hình ảnh và nó vẫn không giúp đỡ. – orelzion

+0

Cảm ơn bạn, nó hoạt động! – validcat

+1

Điều này đáng lẽ phải là câu trả lời được chấp nhận! – Machado

27

Bạn có thể thử với đoạn mã sau, chứ không phải làm việc đó trong XML:

RotateAnimation rotate = new RotateAnimation(0, 360, 
     Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 
     0.5f); 

rotate.setDuration(4000); 
rotate.setRepeatCount(Animation.INFINITE); 
yourView.setAnimation(rotate); 
+0

bạn biết làm thế nào để tránh tạm dừng (gian hàng) sau khi vòng lặp hoàn thành và trước khi nó lặp lại? – portfoliobuilder

+6

Thử thêm rotate.setInterpolator (new LinearInterpolator()); –

+0

Cảm ơn người đàn ông, nó đã hoạt động! – Recomer

0

Thêm xml sau trong thư mục hình ảnh động

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false" 
    android:duration="4000" 
    android:fromdegrees="0" 
    android:pivotx="50%" 
    android:pivoty="50%" 
    android:todegrees="360" 
    android:toyscale="0.0"> 
</set> 

Hy vọng điều này sẽ giúp bạn ..

để biết thêm thông tin http://www.blazin.in/2013/09/simple-rotate-animation-in-android.html