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.elevationbasic; 18 19 import android.support.v4.app.Fragment; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.MenuItem; 23 import android.view.MotionEvent; 24 import android.view.View; 25 import android.view.ViewGroup; 26 27 import com.example.android.common.logger.Log; 28 29 public class ElevationBasicFragment extends Fragment { 30 31 private final static String TAG = "ElevationBasicFragment"; 32 33 @Override onCreate(Bundle savedInstanceState)34 public void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setHasOptionsMenu(true); 37 } 38 39 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)40 public View onCreateView( 41 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 42 /** 43 * Inflates an XML containing two shapes: the first has a fixed elevation 44 * and the second ones raises when tapped. 45 */ 46 View rootView = inflater.inflate(R.layout.elevation_basic, container, false); 47 48 View shape2 = rootView.findViewById(R.id.floating_shape_2); 49 50 /** 51 * Sets a {@Link View.OnTouchListener} that responds to a touch event on shape2. 52 */ 53 shape2.setOnTouchListener(new View.OnTouchListener() { 54 @Override 55 public boolean onTouch(View view, MotionEvent motionEvent) { 56 int action = motionEvent.getActionMasked(); 57 /* Raise view on ACTION_DOWN and lower it on ACTION_UP. */ 58 switch (action) { 59 case MotionEvent.ACTION_DOWN: 60 Log.d(TAG, "ACTION_DOWN on view."); 61 view.setTranslationZ(120); 62 break; 63 case MotionEvent.ACTION_UP: 64 Log.d(TAG, "ACTION_UP on view."); 65 view.setTranslationZ(0); 66 break; 67 default: 68 return false; 69 } 70 return true; 71 } 72 }); 73 return rootView; 74 } 75 }