클릭시 반전 xml 파일, 애니메이션 효과 추가

클릭이벤트 발생하는 xml 파일 생성

res > animator 폴더 만든 후 drawable / press.xml 파일 생성 

메인레이아웃 파일에서 android:background="@drawable/press" 코드 추가함 

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/darker_gray" android:state_pressed="true"/>
    <item android:drawable="@android:color/white"/>
</selector>

 

android:background="@drawable/press" 추가 

...
       <LinearLayout
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/press"
            android:gravity="center"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_bookmark" />
        </LinearLayout>
...        

 

애니메이션 효과 추가 (프래그먼트간 이동)

res폴더에서 animator 폴더를 추가 후 

enter_anim.xml, exit_anim.xml 파일 추가 후

메인액티비티에서 .setCustomAnimations(R.animator.enter_anim2,R.animator.exit_anim2) 이런식으로 추가함 

//enter_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="1"
        android:propertyName="alpha"
        android:duration="500"/>
</set>

//exit_anim.xml 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:valueFrom="1"
        android:valueTo="0"
        android:propertyName="alpha"
        android:duration="500"/>
</set>

//enter_anim2.xml 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="1"
        android:propertyName="scaleX"
        android:duration="500" />
    <objectAnimator
        android:valueFrom="0"
        android:valueTo="1"
        android:propertyName="scaleY"
        android:duration="500" />
</set>

//exit_anim2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:valueFrom="1"
        android:valueTo="0"
        android:propertyName="scaleX"
        android:duration="500" />
    <objectAnimator
        android:valueFrom="1"
        android:valueTo="0"
        android:propertyName="scaleY"
        android:duration="500" />
</set>     

 

액티비티 혹은 프래그먼트에서 FragmentTransaction 에 해당 애니메이션 지정 

//프래그먼트 이동
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.animator.enter_anim2,R.animator.exit_anim2);

Fragment fragment1 = new MyFragment1();
fragment1.setArguments(bundle);
transaction.replace(R.id.main_frame, fragment1);
//transaction.addToBackStack(null);
transaction.commit();