首页 文章 精选 留言 我的

精选列表

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

Android开发实践:自定义带消息循环(Looper)的工作线程

1. 首先,我们完成一个简单的线程框架。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public class LooperThread{ private volatile boolean mIsLooperQuit= false ; private ThreadmThread; public void start(){ if (mThread!= null ){ return ; } mIsLooperQuit= false ; mThread= new Thread(mLooperRunnable); mThread.start(); } public void stop(){ if (mThread== null ){ return ; } mIsLooperQuit= true ; mThread= null ; } protected RunnablemLooperRunnable= new Runnable(){ @Override public void run(){ while (!mIsLooperQuit){ } } }; } 如上述代码所示,mLooperRunnable.run()循环执行线程任务,mIsLooperQuit则是线程退出循环的条件。下面,我们将添加消息的发送和处理代码。 2. 添加线程循环的消息发送和处理代码 (1) 定义消息结构体,创建消息队列 1 2 3 4 5 6 7 8 public class LooperThread{ private Queue<Message>mMessageQueue= new LinkedList<Message>(); public static class Message{ int what; } } (2) 创建互斥锁和条件变量 1 2 3 4 5 public class LooperThread{ private LockmLock= new ReentrantLock(); private ConditionmCondition=mLock.newCondition(); } (3) 创建发送消息的函数 1 2 3 4 5 6 7 8 9 10 //发送消息,由外部其他模块调用,发送消息给线程 public void sendMessage(Messagemessage){ if (mThread== null ){ return ; } mLock.lock(); mMessageQueue.add(message); //添加消息到消息队列 mCondition.signal(); //通知线程循环,有消息来了,请立即处理 mLock.unlock(); } (4) 创建处理消息的函数 1 2 3 4 //处理消息,由线程内部调用 public void handleMessage(Messagemessage){ //这里可以通过一个Callback来回调监听者 } (5) 在mLooperRunnable.run()循环中解析消息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 protected RunnablemLooperRunnable= new Runnable(){ @Override public void run(){ while (!mIsLooperQuit){ mLock.lock(); Messagemessage= null ; try { while (!mIsLooperQuit&&mMessageQueue.isEmpty()){ mCondition.await(); //没有消息到来则休眠 } message=mMessageQueue.poll(); } catch (InterruptedExceptione){ e.printStackTrace(); } finally { mLock.unlock(); } handleMessage(message); } }; } (6) 修改线程的Stop()函数,唤醒休眠的消息循环 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public void stop(){ if (mThread== null ){ return ; } mIsLooperQuit= true ; mLock.lock(); mCondition.signal(); mLock.unlock(); mMessageQueue.clear(); mThread= null ; } 到这里,一个基本的带有消息循环的线程类封装就完成了,相信大家应该从编写这段代码的过程中,理解了系统是如何实现消息循环的。 本文转自 Jhuster 51CTO博客,原文链接:http://blog.51cto.com/ticktick/1565272,如需转载请自行联系原作者

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

Android笔记:根据进度代码改变progressbar背景颜色,ClipDrawable,LayerDrawable,setProgre...

做个笔记,主要是需要使用ClipDrawable、LayerDrawable: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 private void setProgressBg( int progress){ int a=progress* 255 / 100 ; int color=Color.argb(a, 255 , 90 , 147 ); ColorDrawablecolorDrawable= new ColorDrawable(); colorDrawable.setColor(color); ClipDrawableclipDrawable= new ClipDrawable(colorDrawable,Gravity.LEFT,ClipDrawable.HORIZONTAL); Drawabledrawable=getResources().getDrawable(R.color.transparent); Drawable[]layers= new Drawable[]{drawable,clipDrawable}; LayerDrawablelayerDrawable= new LayerDrawable(layers); layerDrawable.setDrawableByLayerId( 0 ,drawable); layerDrawable.setDrawableByLayerId( 1 ,clipDrawable); progressBar.setProgressDrawable(clipDrawable); progressBar.setProgress( 0 ); progressBar.setProgress(progress); Loger.d( "Vote投票" , "result=" +progress); Loger.d( "Vote投票" , "a=" +a); } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1739160,如需转载请自行联系原作者

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

Android GIS开发系列-- 入门季(6)GraphicsLayer添加文字与图片标签

一、GraphicsLayer添加图片 GraphicLayer添加图片Graphic,要用到PictureMarkerSymbol,也是样式的一种。添加代码如下: Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher); PictureMarkerSymbol pictureMarkerSymbol = new PictureMarkerSymbol(this, drawable); Graphic graphic = new Graphic(new Point(113,22), pictureMarkerSymbol); graphicsLayer.addGraphic(graphic); 添加图片效果如下: 二、GraphicsLayer添加文字 GraphicLayer添加文字,要利用TextSymbol。代码如下 TextSymbol textSymbol = new TextSymbol(20,"我是文字" , Color.RED); Graphic graphic = new Graphic(new Point(113,22), textSymbol ); graphicsLayer.addGraphic(graphic); 上面的代码没有设置使用哪种字体,Arcgis会默认的字体。如果 我们想添加中文,要设置textSymbol.setFontFamily("DroidSansFallback.ttf");, 为textSymbol设置字体,arcgis会到system\fonts下找到此字体。但有时我们设置此代码时中文并没有出现,可能会有乱码等,是因为系统的fon ts下面并没有此字体,解决的办法有,将手机root,将DroidSansFallback.ttf字体添加到system\fonts下。华为的手机是有此字体的,而三星5.0以 上则没有此字体,在华为手机上还有一款中文DroidSansChinese.ttf字体arcgis也是支持的。而三星5.0以上的中文字体好像不是ttf字体,而是otf字体 ,Arcgis好像不支持,本人也没有找到好的解决办法。 没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。 本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/7751888.html ,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

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

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

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

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码