首页 文章 精选 留言 我的

精选列表

搜索[android],共10000篇文章
优秀的个人博客,低调大师

Android GIS开发系列-- 入门季(4) GraphicsLayer的点击查询要素

上一讲中我们学会了如何在MapView中添加Graphic要素,那么在百度或高德地图中,当我们点击要素时,会显示出相应的详细信息。在GraphicsLayer中也提供了这样的方法。下面我们来学习在GraphicsLayer中如何点击查找要素。首先在MapView中添加两个Graphic要素。代码如下,注意这里用Graphic(Geometrygeometry,Symbolsymbol, Map<String,Object> attributes)来实例化Graphic,Map<String,Object> attributes是要素的属性值。详细见以下代码。TILED_WORLD_STREETS_URL为网络图层地址。 privatevoidinitLayer(){ mapView.addLayer(newArcGISTiledMapServiceLayer( TILED_WORLD_STREETS_URL)); graphicsLayer=newGraphicsLayer(); mapView.addLayer(graphicsLayer); Polygonpolygon=newPolygon(); polygon.startPath(newPoint(1.2575908509778766E7,2879410.9266042486)); polygon.lineTo(newPoint(1.284360696117901E7,3021972.232083669)); polygon.lineTo(newPoint(1.2826182801620414E7,2713089.403544925)); Map<String,Object>attr1=newHashMap<>(); attr1.put("name","广州"); attr1.put("mark","广州是南方的城市"); Graphicgraphic1=newGraphic(polygon,newSimpleFillSymbol(Color.RED),attr1); graphicsLayer.addGraphic(graphic1); Polygonpolygon2=newPolygon(); polygon2.startPath(newPoint(1.3388507951011453E7,3611225.628065273)); polygon2.lineTo(newPoint(1.3607101952746565E7,3858331.890896268)); polygon2.lineTo(newPoint(1.3613438010767872E7,3449656.14852193)); Map<String,Object>attr2=newHashMap<>(); attr2.put("name","上海"); attr2.put("mark","上海是中部的城市"); Graphicgraphic2=newGraphic(polygon2,newSimpleFillSymbol(Color.GREEN),attr2); graphicsLayer.addGraphic(graphic2); } 效果图如下: 准备工作完成后,设置MapView的点击事件, mapView.setOnSingleTapListener(newOnSingleTapListener(){ @Override publicvoidonSingleTap(floatx,floaty){ //TODOAuto-generatedmethodstub handleSingleTap(x,y); } }); 在handleSingleTap方法中来处理查询事件,GraphicsLayer查询要用到getGraphicIDs(float x, float y, int tolerance, int numberOfResults)或者getGraphicIDs(float x, float y, int tolerance)方法,前面两个参数是地图点击时的 x与y的值,tolerance是围绕x与y这个点所查询的范围,numberOfResults是要返回结果的大小。 /** *GraphicsLayer的点击查询 *@paramx *@paramy */ protectedvoidhandleSingleTap(floatx,floaty){ int[]graphicIds=graphicsLayer.getGraphicIDs(x,y,8); if(graphicIds!=null&&graphicIds.length>0){ for(inti=0;i<graphicIds.length;i++){ Graphicgraphic=graphicsLayer.getGraphic(graphicIds[i]); Map<String,Object>attr=graphic.getAttributes(); Log.i(TAG,attr.get("name")+"===="+attr.get("mark")); } } }这样当我们点击要素时,会打出以下的信息。 没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。 本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/7751872.html ,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android应用程序键盘(Keyboard)消息处理机制分析(14)

3.InputManager分发键盘消息给应用程序的过程分析 在分析InputManager分发键盘消息给应用程序的过程之前,我们先假设现在没有键盘事件发生,因此,InputManager中的InputReader正在睡眠等待键盘事件的发生,而InputManager中的InputDispatcher正在等待InputReader从睡眠中醒过来并且唤醒它,而应用程序也正在消息循环中等待InputDispatcher从睡眠中醒过来并且唤醒它。这时候,用户按下键盘中的一个键,于是,一系列唤醒的事件就依次发生了,一直到应用程序中正在显示的Activity得到通知,有键盘事件发生了。我们先来看这个过程的序列图,然后再详细分析每一个步骤: Step 1. InputReader.pollOnce Step 2. EventHub.getEvent 这两个函数分别定义在frameworks/base/libs/ui/InputReader.cpp和frameworks/base/libs/ui/EventHub.cpp文件中,前面我们在分析InputManager的启动过程的Step 17和Step 18时,已经看到过这两个函数了。InputReaderThread线程会不民地循环调用InputReader.pollOnce函数来读入键盘事件,而实际的键盘事件读入操作是由EventHub.getEvent函数来进行的。如果当前没有键盘事件发生,InputReaderThread线程就会睡眠在EventHub.getEvent函数上,而当键盘事件发生后,就会把这个事件封装成一个RawEvent对象,然后返回到pollOnce函数中,执行process函数进一步处理: voidInputReader::loopOnce(){ RawEventrawEvent; mEventHub->getEvent(&rawEvent); ...... process(&rawEvent); } Step 3. InputReader.process 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidInputReader::process(constRawEvent*rawEvent){ switch(rawEvent->type){ caseEventHubInterface::DEVICE_ADDED: addDevice(rawEvent->deviceId); break; caseEventHubInterface::DEVICE_REMOVED: removeDevice(rawEvent->deviceId); break; caseEventHubInterface::FINISHED_DEVICE_SCAN: handleConfigurationChanged(rawEvent->when); break; default: consumeEvent(rawEvent); break; } } 当键盘事件发生时,rawEvent->type的值为EV_KEY,这是一个宏定义,具体可以参考bionic/libc/kernel/common/linux/input.h文件: #defineEV_KEY0x01 因此,接下来会调用consumeEvent函数进一步处理。 Step 4.InputReader.consumeEvent 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidInputReader::consumeEvent(constRawEvent*rawEvent){ int32_tdeviceId=rawEvent->deviceId; {//acquiredeviceregistryreaderlock RWLock::AutoRLock_rl(mDeviceRegistryLock); ssize_tdeviceIndex=mDevices.indexOfKey(deviceId); if(deviceIndex<0){ LOGW("DiscardingeventforunknowndeviceId%d.",deviceId); return; } InputDevice*device=mDevices.valueAt(deviceIndex); if(device->isIgnored()){ //LOGD("DiscardingeventforignoreddeviceId%d.",deviceId); return; } device->process(rawEvent); }//releasedeviceregistryreaderlock } 首先从rawEvent中取得触发键盘事件设备对象device,然后调用它的process函数进行处理。 Step 5. InputDevice.process 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidInputDevice::process(constRawEvent*rawEvent){ size_tnumMappers=mMappers.size(); for(size_ti=0;i<numMappers;i++){ InputMapper*mapper=mMappers[i]; mapper->process(rawEvent); } } 这里的mMapper成员变量保存了一系列输入设备事件处理象,例如负责处理键盘事件的KeyboardKeyMapper对象、负责处理轨迹球事件的TrackballInputMapper对象以及负责处理触摸屏事件的TouchInputMapper对象, 它们是在InputReader类的成员函数createDevice中创建的。这里查询每一个InputMapper对象是否要对当前发生的事件进行处理。由于发生的是键盘事件,真正会对该事件进行处理的只有KeyboardKeyMapper对象。 Step 6.KeyboardInputMapper.process 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidKeyboardInputMapper::process(constRawEvent*rawEvent){ switch(rawEvent->type){ caseEV_KEY:{ int32_tscanCode=rawEvent->scanCode; if(isKeyboardOrGamepadKey(scanCode)){ processKey(rawEvent->when,rawEvent->value!=0,rawEvent->keyCode,scanCode, rawEvent->flags); } break; } } } 这个函数首先会检查一下键盘扫描码是否正确,如果正确的话,就会调用processKey函数进一步处理。 本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966632,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android应用程序键盘(Keyboard)消息处理机制分析(15)

Step 7.KeyboardInputMapper.processKey 这个函数定义在frameworks/base/libs/ui/InputReader.cpp文件中: voidKeyboardInputMapper::processKey(nsecs_twhen,booldown,int32_tkeyCode, int32_tscanCode,uint32_tpolicyFlags){ int32_tnewMetaState; nsecs_tdownTime; boolmetaStateChanged=false; {//acquirelock AutoMutex_l(mLock); if(down){ //Rotatekeycodesaccordingtoorientationifneeded. //Note:getDisplayInfoisnon-reentrantsowecancontinueholdingthelock. if(mAssociatedDisplayId>=0){ int32_torientation; if(!getPolicy()->getDisplayInfo(mAssociatedDisplayId,NULL,NULL,&orientation)){ return; } keyCode=rotateKeyCode(keyCode,orientation); } //Addkeydown. ssize_tkeyDownIndex=findKeyDownLocked(scanCode); if(keyDownIndex>=0){ //keyrepeat,besuretousesamekeycodeasbeforeincaseofrotation keyCode=mLocked.keyDowns.itemAt(keyDownIndex).keyCode; }else{ //keydown if((policyFlags&POLICY_FLAG_VIRTUAL) &&mContext->shouldDropVirtualKey(when,getDevice(),keyCode,scanCode)){ return; } mLocked.keyDowns.push(); KeyDown&keyDown=mLocked.keyDowns.editTop(); keyDown.keyCode=keyCode; keyDown.scanCode=scanCode; } mLocked.downTime=when; }else{ //Removekeydown. ssize_tkeyDownIndex=findKeyDownLocked(scanCode); if(keyDownIndex>=0){ //keyup,besuretousesamekeycodeasbeforeincaseofrotation keyCode=mLocked.keyDowns.itemAt(keyDownIndex).keyCode; mLocked.keyDowns.removeAt(size_t(keyDownIndex)); }else{ //keywasnotactuallydown LOGI("Droppingkeyupfromdevice%sbecausethekeywasnotdown." "keyCode=%d,scanCode=%d", getDeviceName().string(),keyCode,scanCode); return; } } int32_toldMetaState=mLocked.metaState; newMetaState=updateMetaState(keyCode,down,oldMetaState); if(oldMetaState!=newMetaState){ mLocked.metaState=newMetaState; metaStateChanged=true; } downTime=mLocked.downTime; }//releaselock if(metaStateChanged){ getContext()->updateGlobalMetaState(); } getDispatcher()->notifyKey(when,getDeviceId(),AINPUT_SOURCE_KEYBOARD,policyFlags, down?AKEY_EVENT_ACTION_DOWN:AKEY_EVENT_ACTION_UP, AKEY_EVENT_FLAG_FROM_SYSTEM,keyCode,scanCode,newMetaState,downTime); } 这个函数首先对对按键作一些处理,例如,当某一个DPAD键被按下时,根据当时屏幕方向的不同,它所表示的意义也不同,因此,这里需要根据当时屏幕的方向来调整键盘码: //Rotatekeycodesaccordingtoorientationifneeded. //Note:getDisplayInfoisnon-reentrantsowecancontinueholdingthelock. if(mAssociatedDisplayId>=0){ int32_torientation; if(!getPolicy()->getDisplayInfo(mAssociatedDisplayId,NULL,NULL,&orientation)){ return; } keyCode=rotateKeyCode(keyCode,orientation); } 如果这个键是一直按着不放的,不管屏幕的方向如何,必须保证后面的键盘码和前面的一样: //Addkeydown. ssize_tkeyDownIndex=findKeyDownLocked(scanCode); if(keyDownIndex>=0){ //keyrepeat,besuretousesamekeycodeasbeforeincaseofrotation keyCode=mLocked.keyDowns.itemAt(keyDownIndex).keyCode; }else{ //keydown if((policyFlags&POLICY_FLAG_VIRTUAL) &&mContext->shouldDropVirtualKey(when,getDevice(),keyCode,scanCode)){ return; } mLocked.keyDowns.push(); KeyDown&keyDown=mLocked.keyDowns.editTop(); keyDown.keyCode=keyCode; keyDown.scanCode=scanCode; } 如果是第一次按下某个键,还必须把它保存在mLocked.keyDowns里面,就是为了处理上面讲的当这个键盘一直按着不放的时候屏幕方向发生改变的情况。 如果是松开键盘上的某个键,就把它从mLocked.keyDowns里面删除: //Removekeydown. ssize_tkeyDownIndex=findKeyDownLocked(scanCode); if(keyDownIndex>=0){ //keyup,besuretousesamekeycodeasbeforeincaseofrotation keyCode=mLocked.keyDowns.itemAt(keyDownIndex).keyCode; mLocked.keyDowns.removeAt(size_t(keyDownIndex)); }else{ //keywasnotactuallydown LOGI("Droppingkeyupfromdevice%sbecausethekeywasnotdown." "keyCode=%d,scanCode=%d", getDeviceName().string(),keyCode,scanCode); return; } 当然,对键盘事件的这些处理不是本文的重点,本文的重点是分析从键盘事件到当前激活的Activity窗口接收到这个键盘消息的过程。 最后,KeyboardInputMappger函数通知InputDispatcher,有键盘事件发生了: getDispatcher()->notifyKey(when,getDeviceId(),AINPUT_SOURCE_KEYBOARD,policyFlags, down?AKEY_EVENT_ACTION_DOWN:AKEY_EVENT_ACTION_UP, AKEY_EVENT_FLAG_FROM_SYSTEM,keyCode,scanCode,newMetaState,downTime); 本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966633,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android应用程序键盘(Keyboard)消息处理机制分析(4)

Step 18. EventHub.getEvent 这个函数定义在frameworks/base/libs/ui/EventHub.cpp文件中: boolEventHub::getEvent(RawEvent*outEvent) { outEvent->deviceId=0; outEvent->type=0; outEvent->scanCode=0; outEvent->keyCode=0; outEvent->flags=0; outEvent->value=0; outEvent->when=0; //NotethatweonlyallowonecallertogetEvent(),sodon'tneed //todolockinghere...onlywhenadding/removingdevices. if(!mOpened){ mError=openPlatformInput()?NO_ERROR:UNKNOWN_ERROR; mOpened=true; mNeedToSendFinishedDeviceScan=true; } for(;;){ //Reportanydevicesthathadlastbeenadded/removed. if(mClosingDevices!=NULL){ device_t*device=mClosingDevices; LOGV("Reportingdeviceclosed:id=0x%x,name=%s\n", device->id,device->path.string()); mClosingDevices=device->next; if(device->id==mFirstKeyboardId){ outEvent->deviceId=0; }else{ outEvent->deviceId=device->id; } outEvent->type=DEVICE_REMOVED; outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC); deletedevice; mNeedToSendFinishedDeviceScan=true; returntrue; } if(mOpeningDevices!=NULL){ device_t*device=mOpeningDevices; LOGV("Reportingdeviceopened:id=0x%x,name=%s\n", device->id,device->path.string()); mOpeningDevices=device->next; if(device->id==mFirstKeyboardId){ outEvent->deviceId=0; }else{ outEvent->deviceId=device->id; } outEvent->type=DEVICE_ADDED; outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC); mNeedToSendFinishedDeviceScan=true; returntrue; } if(mNeedToSendFinishedDeviceScan){ mNeedToSendFinishedDeviceScan=false; outEvent->type=FINISHED_DEVICE_SCAN; outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC); returntrue; } //Grabthenextinputevent. for(;;){ //Consumebufferedinputevents,ifany. if(mInputBufferIndex<mInputBufferCount){ conststructinput_event&iev=mInputBufferData[mInputBufferIndex++]; constdevice_t*device=mDevices[mInputDeviceIndex]; LOGV("%sgot:t0=%d,t1=%d,type=%d,code=%d,v=%d",device->path.string(), (int)iev.time.tv_sec,(int)iev.time.tv_usec,iev.type,iev.code,iev.value); if(device->id==mFirstKeyboardId){ outEvent->deviceId=0; }else{ outEvent->deviceId=device->id; } outEvent->type=iev.type; outEvent->scanCode=iev.code; if(iev.type==EV_KEY){ status_terr=device->layoutMap->map(iev.code, &outEvent->keyCode,&outEvent->flags); LOGV("iev.code=%dkeyCode=%dflags=0x%08xerr=%d\n", iev.code,outEvent->keyCode,outEvent->flags,err); if(err!=0){ outEvent->keyCode=AKEYCODE_UNKNOWN; outEvent->flags=0; } }else{ outEvent->keyCode=iev.code; } outEvent->value=iev.value; //Useaneventtimestampinthesametimebaseas //java.lang.System.nanoTime()andandroid.os.SystemClock.uptimeMillis() //asexpectedbytherestofthesystem. outEvent->when=systemTime(SYSTEM_TIME_MONOTONIC); returntrue; } //Finishreadingalleventsfromdevicesidentifiedinpreviouspoll(). //ThiscodeassumesthatmInputDeviceIndexisinitially0andthatthe //reventsmemberofpollfdisinitializedto0whenthedeviceisfirstadded. //SincemFDs[0]isusedforinotify,weprocessregulareventsstartingatindex1. mInputDeviceIndex+=1; if(mInputDeviceIndex>=mFDCount){ break; } conststructpollfd&pfd=mFDs[mInputDeviceIndex]; if(pfd.revents&POLLIN){ int32_treadSize=read(pfd.fd,mInputBufferData, sizeof(structinput_event)*INPUT_BUFFER_SIZE); if(readSize<0){ if(errno!=EAGAIN&&errno!=EINTR){ LOGW("couldnotgetevent(errno=%d)",errno); } }elseif((readSize%sizeof(structinput_event))!=0){ LOGE("couldnotgetevent(wrongsize:%d)",readSize); }else{ mInputBufferCount=readSize/sizeof(structinput_event); mInputBufferIndex=0; } } } ...... mInputDeviceIndex=0; //Pollforevents.Mindthewakelockdance! //Weholdawakelockatalltimesexceptduringpoll().Thisworksduetosome //subtlechoreography.Whenadevicedriverhaspending(unread)events,itacquires //akernelwakelock.However,oncethelastpendingeventhasbeenread,thedevice //driverwillreleasethekernelwakelock.Topreventthesystemfromgoingtosleep //whenthishappens,theEventHubholdsontoitsownuserwakelockwhiletheclient //isprocessingevents.Thusthesystemcanonlysleepiftherearenoevents //pendingorcurrentlybeingprocessed. release_wake_lock(WAKE_LOCK_ID); intpollResult=poll(mFDs,mFDCount,-1); acquire_wake_lock(PARTIAL_WAKE_LOCK,WAKE_LOCK_ID); if(pollResult<=0){ if(errno!=EINTR){ LOGW("pollfailed(errno=%d)\n",errno); usleep(100000); } } } } 这个函数比较长,我们一步一步来分析。 首先,如果是第一次进入到这个函数中时,成员变量mOpened的值为false,于是就会调用openPlatformInput函数来打开系统输入设备,在本文中,我们主要讨论的输入设备就是键盘了。打开了这些输入设备文件后,就可以对这些输入设备进行是监控了。如果不是第一次进入到这个函数,那么就会分析当前有没有输入事件发生,如果有,就返回这个事件,否则就会进入等待状态,等待下一次输入事件的发生。在我们这个场景中,就是等待下一次键盘事件的发生了。 我们先分析openPlatformInput函数的实现,然后回过头来分析这个getEvent函数的具体的实现。 本文转自 Luoshengyang 51CTO博客,原文链接:http://blog.51cto.com/shyluo/966613,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册