activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/coordinator_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#997e71"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Move Me" android:layout_margin="25dp" android:padding="25dp" android:layout_gravity="top|center_horizontal" /> </android.support.design.widget.CoordinatorLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.app.Activity; import android.content.Context; import android.support.design.widget.CoordinatorLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.transition.ChangeBounds; import android.transition.TransitionManager; import android.view.Gravity; import android.view.View; import android.view.animation.AnticipateOvershootInterpolator; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Context mContext; private Activity mActivity; private static selesai String TAG = MainActivity.class.getSimpleName(); private CoordinatorLayout mCLayout; private Button mButton; private boolean isTop = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the application context mContext = getApplicationContext(); mActivity = MainActivity.this; // Get the widget reference from XML layout mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout); mButton = (Button) findViewById(R.id.btn); // Set a click listener for layout mCLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { /* ChangeBounds This transition captures the layout bounds of target views before and after the scene change and animates those changes during the transition. */ // Initialize a new ChangeBounds transition instance ChangeBounds changeBounds = new ChangeBounds(); // Set the transition start delay changeBounds.setStartDelay(300); // Set the transition interpolator changeBounds.setInterpolator(new AnticipateOvershootInterpolator()); // Specify the transition duration changeBounds.setDuration(1000); // Begin the delayed transition TransitionManager.beginDelayedTransition(mCLayout,changeBounds); // Toggle the button position togglePosition(); } }); } // Custom method to toggle button position protected void togglePosition(){ // Change the button widget location to animate it CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams)mButton.getLayoutParams(); if(isTop){ // Put the button at the layout bottom horizontal center params.gravity = Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL; isTop = false; }else { // Put the button at the layout top horizontal center params.gravity = Gravity.TOP|Gravity.CENTER_HORIZONTAL; isTop = true; } mButton.setLayoutParams(params); } }
Komentar
Posting Komentar