1. 程式人生 > >實現了簡單的旋轉動畫

實現了簡單的旋轉動畫

簡單的實現了以上動畫,程式碼如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Animation animation = null;
    private ImageView img;
    private Button rotate_center;
    private Button rotateX;
    private Button rotateY;
    private static int count=0;
    @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void setListener() { rotate_center.setOnClickListener(this); rotateX.setOnClickListener(this
); rotateY.setOnClickListener(this); } private void initView() { img = ((ImageView) findViewById(R.id.img)); rotate_center = ((Button) findViewById(R.id.rotate_center)); rotateX = ((Button) findViewById(R.id.rotateX)); rotateY = ((Button) findViewById(R.id.rotateY
)); } /* * 繞中心逆時針旋轉0-180 * */ private void rotate_center_n() { Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_n); LinearInterpolator lin = new LinearInterpolator(); operatingAnim.setInterpolator(lin); operatingAnim.setFillAfter(true); if (operatingAnim != null) { img.startAnimation(operatingAnim); } } /* * 繞中心順時針旋轉 恢復原位置 -180-0 * */ private void rotate_center_s() { Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_s); LinearInterpolator lin = new LinearInterpolator(); operatingAnim.setInterpolator(lin); operatingAnim.setFillAfter(true); if (operatingAnim != null) { img.startAnimation(operatingAnim); } } private void rotate (){ animation = new RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f); animation.setDuration(3000); img.startAnimation(animation); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.rotate_center: count++; if (count%2==1){ rotate_center_n(); }else { rotate_center_s(); } break; case R.id.rotateX: startActivity(new Intent(MainActivity.this,SecondActivity.class)); break; case R.id.rotateY: RotateAnimation r=new RotateAnimation(0f,90f,50,50); rotate (); break; } } }

xml佈局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal"
android:gravity="center"
>
    <ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_shops_down_arrow_pink"
/>
</LinearLayout>

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
        <Button
android:id="@+id/rotate_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="繞中心旋轉"
/>
        <Button
android:id="@+id/rotateX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="屬性動畫"
/> <Button
android:id="@+id/rotateY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="繞Y軸旋轉"
/>



    </LinearLayout>
</LinearLayout>

逆時針旋轉動畫:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
android:fromDegrees="0"
android:toDegrees="-180"
android:duration="1200"
android:fillAfter="true"
android:pivotX="50%"
android:pivotY="50%" />
</set>
順時針旋轉動畫:
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
android:fromDegrees="-180"
android:toDegrees="0"
android:duration="1200"
android:fillAfter="true"
android:pivotX="50%"
android:pivotY="50%" />
</set>