Đâ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);
}
}
Nguồn
2012-10-14 15:13:14
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
Cảm ơn bạn, nó hoạt động! – validcat
Điều này đáng lẽ phải là câu trả lời được chấp nhận! – Machado