Home | History | Annotate | Download | only in com.example.android.elevationdrag
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.elevationdrag;
     18 
     19 import com.example.android.common.logger.Log;
     20 
     21 import android.graphics.Outline;
     22 import android.os.Bundle;
     23 import android.support.v4.app.Fragment;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.view.ViewOutlineProvider;
     28 
     29 import java.util.Locale;
     30 
     31 public class ElevationDragFragment extends Fragment {
     32 
     33     public static final String TAG = "ElevationDragFragment";
     34 
     35     /* The circular outline provider */
     36     private ViewOutlineProvider mOutlineProviderCircle;
     37 
     38     /* The current elevation of the floating view. */
     39     private float mElevation = 0;
     40 
     41     /* The step in elevation when changing the Z value */
     42     private int mElevationStep;
     43 
     44     @Override
     45     public void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47 
     48         mOutlineProviderCircle = new CircleOutlineProvider();
     49 
     50         mElevationStep = getResources().getDimensionPixelSize(R.dimen.elevation_step);
     51     }
     52 
     53     @Override
     54     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     55             Bundle savedInstanceState) {
     56         View rootView = inflater.inflate(R.layout.ztranslation, container, false);
     57 
     58         /* Find the {@link View} to apply z-translation to. */
     59         final View floatingShape = rootView.findViewById(R.id.circle);
     60 
     61         /* Define the shape of the {@link View}'s shadow by setting one of the {@link Outline}s. */
     62         floatingShape.setOutlineProvider(mOutlineProviderCircle);
     63 
     64         /* Clip the {@link View} with its outline. */
     65         floatingShape.setClipToOutline(true);
     66 
     67         DragFrameLayout dragLayout = ((DragFrameLayout) rootView.findViewById(R.id.main_layout));
     68 
     69         dragLayout.setDragFrameController(new DragFrameLayout.DragFrameLayoutController() {
     70 
     71             @Override
     72             public void onDragDrop(boolean captured) {
     73                 /* Animate the translation of the {@link View}. Note that the translation
     74                  is being modified, not the elevation. */
     75                 floatingShape.animate()
     76                         .translationZ(captured ? 50 : 0)
     77                         .setDuration(100);
     78                 Log.d(TAG, captured ? "Drag" : "Drop");
     79             }
     80         });
     81 
     82         dragLayout.addDragView(floatingShape);
     83 
     84         /* Raise the circle in z when the "z+" button is clicked. */
     85         rootView.findViewById(R.id.raise_bt).setOnClickListener(new View.OnClickListener() {
     86             @Override
     87             public void onClick(View v) {
     88                 mElevation += mElevationStep;
     89                 Log.d(TAG, String.format(Locale.US, "Elevation: %.1f", mElevation));
     90                 floatingShape.setElevation(mElevation);
     91             }
     92         });
     93 
     94         /* Lower the circle in z when the "z-" button is clicked. */
     95         rootView.findViewById(R.id.lower_bt).setOnClickListener(new View.OnClickListener() {
     96             @Override
     97             public void onClick(View v) {
     98                 mElevation -= mElevationStep;
     99                 // Don't allow for negative values of Z.
    100                 if (mElevation < 0) {
    101                     mElevation = 0;
    102                 }
    103                 Log.d(TAG, String.format(Locale.US, "Elevation: %.1f", mElevation));
    104                 floatingShape.setElevation(mElevation);
    105             }
    106         });
    107 
    108         return rootView;
    109     }
    110 
    111     /**
    112      * ViewOutlineProvider which sets the outline to be an oval which fits the view bounds.
    113      */
    114     private class CircleOutlineProvider extends ViewOutlineProvider {
    115         @Override
    116         public void getOutline(View view, Outline outline) {
    117             outline.setOval(0, 0, view.getWidth(), view.getHeight());
    118         }
    119     }
    120 
    121 }