实现了在android实现左右滑动切换界面的效果

时间:2022-12-23 17:58:44

这是实现了在android实现左右滑动切换界面的效果,该效果的源码下载,请到源码天堂下载吧,喜欢的朋友可以研究一下。

布局文件

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layContain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
	android:background="@drawable/bg"
    >
   <!-- android:background="#FFC0CB"-->
   
   <FrameLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"   
    
   >
   <LinearLayout 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="horizontal"    
   >
    <LinearLayout
        android:id="@+id/layFirst"
        android:layout_width="400px"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:layout_marginBottom="50dp"
        >
        
       
    </LinearLayout>
    
    <LinearLayout
        android:id="@+id/laySec"
        android:layout_width="400px"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

       
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layThird"
        android:layout_width="400px"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

       
    </LinearLayout>   
    <LinearLayout
        android:id="@+id/layFourth"
        android:layout_width="400px"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        
    </LinearLayout>  
    </LinearLayout>  
    <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"   
    android:layout_gravity="center_horizontal|bottom"  
    android:layout_marginBottom="40dp"
    >
      <TextView 
      android:id="@+id/roll_dot1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="."
    android:textSize="50dp"
    android:textColor="#ffffff"
    />
    <TextView 
        android:id="@+id/roll_dot2"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="."
    android:textSize="50dp"
    android:textColor="#000000"
    />  
    <TextView 
        android:id="@+id/roll_dot3"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="."
    android:textSize="50dp"
    android:textColor="#000000"
    />  
    <TextView 
        android:id="@+id/roll_dot4"
     android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="."
    android:textSize="50dp"
    android:textColor="#000000"
    />  
    </LinearLayout>
    </FrameLayout>
</LinearLayout>

代码

 

 

package com.marsor.desk;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MarsorDeskActivity extends Activity {
	GestureDetector mGestureDetector;
	LinearLayout layFirst;
	LinearLayout laySec;
	LinearLayout layThird;
	LinearLayout layFourth;
	LinearLayout layContain;
	//当前显示的layout
	LinearLayout layCur;
	//左边的layout
	LinearLayout layLeft;
	//右边的layout
	LinearLayout layRight;
	int screenWidth;
	ArrayList<MyAppInfo> mApplications = new ArrayList<MyAppInfo>();
	
	TextView roll_dot1,roll_dot2,roll_dot3,roll_dot4;
	
	ArrayList<String> packagNameList ;
	private final int MENU_EXIT = 01;
	private MyReceiver receiver;
		
	private OnTouchListener myTouch = new OnTouchListener(){

    	
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			return mGestureDetector.onTouchEvent(event);
		}
    	
    };
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initpackagNameList();
        //监听系统新安装程序的广播
        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
        filter.addDataScheme("package");    //必须添加这项,否则拦截不到广播
        registerReceiver(receiver, filter);
        
        layContain = (LinearLayout) this.findViewById(R.id.layContain);
        layFirst = (LinearLayout) this.findViewById(R.id.layFirst);
        laySec = (LinearLayout) this.findViewById(R.id.laySec);
        layThird = (LinearLayout) this.findViewById(R.id.layThird);
        layFourth = (LinearLayout) this.findViewById(R.id.layFourth);
        
        roll_dot1 = (TextView) findViewById(R.id.roll_dot1);
        roll_dot2 = (TextView) findViewById(R.id.roll_dot2);
        roll_dot3 = (TextView) findViewById(R.id.roll_dot3);
        roll_dot4 = (TextView) findViewById(R.id.roll_dot4);
        
        layCur = layFirst;
        layLeft = null;
        layRight = laySec;
        
        layFirst.setOnTouchListener(myTouch);
        laySec.setOnTouchListener(myTouch);
        layThird.setOnTouchListener(myTouch);
        layFourth.setOnTouchListener(myTouch);
        
        //设置宽度
        screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        layFirst.getLayoutParams().width = screenWidth;
        laySec.getLayoutParams().width = screenWidth;
        layThird.getLayoutParams().width = screenWidth;
        layFourth.getLayoutParams().width = screenWidth;
        
         mGestureDetector = new GestureDetector(this, new OnGestureListener(){
			@Override
			public boolean onDown(MotionEvent e) {
				return true;
			}

			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
				int x = (int) (e2.getX() - e1.getX());
				
				//判断方向
				boolean dir = x>0;//如果大于0,为true,说明向右移动,直接将其前一个视图的marginleft设置成0,如果是向左移动,则直接将maringleft试着称宽度的负数
				if(dir){
					if(layLeft == null)return false;
					LinearLayout.LayoutParams llp = (LayoutParams) layLeft.getLayoutParams();
			
					TranslateAnimation anim1 = new TranslateAnimation(llp.leftMargin,0,0,0);
					anim1.setDuration(500l);
					layLeft.startAnimation(anim1);
					
					//layLeft.setAnimation(anim);
					llp.setMargins(0, 0, 0, 0);
					layLeft.setLayoutParams(llp);
					if(layLeft == layFirst){
						layLeft = null;
						layCur = layFirst;
						layRight =laySec ;
						//设置屏幕下方的小点随着页面的切换而改变
						roll_dot2.setTextColor(Color.BLACK);
						roll_dot1.setTextColor(Color.WHITE);
					}else if(layLeft == laySec){
						layLeft = layFirst;
						layCur = laySec;
						layRight =layThird ;
						roll_dot3.setTextColor(Color.BLACK);
						roll_dot2.setTextColor(Color.WHITE);
					}else if(layLeft == layThird){
						layLeft = laySec;
						layCur = layThird;
						layRight =layFourth ;
						roll_dot4.setTextColor(Color.BLACK);
						roll_dot3.setTextColor(Color.WHITE);
					}
				}else{
					if(layRight == null)return false;
					LinearLayout.LayoutParams llp = (LayoutParams) layCur.getLayoutParams();
					int width = layCur.getWidth();
					TranslateAnimation anim = new TranslateAnimation(width,0,0,0);
					
					anim.setDuration(500l);
					layRight.startAnimation(anim);
					
					llp.setMargins(-width, 0, 0, 0);
					layCur.setLayoutParams(llp);
					if(layCur == layFirst){
						layLeft = layFirst;
						layCur = laySec;
						layRight =layThird ;
						roll_dot1.setTextColor(Color.BLACK);
						roll_dot2.setTextColor(Color.WHITE);
					}else if(layCur == laySec){
						layLeft = laySec;
						layCur = layThird;
						layRight = layFourth ;
						roll_dot2.setTextColor(Color.BLACK);
						roll_dot3.setTextColor(Color.WHITE);
					}else if(layCur == layThird){
						layLeft = layThird;
						layCur = layFourth;
						layRight = null ;
						roll_dot3.setTextColor(Color.BLACK);
						roll_dot4.setTextColor(Color.WHITE);
					}
				}

				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;
			}
        	
        });
}