Home | History | Annotate | Download | only in 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 public class ElevationDragFragment extends Fragment {
     30 
     31     public static final String TAG = "ElevationDragFragment";
     32 
     33     /* The circular outline provider */
     34     private ViewOutlineProvider mOutlineProviderCircle;
     35 
     36     /* The current elevation of the floating view. */
     37     private float mElevation = 0;
     38 
     39     /* The step in elevation when changing the Z value */
     40     private int mElevationStep;
     41 
     42     @Override
     43     public void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45 
     46         mOutlineProviderCircle = new CircleOutlineProvider();
     47 
     48         mElevationStep = getResources().getDimensionPixelSize(R.dimen.elevation_step);
     49     }
     50 
     51     @Override
     52     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     53             Bundle savedInstanceState) {
     54         View rootView = inflater.inflate(R.layout.ztranslation, container, false);
     55 
     56         /* Find the {@link View} to apply z-translation to. */
     57         final View floatingShape = rootView.findViewById(R.id.circle);
     58 
     59         /* Define the shape of the {@link View}'s shadow by setting one of the {@link Outline}s. */
     60         floatingShape.setOutlineProvider(mOutlineProviderCircle);
     61 
     62         /* Clip the {@link View} with its outline. */
     63         floatingShape.setClipToOutline(true);
     64 
     65         DragFrameLayout dragLayout = ((DragFrameLayout) rootView.findViewById(R.id.main_layout));
     66 
     67         dragLayout.setDragFrameController(new DragFrameLayout.DragFrameLayoutController() {
     68 
     69             @Override
     70             public void onDragDrop(boolean captured) {
     71                 /* Animate the translation of the {@link View}. Note that the translation
     72                  is being modified, not the elevation. */
     73                 floatingShape.animate()
     74                         .translationZ(captured ? 50 : 0)
     75                         .setDuration(100);
     76                 Log.d(TAG, captured ? "Drag" : "Drop");
     77             }
     78         });
     79 
     80         dragLayout.addDragView(floatingShape);
     81 
     82         /* Raise the circle in z when the "z+" button is clicked. */
     83         rootView.findViewById(R.id.raise_bt).setOnClickListener(new View.OnClickListener() {
     84             @Override
     85             public void onClick(View v) {
     86                 mElevation += mElevationStep;
     87                 Log.d(TAG, String.format("Elevation: %.1f", mElevation));
     88                 floatingShape.setElevation(mElevation);
     89             }
     90         });
     91 
     92         /* Lower the circle in z when the "z-" button is clicked. */
     93         rootView.findViewById(R.id.lower_bt).setOnClickListener(new View.OnClickListener() {
     94             @Override
     95             public void onClick(View v) {
     96                 mElevation -= mElevationStep;
     97                 // Don't allow for negative values of Z.
     98                 if (mElevation < 0) {
     99                     mElevation = 0;
    100                 }
    101                 Log.d(TAG, String.format("Elevation: %.1f", mElevation));
    102                 floatingShape.setElevation(mElevation);
    103             }
    104         });
    105 
    106         return rootView;
    107     }
    108 
    109     /**
    110      * ViewOutlineProvider which sets the outline to be an oval which fits the view bounds.
    111      */
    112     private class CircleOutlineProvider extends ViewOutlineProvider {
    113         @Override
    114         public void getOutline(View view, Outline outline) {
    115             outline.setOval(0, 0, view.getWidth(), view.getHeight());
    116         }
    117     }
    118 
    119 }