1. 程式人生 > >動畫翻轉

動畫翻轉

佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/text"
        android:gravity="center"
        android:textSize="30dp"
        android:text="八維電商"/>

    <Button
        android:layout_marginLeft="200dp"
        android:text="開始程式設計"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

mainactivity

public class MainActivity extends AppCompatActivity {

    private Button button;
    private TextView text;

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

        //找控制元件
        text = findViewById(R.id.text);
        button = findViewById(R.id.button);

        //給text設定點選事件
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //動畫
                ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "scaleY", 1, 3);
                ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "alpha", 1, 2);
                ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, "rotationX", 360);
                AnimatorSet set = new AnimatorSet();

                //集合
                set.playTogether(animator1,animator2,animator3);
                set.setDuration(3000);
                set.start();
                set.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        //跳轉
                        Intent intent = new Intent(MainActivity.this, ShowActivity.class);
                        startActivity(intent);
                    }
                });
            }
        });
    }
}