endingTransition 调用时执行它. 对于进入动画我们指定值为0,也就是没有动画。你可以创建一个空的动画XML文件,并且引用它, 但是会有相当大概率的次数中,系统会弄糊涂,输出的动画执行起来会非常快.
gestureOverlayView = (GestureOverlayView)findViewById(R.id.gestures);
? ? //initialize the gesture library and set up the gesture listener
? ? gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
? ? gestureLibrary.load();
? ? gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener(){
? ? ? @Override
? ? ? public void onGesturePerformed(GestureOverlayView view, Gesture gesture) {
? ? ? ? ArrayList prediction = gestureLibrary.recognize(gesture);
? ? ? ? if(prediction.size() > 0){
? ? ? ? ? String action= prediction.get(0).name;
? ? ? ? ? //our gesture library contains "left swipe" and "right swipe" gestures
? ? ? ? ? if("left swipe".equals(action)){
? ? ? ? ? ? //slide out to the left
? ? ? ? ? ? SearchResultsActivity.this.finish();
? ? ? ? ? ? overridePendingTransition(0, R.anim.move_left);
? ? ? ? ? } else if("right swipe".equals(action)){
? ? ? ? ? ? //slide out to the right
? ? ? ? ? ? SearchResultsActivity.this.finish();
? ? ? ? ? ? overridePendingTransition(0, R.anim.move_right);
? ? ? ? ? }
? ? ? ? }
?
? ? ? }});
? ? //gesture is transparent (no longer a yellow line)
? ? gestureOverlayView.setGestureVisible(false);
代码示例 7: 在onCreate方法中初始化 GestureOverlayView
下面是动画文件 move_left.xml: (除了toXDelta是正数之外?move_right.xml?跟 move_left.xml是一样的)
? ? android:duration="500"
? ? android:fromXDelta="0"
? ? android:toXDelta="-100%"
? ? android:interpolator="@android:anim/decelerate_interpolator"
/>
代码示例 8: 向左移动动画的代码
?注意当用在一个GestureOverlayView里面的时候,你的网格视图不能有一个0dp的layout_height值, 因为将真的只是0dp,而不是像我们想要的那样在一个线性布局中扩展开. 为了在我们的案例中适应这种情况,我们将layout_height设置成fill_parent. 我们有不想要我们的手势可见,并且我也不想有等待可见的手势渐变消失这种延时, 因此你会需要将 fadeOffset?和?fadeDuration 设置成 0.
? ? android:id="@+id/gestures"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent"
? ? android:fadeOffset="0"
? ? android:fadeDuration="0"
? ? android:eventsInterceptionEnabled="true">? ? ?
? ? ? ? android:id="@+id/search_results"
? ? ? android:layout_width="fill_parent"
? ? ? android:layout_height="fill_parent"
? ? ? android:paddingTop="10dp"
? ? ? android:numColumns="4"
? ? ? android:verticalSpacing="10dp"
? ? ? android:horizontalSpacing="10dp"
? ? ? android:layout_weight="1"
? ? ? android:stretchMode="columnWidth"
? ? ? android:gravity="center"/>
代码示例 9: 得到用于布局 xml 的 GestureOverlayView 的更新的 GridView 块
现在你已经了解到本地搜索可以被添加到Android应用程序中,此外还了解到一些关键的UI是怎么做出选择决策的. 我也指出了一些其中会出现的挑战,以及如何去避免他们. 你现在应该能够把搜索整合到你自己的应用程序去了,同时对用户体验进行更多的考虑.
https://developer.android.com/training/search/index.html
Whitney Foster 是Intel的软件解决方案组的软件工程师,工作于在Android应用程序上大规模应用的项目上.
*其它名称和品牌可能已经被声称是属于其它人的财产.
**这个实例的代码以Intel示例源代码许可证发布.
英文原文:Adding Local Search Functionality to Your Android* Application