这是实现了在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;
} });
}

实现了在android实现左右滑动切换界面的效果的更多相关文章

  1. android tab之间滑动切换界面功能

    1. onTouchListener();                       //捕捉touch事件,比如说onDown 需要将可滑动的控件加上两个方法:(1)view.setOnTouch ...

  2. Fragment+ViewPager实现仿微信点击和滑动切换界面

    这是在我写的新闻App中实现的界面切换 贴出切换界面的主要代码: xml代码: <span style="font-size:14px;"> <android.s ...

  3. 使用Android studio作按键切换界面

    一.新建工程 二.新建一个按键             android:layout_width="wrap_content"         android:layout_hei ...

  4. 转:Android ViewPager多页面滑动切换以及动画效果

    一.首先,我们来看一下效果图,这是新浪微博的Tab滑动效果.我们可以手势滑动,也可以点击上面的头标进行切换.与此同方式, 白色横条会移动到相应的页卡头标下.这是一个动画效果,白条是缓慢滑动过去的.好了 ...

  5. Android ViewPager多页面滑动切换以及动画效果

    一.首先,我们来看一下效果图,这是新浪微博的Tab滑动效果.我们可以手势滑动,也可以点击上面的头标进行切换.与此同方式,白色横条会移动到相应的页卡头标下.这是一个动画效果,白条是缓慢滑动过去的.好了, ...

  6. Android——ViewPager多页面滑动切换以及动画效果

    一.首先,我们来看一下效果图,这是新浪微博的Tab滑动效果.我们可以手势滑动,也可以点击上面的头标进行切换.与此同方式,白色横条会移动到相应的页卡头标下.这是一个动画效果,白条是缓慢滑动过去的.好了, ...

  7. android 图片浏览器滑动切换图片

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  8. android: activity之间切换的抽屉效果

    之前一直用的是向左平移和向右平移的切换动画,看到别的APP那个抽屉效果,自己也弄了一个!感谢给我提供帮助的大神们! 将退出动画的参数设置为0时,进入动画则设置为向左平移,就实现了抽屉效果! 进入的动画 ...

  9. Android开发Activity全局切换的动画效果

    切换动画 slide_left_in.xml 从左边进 --> 退出的时候使用 <?xml version="1.0" encoding="utf-8&quo ...

随机推荐

  1. devStack

    1,devstack shell 脚本开源官网 http://devstack.org/ 脚本功能快速搭建 OpenStack 的运行和开发环境 [Note tips by Ruiy devstack ...

  2. cocos2dx CCControlButton button大事

    =================================.cpp文件 <pre name="code" class="cpp">bool ...

  3. 计算机与ARM板通过路由器相连

    首先,使用两根网线分别将计算机和ARM板与路由器的LAN口连接. 要想使计算机和ARM板通信,必须使二者在同一网段. 在计算机的终端输入:ifconfig 获取计算机的ip地址,查看eth0,我的ip ...

  4. 【贪心+中位数】【新生赛3 1007题】 Problem G (K)

    Problem G Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  5. Vlc基础数据结构记录

    1.  Vlc基础数据结构 hongxianzhao@hotmail.com 1.1  基础数据结构 struct vlc_object_t,相关文件为src\misc\objects.c. 定义为: ...

  6. C#钩子应用实例

    C#钩子应用实例一.写在最前 本文的内容只想以最通俗的语言说明钩子的使用方法,具体到钩子的详细介绍可以参照下面的网址: http://www.microsoft.com/china/community ...

  7. relativelayout常用属性

    ===================================================================================== 整理于http://naot ...

  8. gdb零散学习心得。

    引用: 记录学习心得.未来将统一整理. 一.基础知识 1:细节整理. 1,启动GDB的前置要求. 1)使用gcc/g++时,添加-g的指令.例如:g++ -g test.cpp -o test 2)如 ...

  9. [转]IP地址-子网掩码-默认网关

    IP地址:是给每个连接在Internet上的主机分配的一个32bit地址.地址有两部分组成,一部分为网络地址,另一部分为主机地址.IP地址分为A.B.C.D.E 5类.常用的是B和C两类.网络地址的位 ...

  10. PHP学习笔记,自己动手写个MVC的框架

    最新在大家自己的博客的过程中,发现各种开源的博客系统都或多或少的用起来别扭.于是想动手自己写个博客系统.既然写,就想好好写.那就先写个MVC框架.一点一点来.写的过程中有很多想法.还希望大家能够多多指 ...