这是实现了在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. Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  2. java POI读取excel 2007/2003

    2003版office excel读取 import java.io.FileNotFoundException; import java.io.IOException; import java.io ...

  3. 【转】nand flash坏块管理OOB,BBT,ECC

    0.NAND的操作管理方式      NAND FLASH的管理方式:以三星FLASH为例,一片Nand flash为一个设备(device),1 (Device) = xxxx (Blocks),1 ...

  4. eclipse默认编码设置为utf-8

    需要设置的几处地方为: Window->Preferences->General ->Content Type->Text->JSP 最下面设置为UTF-8 Window ...

  5. android-读取Assets图片资源保存到SD - 随心

    public class ReadBitmap { public void readByte(Context c, String name, int indexInt) { byte[] b = nu ...

  6. [Android4.4.3] Nubia Z5S Mokee4.4.3 RC2.0 by syhost

    这个ROM先前在Mokee官网公布过,但一些人測试bug不少,因此已经撤下, 但又有人反馈跟之前RC1.0版的bug差点儿相同, 所以再次在网盘单独公布, 截图以及注意事项见之前的RC1.0的帖子, ...

  7. CentOS6.5下使用NetHogs监控进程网络使用情况

    Nethogs 是一个终端下的网络流量监控工具,它的特别之处在于能够显示每一个进程的带宽占用情况,这样能够更直观获取网络使用情况.它支持 IPv4 和 IPv6 协议.支持本地网卡及 PPP 链接. ...

  8. Eclipse SVN插件的帐号、password改动

    问题描写叙述: Eclipse的SVN插件Subclipse做得非常好,在svn操作方面提供了非常强大丰富的功能.但到眼下为止,该插件对svn用户的概念极为淡薄,不但不能方便地切换用户,并且一旦用户的 ...

  9. Web页面布局方式小结

    Web页面是由块元素组成的,正常情况下块元素一个个按垂直方向排布,构成了页面.可是这样的主要的布局方式绝大多时候不能满足我们的需求,所以各种布局方式应运而生,本文就对这些布局方式做个小结. 1.元素漂 ...

  10. GTID复制报错处理:Last_Error: Error 'Can't drop database 'test'; database doesn't exist' on query

    创建GTID主从连接: mysql, master_user; 报错显示: Slave_IO_Running: Yes Slave_SQL_Running: No Last_Error: Error ...