这是实现了在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. hdu 4635 Strongly connected (tarjan)

    题意:给一个n个顶点m条弧的简单有向图(无环无重边),求最多能够加入多少条弧使得加入后的有向图仍为简单有向图且不是一个强连通图.假设给的简单有向图本来就是强连通图,那么输出-1. 分析: 1.用tar ...

  2. C#主要字典集合性能对比[转]

    A post I made a couple days ago about the side-effect of concurrency (the concurrent collections in ...

  3. linux下php扩展curl的安装

    方法一 安装cURL wget http://curl.haxx.se/download/curl-7.17.1.tar.gz tar -zxf curl-7.17.1.tar.gz ./config ...

  4. 关于使用Html5 canvas、 map、jquery构造不规则变色点击区域 热点区域

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. XML及PullParser解析

    一.什么是XML 1.概念:extensive markup language(可扩展的标记语言) XML是一种通用的数据存储和交换格式,与平台无关,与编程语言无关,与操作系统无关.给数据集成和交互提 ...

  6. ASP.NET和PHP全面对比

    谁是速度之王? 刚刚在9月编程语言排行榜上取得历史性突破的PHP在Web开发领域最到的对手可能就是基于微软.NET技术的ASP.NET.近日,微软的 Joe Stagner在博客上发表了一系列文章比较 ...

  7. python版去UTF-8 BOM

    今天给app弄银联支付接口.直接copy银联的sdk.结果.安卓和ios始终报json格式错误.找了半天.都没找到问题.最后怀疑可能是BOM破坏了json的数据格式转换.验证后确认是BOM的问题.为方 ...

  8. sublime远程连接到linux主机

    sublime远程连接到linux主机 sublime远程连接到linux主机 微信开发,直接使用sublime的sftp功能修改wx_sample.php 1.为sublime安装安装包管理插件Pa ...

  9. tab标签切换(无炫效果,简单的显示隐藏)

    从最简单的效果开始写起,一个简单的JQ写出tab切换效果,很静态,没有任何的轮转特效,单纯的点击标签显示区域块. 附上代码: HTML: <div class="wrapper&quo ...

  10. 生成简历经验总结(解析HTML字符串)

    在生成简历的过程中,我的做法是首先设计一个word的简历模板,设置好书签,从数据库中读取数据,调用aspose进行填充.一般的数据项包括图片文件都没有问题. 问题出在了HTML字符串上.因为简历中有几 ...