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