首页 文章 精选 留言 我的

精选列表

搜索[android studio],共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中使用Canvas绘制指定位置和宽高度的图片

/*--------------------------------- * 绘制图片 * @param x屏幕上的x坐标 * @param y屏幕上的y坐标 * @param w要绘制的图片的宽度 * @param h要绘制的图片的高度 * @param bx图片上的x坐标 * @param by图片上的y坐标 * * @return null ------------------------------------*/ public static void drawImage(Canvas canvas, Bitmap blt, int x, int y, int w, int h, int bx, int by) { Rect src = new Rect();// 图片 >>原矩形 Rect dst = new Rect();// 屏幕 >>目标矩形 src.left = bx; src.top = by; src.right = bx + w; src.bottom = by + h; dst.left = x; dst.top = y; dst.right = x + w; dst.bottom = y + h; // 画出指定的位图,位图将自动--》缩放/自动转换,以填补目标矩形 // 这个方法的意思就像 将一个位图按照需求重画一遍,画后的位图就是我们需要的了 canvas.drawBitmap(blt, null, dst, null); src = null; dst = null; } /** * 绘制一个Bitmap * * @param canvas 画布 * @param bitmap 图片 * @param x 屏幕上的x坐标 * @param y 屏幕上的y坐标 */ public static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y) { // 绘制图像 将bitmap对象显示在坐标 x,y上 canvas.drawBitmap(bitmap, x, y, null); } 本文转自java豆子博客园博客,原文链接:http://www.cnblogs.com/error404/archive/2012/02/05/2339255.html,如需转载请自行联系原作者

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

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,如需转载请自行联系原作者

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

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

Sublime Text

Sublime Text

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

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册