(android地图开发) 高德地图手势切换

小虾米 阅读:684 2021-04-01 10:19:58 评论:0

效果截图:

相关布局文件:

(手势滑动第一个界面)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"  
     android:background="#ffffff" 
    > 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="42dp" 
        android:background="#ffffff" 
        android:orientation="horizontal" > 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_margin="6dp" 
            android:background="@drawable/ml_card_bg" 
            android:src="@drawable/back_btn" > 
        </ImageView> 
 
        <LinearLayout 
            android:layout_width="210dp" 
            android:layout_height="wrap_content" 
            android:layout_marginBottom="6dp" 
            android:layout_marginTop="6dp" 
            android:background="@drawable/sapi_input" 
            android:orientation="horizontal" > 
 
            <ImageView 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_marginTop="5dp" 
                android:background="@drawable/change" /> 
 
            <TextView 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_gravity="center" 
                android:layout_margin="2dp" 
                android:hint="地点、地铁、公交..." 
                android:textColor="@color/grey" 
                android:textSize="16dp" > 
            </TextView> 
        </LinearLayout> 
 
        <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_marginBottom="6dp" 
            android:layout_marginTop="6dp" 
            android:background="@drawable/header_voice_normal" > 
        </Button> 
    </LinearLayout> 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="16dp" 
        android:background="#ffffff" 
        android:gravity="center" 
        android:orientation="horizontal" > 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_cur" > 
        </ImageView> 
 
        <View 
            android:layout_width="16dp" 
            android:layout_height="wrap_content" > 
        </View> 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_notcur" > 
        </ImageView> 
 
        <View 
            android:layout_width="16dp" 
            android:layout_height="wrap_content" > 
        </View> 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_notcur" > 
        </ImageView> 
    </LinearLayout> 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"        
        android:layout_marginTop="6dp" 
        android:orientation="vertical" > 
 
        <ListView 
            android:id="@+id/list_search_me" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" > 
        </ListView> 
    </LinearLayout> 
 
</LinearLayout>


(手势滑动第二个界面)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
     android:background="#ffffff" 
     > 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="42dp" 
        android:background="#ffffff" 
        android:orientation="horizontal" > 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_margin="6dp" 
            android:background="@drawable/ml_card_bg" 
            android:src="@drawable/back_btn" > 
        </ImageView> 
 
        <LinearLayout 
            android:layout_width="210dp" 
            android:layout_height="wrap_content" 
            android:layout_marginBottom="6dp" 
            android:layout_marginTop="6dp" 
            android:background="@drawable/sapi_input" 
            android:orientation="horizontal" > 
 
            <ImageView 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_marginTop="5dp" 
                android:background="@drawable/change" /> 
 
            <TextView 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_gravity="center" 
                android:layout_margin="2dp" 
                android:hint="地点、地铁、公交..." 
                android:textColor="@color/grey" 
                android:textSize="16dp" > 
            </TextView> 
        </LinearLayout> 
 
        <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_marginBottom="6dp" 
            android:layout_marginTop="6dp" 
            android:background="@drawable/header_voice_normal" > 
        </Button> 
    </LinearLayout> 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="16dp" 
        android:background="#ffffff" 
        android:gravity="center" 
        android:orientation="horizontal" > 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_notcur" > 
        </ImageView> 
 
        <View 
            android:layout_width="16dp" 
            android:layout_height="wrap_content" > 
        </View> 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_cur" > 
        </ImageView> 
 
        <View 
            android:layout_width="16dp" 
            android:layout_height="wrap_content" > 
        </View> 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_notcur" > 
        </ImageView> 
    </LinearLayout> 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginTop="6dp"        
        android:orientation="vertical"         
        > 
 
        <GridView 
            android:id="@+id/grid_search_launcher" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:horizontalSpacing="8dp" 
            android:numColumns="5" 
            android:verticalSpacing="6dp" > 
        </GridView> 
    </LinearLayout> 
 
</LinearLayout>


(手势滑动第三个界面)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="42dp" 
        android:background="#ffffff" 
        android:orientation="horizontal" > 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_margin="6dp" 
            android:background="@drawable/ml_card_bg" 
            android:src="@drawable/back_btn" > 
        </ImageView> 
 
        <LinearLayout 
            android:layout_width="210dp" 
            android:layout_height="wrap_content" 
            android:layout_marginBottom="6dp" 
            android:layout_marginTop="6dp" 
            android:background="@drawable/sapi_input" 
            android:orientation="horizontal" > 
 
            <ImageView 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_marginTop="5dp" 
                android:background="@drawable/change" /> 
 
            <TextView 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_gravity="center" 
                android:layout_margin="2dp" 
                android:hint="地点、地铁、公交..." 
                android:textColor="@color/grey" 
                android:textSize="16dp" > 
            </TextView> 
        </LinearLayout> 
 
 
       <Button 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_marginBottom="6dp" 
            android:layout_marginTop="6dp" 
            android:background="@drawable/header_voice_normal" > 
        </Button> 
    </LinearLayout> 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="16dp" 
        android:background="#ffffff" 
        android:gravity="center" 
        android:orientation="horizontal" > 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_notcur" > 
        </ImageView> 
 
        <View 
            android:layout_width="16dp" 
            android:layout_height="wrap_content" > 
        </View> 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_notcur" > 
        </ImageView> 
 
        <View 
            android:layout_width="16dp" 
            android:layout_height="wrap_content" > 
        </View> 
 
        <ImageView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon_scrollview_cur" > 
        </ImageView> 
    </LinearLayout> 
 
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_marginTop="6dp"       
        android:orientation="vertical" > 
 
        <GridView 
            android:id="@+id/grid_search_life" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:horizontalSpacing="8dp" 
            android:numColumns="5" 
            android:verticalSpacing="6dp" > 
        </GridView> 
    </LinearLayout> 
 
</LinearLayout>


源代码:

(滑动第一个页面)

package com.rf.mapabc; 
 
 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.GestureDetector.OnGestureListener; 
 
public class SearchActivity extends Activity implements OnGestureListener { 
	// 手势检测器 
	GestureDetector detector; 
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState); 
		// 全屏 
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN); 
 
		setContentView(R.layout.search); 
		detector = new GestureDetector(this); 
		//ListView 数据实例化方法在此编写 
	} 
	 
	//相关的覆写方法	 
	@Override 
	public boolean onDown(MotionEvent e) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	@Override 
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
			float velocityY) { 
		// TODO Auto-generated method stub 
		if (e1.getX() - e2.getX() > 50) { 
			// 相关页面的滑动 
			Intent intent = new Intent(SearchActivity.this, Search_middleActivity.class); 
			startActivity(intent); 
			 
			return true; 
		} 
		return true; 
		 
	} 
	@Override 
	public void onLongPress(MotionEvent e) { 
		// TODO Auto-generated method stub 
		 
	} 
	@Override 
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
			float distanceY) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	@Override 
	public void onShowPress(MotionEvent e) { 
		// TODO Auto-generated method stub 
		 
	} 
	@Override 
	public boolean onSingleTapUp(MotionEvent e) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	//添加事件处理函数		 
 
	@Override 
	public boolean onTouchEvent(MotionEvent event) { 
		// TODO Auto-generated method stub 
		return this.detector.onTouchEvent(event); 
	} 
	 
	 
 
}


(滑动第二个页面)

package com.rf.mapabc; 
 
import com.rf.adapter.ImageAdapter; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.GestureDetector.OnGestureListener; 
import android.widget.GridView; 
import android.widget.SlidingDrawer; 
 
public class Search_middleActivity extends Activity implements OnGestureListener { 
	// 手势检测器 
    GestureDetector detector; 
 
     
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState); 
		//全屏代码 
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		setContentView(R.layout.search_middle); 
		// 手势监听实例化代码 
				detector = new GestureDetector(this); 
			  //组件类 
				final GridView gridview=(GridView) findViewById(R.id.grid_search_launcher); 
				//相关资源组件 
				 int[] images=new int[]{ 
							R.drawable.pg1, 
							R.drawable.pg2, 
							R.drawable.pg3, 
							R.drawable.pg4, 
							R.drawable.pg5, 
							R.drawable.pg6, 
							R.drawable.pg7, 
							R.drawable.pg8, 
							R.drawable.pg9, 
							R.drawable.pg10 
					}; 
				 String[] contents=new String[]{ 
						 "团购","美食","酒店","公交站","银行","电影院","KTV","丽人","超市","景点" 
						  
				 }; 
				 //自定义适配器			 
					final ImageAdapter adapter=new ImageAdapter(this,images,contents); 
					gridview.setAdapter(adapter); 
				 
	} 
	//覆写相关方法 
	@Override 
	public boolean onDown(MotionEvent e) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	@Override 
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
			float velocityY) { 
		// TODO Auto-generated method stub 
		// TODO Auto-generated method stub 
				// e1 触摸的起始位置,e2 触摸的结束位置,velocityX X轴每一秒移动的像素速度(大概这个意思) velocityY 就是Y咯 
				// 手势左,上为正 ——,右,下为负正 
				if (e2.getX() - e1.getX() > 50) { 
					// 为什么是50? 这个根据你的模拟器大小来定,看看模拟器宽度,e2.getX()-e1.getX()<屏幕宽度就OK 
 
					// 要触发什么事件都在这里写就OK 
					// 如果要跳转到另外一个activity//SecondActivity 
					Intent intent = new Intent(Search_middleActivity.this, SearchActivity.class); 
					startActivity(intent);						 
					return true; 
				} 
				if (Math.abs(e2.getX() - e1.getX()) > 50) { 
					Intent intent = new Intent(Search_middleActivity.this, Search_endActivity.class); 
					startActivity(intent);					 
					return true; 
				} 
		return false; 
	} 
	@Override 
	public void onLongPress(MotionEvent e) { 
		// TODO Auto-generated method stub 
		 
	} 
	@Override 
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
			float distanceY) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	@Override 
	public void onShowPress(MotionEvent e) { 
		// TODO Auto-generated method stub 
		 
	} 
	@Override 
	public boolean onSingleTapUp(MotionEvent e) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	//事件方法 
	@Override 
	public boolean onTouchEvent(MotionEvent event) { 
		// TODO Auto-generated method stub 
		return this.detector.onTouchEvent(event); 
	} 
	 
 
}


(滑动第三个界面)

package com.rf.mapabc; 
 
 
 
 
 
import com.rf.adapter.ImageAdapter; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.GestureDetector.OnGestureListener; 
import android.widget.GridView; 
 
public class Search_endActivity extends Activity implements OnGestureListener{ 
	// 手势检测器 
    GestureDetector detector; 
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState); 
		//全屏代码 
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
				WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		setContentView(R.layout.serach_end); 
		// 手势监听实例化代码 
		detector = new GestureDetector(this); 
		//组件类 
		final GridView gridview=(GridView) findViewById(R.id.grid_search_life); 
		//相关资源组件 
		 int[] images=new int[]{ 
					R.drawable.pg1, 
					R.drawable.pg2, 
					R.drawable.pg3, 
					R.drawable.pg4, 
					R.drawable.pg5, 
					R.drawable.pg6, 
					R.drawable.pg7, 
					R.drawable.pg8, 
					R.drawable.pg9, 
					R.drawable.pg10 
			}; 
		 String[] contents=new String[]{ 
				 "团购","美食","酒店","公交站","银行","电影院","KTV","丽人","超市","景点" 
				  
		 }; 
		 //自定义适配器			 
			final ImageAdapter adapter=new ImageAdapter(this,images,contents); 
			gridview.setAdapter(adapter); 
	} 
	//覆写相关方法 
	@Override 
	public boolean onDown(MotionEvent e) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	@Override 
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
			float velocityY) { 
		// TODO Auto-generated method stub 
		if (e2.getX() - e1.getX() > 50) { 
			// 为什么是50? 这个根据你的模拟器大小来定,看看模拟器宽度,e2.getX()-e1.getX()<屏幕宽度就OK 
 
			// 要触发什么事件都在这里写就OK 
			// 如果要跳转到另外一个activity//SecondActivity 
			Intent intent = new Intent(Search_endActivity.this, Search_middleActivity.class); 
			startActivity(intent);			 
			return true; 
		} 
		return true; 
	} 
	@Override 
	public void onLongPress(MotionEvent e) { 
		// TODO Auto-generated method stub 
		 
	} 
	@Override 
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, 
			float distanceY) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	@Override 
	public void onShowPress(MotionEvent e) { 
		// TODO Auto-generated method stub 
		 
	} 
	@Override 
	public boolean onSingleTapUp(MotionEvent e) { 
		// TODO Auto-generated method stub 
		return false; 
	} 
	//事件监听 
	 
	@Override 
	public boolean onTouchEvent(MotionEvent event) { 
		// TODO Auto-generated method stub 
		return this.detector.onTouchEvent(event); 
	} 
	 
 
}


 

标签:Android
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号