登录
首页 » Java » Android中通过ViewPager动态加载数据完整示例源码下载

Android中通过ViewPager动态加载数据完整示例源码下载

于 2014-04-05 发布
0 272
下载积分: 1 下载次数: 0

代码说明:

Android中通过ViewPager动态加载数据完整示例源码下载

下载说明:请别用迅雷下载,失败请重下,重下不扣分!

发表评论

0 个回复

  • Android -BLE蓝牙小DEMO
    一、ble简单介绍 BLE: Bluetooth Low Energy,即蓝牙低功耗,它是一种技术,从蓝牙4.0开始支持。蓝牙低功耗芯片有两种模式:单模和双模。 单模:只能执行低功耗协议栈,也就是只支持ble。双模:支持传统蓝牙以及ble的使用。 较传统蓝牙:传输速度更快,覆盖范围更广,安全性更高,延迟更短,耗电低等优点。 关键术语和概念:Gatt:(Generic Attribute Profile)—通用属性配置文件,用于在ble链路上发送和接收被称为“属性”的数据块。目前所有的ble应用都是基于GATT的。一个设备可以实现多个配置文件。 ble交互的桥梁是Service、Characteristic、Desciptor。Characteristic:可以理解为一个数据类型,它包括一个value和0至多个对此characteristic的描述(Descriptor)。 Descriptor:对Characterisctic的描述,如范围、单位等。 Service:Characteristic的集合。它可以包含多个Characteristic。 一个ble终端可以包含多个Service,一个Service可以包含多个Characteristic,一个Characteristic包含一个value和多个Descriptor,一个Descriptor包含一个value。其中Characteristic比较重要,用的比较多。这三部分都由UUID作为唯一标示符,以此区分。UUID(Universally Unique Identifier),含义是通用唯一识别码,它是在一定范围内唯一的机器生成的标识符。标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12)。 ble中有四个角色:广播者(Braodcaster):广播发送者,是不可连接的设备。观察者(Observer):扫描广播,不能够启动连接。广播者和观察者不能建立连接。应用:温度传感器和温度显示器。外围(periphery):广播发送者,可连接的设备,在单一链路层作为从机。中央(central):扫描广播,启动连接,在单一或多链路层作为主机。 中央和外围可以进行配对、连接、数据通信。应用:手机和手表。一个中央可以同时连接多个周边,但是一个周边只能连接一个中央(但是我测试,周边可以连接多个中央设备,并且能正常通信)。二、Android 注意:Android 4.3(API 18)引入ble相关接口。相关类目录:frameworks/base/core/java/android/bluetooth/BluetoothGatt:中央使用和处理数据;BluetoothGattCallback:中央的回调。 BluetoothGattServer:周边提供数据; BluetoothGattServerCallback:周边的回调 BluetoothGattService:Gatt服务 BluetoothGattCharacteristic:Gatt特性 BluetoothGattDescriptor:Gatt描述2.1 中央设备 搜索ble设备 //搜索附近所有的外围设备 mBluetoothAdapter.startLeScan(mLeScanCallback); //搜索某些uuid的外围设备。 mBluetoothAdapter.startLeScan(uuid[] ,mLeScanCallback); 停止扫描 mBluetoothAdapter.stopLeScan(mLeScanCallback); 监听扫描结果。 mLeScanCallback = new BluetoothAdapter.LeScanCallback() { public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { } }; device 搜索到的ble设备。rssi 信号强度scanRecord 远程设备广告记录的内容(蓝牙名称) 发起连接请求,获得中央。 mBluetoothGatt = device.connectGatt(mContext, false,mGattCallback);第二个参数: 如果为false,则直接立即连接。如果为true,则等待远程设备可用时(在范围内,。。)连接。并不是断开后重新连接。 第三个参数:连接回调private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {};BluetoothGattCallback 类中提供了许多回调,包括:连接状态改变、characteristic的read、write、change,mtu change等。根据需要实现即可。 连接成功后,发送 gatt服务发现请求。mBluetoothGatt.discoverServices().发现服务成功会失败都会回调onServicesDiscovered()函数。通过mBluetoothGatt.getServices()获取连接的ble设备所提供的服务列表,返回值类型为List。 //连接状态改变回调 onConnectionStateChange(BluetoothGatt gatt, int status, int newState){ if(newState == BluetoothProfile.STATE_CONNECTED){ //连接成功后,发送发现服务请求。 mBluetoothGatt.discoverServices(); } } //发现服务回调。 public void onServicesDiscovered(BluetoothGatt gatt, int status) { if(status == BluetoothGatt.GATT_SUCCESS){ //发现成功后,则可以通过下面方法获取service 列表。 mBluetoothGatt.getServices(); } } 获得Characteristic和Descriptor。 通过服务列表中的BluetoothGattService,可以获取到服务所包含的characteristic(getCharacteristics()返回值类型为List)。 通过BluetoothGattCharacteristic可以获取特征所包含的descriptor(getDescriptors()返回值类型是List)。 BluetoothGattService、BluetoothGattCharacteristic和BluetoothGattDescriptor三个类中都提供了一个方法getUuid(),通过该方法可以获取其对应的uuid,从而可以判断是否是自己需要的service、characteristic或者descriptor。 通过获取的特征值,可以进行下操作: 写入特性值 读取特性值 订阅特性值。写入特征值: characteristic.setValue(data.getBytes()); mBluetoothGatt.writeCharacteristic(characteristic); 要想成功写入特征值: 首先此characteristic属性满足BluetoothGattCharacteristic.PROPERTY_WRITY或BluetoothGattCharacteristic.PROPERTY_WRITY_NO_RESPONSE,如果其property都不包含这两个,写特征值writeCharacteristic()函数直接返回false,什么都不做处理(具体可以看BluetoothGatt源码)。其次此characteristic权限应满足BluetoothGattCharacteristic.PERMISSION_WRITE,否则onCharacteristicWrite()回调收到GATT_WRITE_NOT_PERMITTED回应。写特征值前可以设置写的类型setWriteType(),写类型有三种,如下: WRITE_TYPE_DEFAULT 默认类型,需要外围设备的确认,也就是需要外围设备的回应,这样才能继续发送写。WRITE_TYPE_NO_RESPONSE 设置该类型不需要外围设备的回应,可以继续写数据。加快传输速率。WRITE_TYPE_SIGNED 写特征携带认证签名,具体作用不太清楚。 外围设备收到中央写特征值的请求,会回调 onCharacteristicWriteRequest如果此次请求需要回应,则外围设备回应 mGattServer.sendResponse中央设备收到响应,回调onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status)读取特征值: mBluetoothGatt.readCharacteristic(characteristic);读特征值与写类似,也需要响应的权限和属性。该characteristic属性需包含PROPERTY_READ,否则直接返回false(具体可以看BluetoothGatt源码)。该characteristic权限应满足BluetoothGattCharacteristic.PERMISSION_READ,否则onCharacteristicRead()回调收到GATT_READ_NOT_PERMITTED回应。 外围设备接收到中央设备的读特征值请求,则会回调 onCharacteristicReadRequest()函数,外围设备应该回应此请求 sendResponse。中央设备收到响应回调onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)订阅: //第二个参数:true则订阅该特征,false则取消订阅。mBluetoothGatt.setCharacteristicNotification(characteristic, true);当指定Characteristic值发生变化时,是否接收通知。当设为true,如果Characteristic发生变化时,会回调方法:onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)通过参数characteristic,可获得getValue获得其中的内容。注意:虽然订阅了该特征,并且该特征属性也满足PROPERTY_NOTIFY,但是并没有收到特征值改变的回调。这是为什么呢?查看sdk中的demo,发现需要写一下Descriptor。这样就可以正常监听特征值的改变了。 //CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb" BluetoothGattDescriptor descriptor = characteristic.getDescriptor( UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG)); descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor); 中央设备的其他一些方法readDescriptor(descriptor) 读取描述writeDescriptor(descriptor) 写描述readRemoteRssi() 读取连接设备的rssi。disconnect(); 断开bel连接。close(); 关闭中央设备。(不用时及时关闭,否则有的手机重连连不上。)2.2 外围设备获取/打开周边(外围) mGattServer = mBluetoothManager.openGattServer(mContext, callback); //其中callback是一个MyGattServerCallback(继承了BluetoothGattServerCallback)对象。 初始化描述、特性和服务。 //描述: new BluetoothGattDescriptor(UUID.fromString(DESC_UUID), descPermissions); //特性 : final int properties = BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE | BluetoothGattCharacteristic.PROPERTY_NOTIFY; final int permissions = BluetoothGattCharacteristic.PERMISSION_READ; | BluetoothGattCharacteristic.PERMISSION_WRITE; new BluetoothGattCharacteristic(UUID.fromString(CHAR_UUID), properties, permissions); gattChar.addDescriptor(gattDesc); property 表示属性。permission 表示权限。这两个都和权限相关。如果property未设置PROPERTY_READ,permission设置PERMISSION_READ,则中央设备readCharacteristic主动读取特征值方法返回false,此操作失败。而如果property设置PROPERTY_READ,permission未设置PERMISSION_READ,则中央设备readCharacteristic主动读取特征值方法返回true,此操作成功,外围设备发送响应,中央设备收到响应 GATT_READ_NOT_PERMITTED。所以说如果想要characteristic可读,则这两个都要设置。PROPERTY_WRITE和PERMISSION_WRITE也和上面类似。PROPERTY_NOTIFY 表示支持notification。 //服务: BluetoothGattService bs = new BluetoothGattService( UUID.fromString(SERV_UUID), BluetoothGattService.SERVICE_TYPE_PRIMARY); bs.addCharacteristic(gattChar); 第二个参数为service type,SERVICE_TYPE_PRIMARY 基础服务、主要服务。SERVICE_TYPE_SECONDARY 辅助服务(由初级服务包含在内)。BluetoothGattService 类中方法addService(bluetoothGattService),将辅助服务添加到主要服务中。getIncludeedServices() 获取包含的服务列表。getType() 获取服务的type。getUuid() 获取服务的UUID。添加服务mGattServer.addService(bs);设置广播数据开始广播这在android4.3没有提供,在android5.0才提供了设置广播数据,发送广告包等方法。我们开发是基于android4.3的,按理说我们是不可以作为外围设备的,不过我们framework以及底层都进行了修改,提供了这些方法,说以我们的android4.3设备可以作为外围。 mGattServer.startAdvertising();//开始广播mGattServer.stopAdvertising();//停止广播收到central扫描请求,回应扫描请求。这个不需要我们管理,此时会广播之前的设置的广播数据。收到central连接请求,建立连接。 连接成功后 外围可以断开连接。mGattServer.cancelConnection(device);响应central发起的gatt服务发现请求,回应服务信息。响应central发起的gatt特性发现请求,回应特性信息。响应central发起的gatt描述发现请求,回应描述信息。这三个不需要我们去操作,系统底层会处理。对central的读写做响应。回应特性值更新特性值。回应特征值:MyGattServerCallback extends BluetoothGattServerCallback.其中有几个常用的方法:onConnectionStateChange(BluetoothDevice device, int status, int newState)监听设备连接状态。  device远程设备  newStateble连接状态,只能为BluetoothProfile.STATE_CONNECTED和BluetoothProfile.STATE_DISCONNECTED。 onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset,BluetoothGattCharacteristic characteristic)监听中心设备读Characteristic的请求,  requestId 请求的标识。  offset 特性值偏移量。 Characteristic 要读的特性。此方法要求作出响应。mGattServer.sendResponse(device, requestId,BluetoothGatt.GATT_SUCCESS, offset, null); 最后一个参数可以设置传的数据,byte[]类型的。 onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,boolean preparedWrite, boolean responseNeeded, int offset, byte[] value)监听中心设备写Characteristic的请求, preparedWrite true则写操作必须排队等待稍后执行。 responseNeeded 是否需要响应。 value 写的数据。需要响应则必须sendResponse.更新特征值:外围设备向中心设备不能发送数据,必须通过notify 或者indicate的方式,andorid只发现notify接口。characteristic.setValue(res.getBytes());mGattServer.notifyCharacteristicChanged(device,characteristic, false);最后一个参数表示是否需要客户端确认。
    2019-04-14下载
    积分:1
  • android应用中的各种报表
    展示各种android应用中的报表图表
    2013-06-04下载
    积分:1
  • android SeeJoPlay播放器 实例源码下载
    android SeeJoPlay播放器 实例源码下载
    2014-04-02下载
    积分:1
  • BidirSlidingLayout_Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
    BidirSlidingLayout_Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效package com.example.bidirslidinglayout;import android.content.Context;import android.os.AsyncTask;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.VelocityTracker;import android.view.View;import android.view.ViewConfiguration;import android.view.View.OnTouchListener;import android.view.WindowManager;import android.widget.RelativeLayout;/** * 双向滑动菜单框架 * * @author guolin */public class BidirSlidingLayout extends RelativeLayout implements OnTouchListener { /** * 滚动显示和隐藏左侧布局时,手指滑动需要达到的速度。 */ public static final int SNAP_VELOCITY = 200; /** * 滑动状态的一种,表示未进行任何滑动。 */ public static final int DO_NOTHING = 0; /** * 滑动状态的一种,表示正在滑出左侧菜单。 */ public static final int SHOW_LEFT_MENU = 1; /** * 滑动状态的一种,表示正在滑出右侧菜单。 */ public static final int SHOW_RIGHT_MENU = 2; /** * 滑动状态的一种,表示正在隐藏左侧菜单。 */ public static final int HIDE_LEFT_MENU = 3; /** * 滑动状态的一种,表示正在隐藏右侧菜单。 */ public static final int HIDE_RIGHT_MENU = 4; /** * 记录当前的滑动状态 */ private int slideState; /** * 屏幕宽度值。 */ private int screenWidth; /** * 在被判定为滚动之前用户手指可以移动的最大值。 */ private int touchSlop; /** * 记录手指按下时的横坐标。 */ private float xDown; /** * 记录手指按下时的纵坐标。 */ private float yDown; /** * 记录手指移动时的横坐标。 */ private float xMove; /** * 记录手指移动时的纵坐标。 */ private float yMove; /** * 记录手机抬起时的横坐标。 */ private float xUp; /** * 左侧菜单当前是显示还是隐藏。只有完全显示或隐藏时才会更改此值,滑动过程中此值无效。 */ private boolean isLeftMenuVisible; /** * 右侧菜单当前是显示还是隐藏。只有完全显示或隐藏时才会更改此值,滑动过程中此值无效。 */ private boolean isRightMenuVisible; /** * 是否正在滑动。 */ private boolean isSliding; /** * 左侧菜单布局对象。 */ private View leftMenuLayout; /** * 右侧菜单布局对象。 */ private View rightMenuLayout; /** * 内容布局对象。 */ private View contentLayout; /** * 用于监听滑动事件的View。 */ private View mBindView; /** * 左侧菜单布局的参数。 */ private MarginLayoutParams leftMenuLayoutParams; /** * 右侧菜单布局的参数。 */ private MarginLayoutParams rightMenuLayoutParams; /** * 内容布局的参数。 */ private RelativeLayout.LayoutParams contentLayoutParams; /** * 用于计算手指滑动的速度。 */ private VelocityTracker mVelocityTracker; /** * 重写BidirSlidingLayout的构造函数,其中获取了屏幕的宽度和touchSlop的值。 * * @param context * @param attrs */ public BidirSlidingLayout(Context context, AttributeSet attrs) { super(context, attrs); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); screenWidth = wm.getDefaultDisplay().getWidth(); touchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } /** * 绑定监听滑动事件的View。 * * @param bindView * 需要绑定的View对象。 */ public void setScrollEvent(View bindView) { mBindView = bindView; mBindView.setOnTouchListener(this); } /** * 将界面滚动到左侧菜单界面,滚动速度设定为-30. */ public void scrollToLeftMenu() { new LeftMenuScrollTask().execute(-30); } /** * 将界面滚动到右侧菜单界面,滚动速度设定为-30. */ public void scrollToRightMenu() { new RightMenuScrollTask().execute(-30); } /** * 将界面从左侧菜单滚动到内容界面,滚动速度设定为30. */ public void scrollToContentFromLeftMenu() { new LeftMenuScrollTask().execute(30); } /** * 将界面从右侧菜单滚动到内容界面,滚动速度设定为30. */ public void scrollToContentFromRightMenu() { new RightMenuScrollTask().execute(30); } /** * 左侧菜单是否完全显示出来,滑动过程中此值无效。 * * @return 左侧菜单完全显示返回true,否则返回false。 */ public boolean isLeftLayoutVisible() { return isLeftMenuVisible; } /** * 右侧菜单是否完全显示出来,滑动过程中此值无效。 * * @return 右侧菜单完全显示返回true,否则返回false。 */ public boolean isRightLayoutVisible() { return isRightMenuVisible; } /** * 在onLayout中重新设定左侧菜单、右侧菜单、以及内容布局的参数。 */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (changed) { // 获取左侧菜单布局对象 leftMenuLayout = getChildAt(0); leftMenuLayoutParams = (MarginLayoutParams) leftMenuLayout.getLayoutParams(); // 获取右侧菜单布局对象 rightMenuLayout = getChildAt(1); rightMenuLayoutParams = (MarginLayoutParams) rightMenuLayout.getLayoutParams(); // 获取内容布局对象 contentLayout = getChildAt(2); contentLayoutParams = (RelativeLayout.LayoutParams) contentLayout.getLayoutParams(); contentLayoutParams.width = screenWidth; contentLayout.setLayoutParams(contentLayoutParams); } } @Override public boolean onTouch(View v, MotionEvent event) { createVelocityTracker(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 手指按下时,记录按下时的坐标 xDown = event.getRawX(); yDown = event.getRawY(); // 将滑动状态初始化为DO_NOTHING slideState = DO_NOTHING; break; case MotionEvent.ACTION_MOVE: xMove = event.getRawX(); yMove = event.getRawY(); // 手指移动时,对比按下时的坐标,计算出移动的距离。 int moveDistanceX = (int) (xMove - xDown); int moveDistanceY = (int) (yMove - yDown); // 检查当前的滑动状态 checkSlideState(moveDistanceX, moveDistanceY); // 根据当前滑动状态决定如何偏移内容布局 switch (slideState) { case SHOW_LEFT_MENU: contentLayoutParams.rightMargin = -moveDistanceX; checkLeftMenuBorder(); contentLayout.setLayoutParams(contentLayoutParams); break; case HIDE_LEFT_MENU: contentLayoutParams.rightMargin = -leftMenuLayoutParams.width - moveDistanceX; checkLeftMenuBorder(); contentLayout.setLayoutParams(contentLayoutParams); case SHOW_RIGHT_MENU: contentLayoutParams.leftMargin = moveDistanceX; checkRightMenuBorder(); contentLayout.setLayoutParams(contentLayoutParams); break; case HIDE_RIGHT_MENU: contentLayoutParams.leftMargin = -rightMenuLayoutParams.width moveDistanceX; checkRightMenuBorder(); contentLayout.setLayoutParams(contentLayoutParams); default: break; } break; case MotionEvent.ACTION_UP: xUp = event.getRawX(); int upDistanceX = (int) (xUp - xDown); if (isSliding) { // 手指抬起时,进行判断当前手势的意图 switch (slideState) { case SHOW_LEFT_MENU: if (shouldScrollToLeftMenu()) { scrollToLeftMenu(); } else { scrollToContentFromLeftMenu(); } break; case HIDE_LEFT_MENU: if (shouldScrollToContentFromLeftMenu()) { scrollToContentFromLeftMenu(); } else { scrollToLeftMenu(); } break; case SHOW_RIGHT_MENU: if (shouldScrollToRightMenu()) { scrollToRightMenu(); } else { scrollToContentFromRightMenu(); } break; case HIDE_RIGHT_MENU: if (shouldScrollToContentFromRightMenu()) { scrollToContentFromRightMenu(); } else { scrollToRightMenu(); } break; default: break; } } else if (upDistanceX < touchSlop && isLeftMenuVisible) { // 当左侧菜单显示时,如果用户点击一下内容部分,则直接滚动到内容界面 scrollToContentFromLeftMenu(); } else if (upDistanceX < touchSlop && isRightMenuVisible) { // 当右侧菜单显示时,如果用户点击一下内容部分,则直接滚动到内容界面 scrollToContentFromRightMenu(); } recycleVelocityTracker(); break; } if (v.isEnabled()) { if (isSliding) { // 正在滑动时让控件得不到焦点 unFocusBindView(); return true; } if (isLeftMenuVisible || isRightMenuVisible) { // 当左侧或右侧布局显示时,将绑定控件的事件屏蔽掉 return true; } return false; } return true; } /** * 根据手指移动的距离,判断当前用户的滑动意图,然后给slideState赋值成相应的滑动状态值。 * * @param moveDistanceX * 横向移动的距离 * @param moveDistanceY * 纵向移动的距离 */ private void checkSlideState(int moveDistanceX, int moveDistanceY) { if (isLeftMenuVisible) { if (!isSliding && Math.abs(moveDistanceX) >= touchSlop && moveDistanceX < 0) { isSliding = true; slideState = HIDE_LEFT_MENU; } } else if (isRightMenuVisible) { if (!isSliding && Math.abs(moveDistanceX) >= touchSlop && moveDistanceX > 0) { isSliding = true; slideState = HIDE_RIGHT_MENU; } } else { if (!isSliding && Math.abs(moveDistanceX) >= touchSlop && moveDistanceX > 0 && Math.abs(moveDistanceY) < touchSlop) { isSliding = true; slideState = SHOW_LEFT_MENU; contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); contentLayout.setLayoutParams(contentLayoutParams); // 如果用户想要滑动左侧菜单,将左侧菜单显示,右侧菜单隐藏 leftMenuLayout.setVisibility(View.VISIBLE); rightMenuLayout.setVisibility(View.GONE); } else if (!isSliding && Math.abs(moveDistanceX) >= touchSlop && moveDistanceX < 0 && Math.abs(moveDistanceY) < touchSlop) { isSliding = true; slideState = SHOW_RIGHT_MENU; contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); contentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); contentLayout.setLayoutParams(contentLayoutParams); // 如果用户想要滑动右侧菜单,将右侧菜单显示,左侧菜单隐藏 rightMenuLayout.setVisibility(View.VISIBLE); leftMenuLayout.setVisibility(View.GONE); } } } /** * 在滑动过程中检查左侧菜单的边界值,防止绑定布局滑出屏幕。 */ private void checkLeftMenuBorder() { if (contentLayoutParams.rightMargin > 0) { contentLayoutParams.rightMargin = 0; } else if (contentLayoutParams.rightMargin < -leftMenuLayoutParams.width) { contentLayoutParams.rightMargin = -leftMenuLayoutParams.width; } } /** * 在滑动过程中检查右侧菜单的边界值,防止绑定布局滑出屏幕。 */ private void checkRightMenuBorder() { if (contentLayoutParams.leftMargin > 0) { contentLayoutParams.leftMargin = 0; } else if (contentLayoutParams.leftMargin < -rightMenuLayoutParams.width) { contentLayoutParams.leftMargin = -rightMenuLayoutParams.width; } } /** * 判断是否应该滚动将左侧菜单展示出来。如果手指移动距离大于左侧菜单宽度的1/2,或者手指移动速度大于SNAP_VELOCITY, * 就认为应该滚动将左侧菜单展示出来。 * * @return 如果应该将左侧菜单展示出来返回true,否则返回false。 */ private boolean shouldScrollToLeftMenu() { return xUp - xDown > leftMenuLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判断是否应该滚动将右侧菜单展示出来。如果手指移动距离大于右侧菜单宽度的1/2,或者手指移动速度大于SNAP_VELOCITY, * 就认为应该滚动将右侧菜单展示出来。 * * @return 如果应该将右侧菜单展示出来返回true,否则返回false。 */ private boolean shouldScrollToRightMenu() { return xDown - xUp > rightMenuLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判断是否应该从左侧菜单滚动到内容布局,如果手指移动距离大于左侧菜单宽度的1/2,或者手指移动速度大于SNAP_VELOCITY, * 就认为应该从左侧菜单滚动到内容布局。 * * @return 如果应该从左侧菜单滚动到内容布局返回true,否则返回false。 */ private boolean shouldScrollToContentFromLeftMenu() { return xDown - xUp > leftMenuLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 判断是否应该从右侧菜单滚动到内容布局,如果手指移动距离大于右侧菜单宽度的1/2,或者手指移动速度大于SNAP_VELOCITY, * 就认为应该从右侧菜单滚动到内容布局。 * * @return 如果应该从右侧菜单滚动到内容布局返回true,否则返回false。 */ private boolean shouldScrollToContentFromRightMenu() { return xUp - xDown > rightMenuLayoutParams.width / 2 || getScrollVelocity() > SNAP_VELOCITY; } /** * 创建VelocityTracker对象,并将触摸事件加入到VelocityTracker当中。 * * @param event * 右侧布局监听控件的滑动事件 */ private void createVelocityTracker(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); } /** * 获取手指在绑定布局上的滑动速度。 * * @return 滑动速度,以每秒钟移动了多少像素值为单位。 */ private int getScrollVelocity() { mVelocityTracker.computeCurrentVelocity(1000); int velocity = (int) mVelocityTracker.getXVelocity(); return Math.abs(velocity); } /** * 回收VelocityTracker对象。 */ private void recycleVelocityTracker() { mVelocityTracker.recycle(); mVelocityTracker = null; } /** * 使用可以获得焦点的控件在滑动的时候失去焦点。 */ private void unFocusBindView() { if (mBindView != null) { mBindView.setPressed(false); mBindView.setFocusable(false); mBindView.setFocusableInTouchMode(false); } } class LeftMenuScrollTask extends AsyncTask { @Override protected Integer doInBackground(Integer... speed) { int rightMargin = contentLayoutParams.rightMargin; // 根据传入的速度来滚动界面,当滚动到达边界值时,跳出循环。 while (true) { rightMargin = rightMargin speed[0]; if (rightMargin < -leftMenuLayoutParams.width) { rightMargin = -leftMenuLayoutParams.width; break; } if (rightMargin > 0) { rightMargin = 0; break; } publishProgress(rightMargin); // 为了要有滚动效果产生,每次循环使线程睡眠一段时间,这样肉眼才能够看到滚动动画。 sleep(15); } if (speed[0] > 0) { isLeftMenuVisible = false; } else { isLeftMenuVisible = true; } isSliding = false; return rightMargin; } @Override protected void onProgressUpdate(Integer... rightMargin) { contentLayoutParams.rightMargin = rightMargin[0]; contentLayout.setLayoutParams(contentLayoutParams); unFocusBindView(); } @Override protected void onPostExecute(Integer rightMargin) { contentLayoutParams.rightMargin = rightMargin; contentLayout.setLayoutParams(contentLayoutParams); } } class RightMenuScrollTask extends AsyncTask { @Override protected Integer doInBackground(Integer... speed) { int leftMargin = contentLayoutParams.leftMargin; // 根据传入的速度来滚动界面,当滚动到达边界值时,跳出循环。 while (true) { leftMargin = leftMargin speed[0]; if (leftMargin < -rightMenuLayoutParams.width) { leftMargin = -rightMenuLayoutParams.width; break; } if (leftMargin > 0) { leftMargin = 0; break; } publishProgress(leftMargin); // 为了要有滚动效果产生,每次循环使线程睡眠一段时间,这样肉眼才能够看到滚动动画。 sleep(15); } if (speed[0] > 0) { isRightMenuVisible = false; } else { isRightMenuVisible = true; } isSliding = false; return leftMargin; } @Override protected void onProgressUpdate(Integer... leftMargin) { contentLayoutParams.leftMargin = leftMargin[0]; contentLayout.setLayoutParams(contentLayoutParams); unFocusBindView(); } @Override protected void onPostExecute(Integer leftMargin) { contentLayoutParams.leftMargin = leftMargin; contentLayout.setLayoutParams(contentLayoutParams); } } /** * 使当前线程睡眠指定的毫秒数。 * * @param millis * 指定当前线程睡眠多久,以毫秒为单位 */ private void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } }}  
    2014-04-10下载
    积分:1
  • J2E图书管理系统
    J2E图书管理系统
    2017-05-31下载
    积分:1
  • android照相机,录像机 例子源码
    android照相机,录像机 例子源码
    2015-03-09下载
    积分:1
  • android 百度地图api 示例源码
    android 百度地图api 示例源码
    2014-02-25下载
    积分:1
  • androidstudio测式-匯率變換
    使用androidstudio写程式的测试
    2015-05-22下载
    积分:1
  • android touch事件详解PPT下载
    TouchEventDemo是一个说明如何处理触摸事件的示例TouchEventDemo用户界面浅蓝色区域是可以接受触摸事件的区域,用户可以在Android模拟器中使用鼠标点击屏幕,用以模拟触摸手机屏幕下方黑色区域是显示区域,用来显示触摸事件的类型、相对坐标、绝对坐标、触点压力、触点尺寸和历史数据量等信息
    2014-09-17下载
    积分:1
  • android 模仿微博登陆框实例源码
    当文字输入时改变图片  仿微博
    2014-08-22下载
    积分:1
  • 696516资源总数
  • 106914会员总数
  • 0今日下载