首页 文章 精选 留言 我的

精选列表

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

12.源码阅读(IPC Binder机制-android api 26)

调用bindService方法绑定服务最终会执行Service的onBind方法并在ServiceConnection的onServiceConnected中得到IBinder对象,我们从源码角度看看这一过程是如何进行的 首先从ContextImpl的bindService看起 @Override 1538 public boolean bindService(Intent service, ServiceConnection conn, 1539 int flags) { 1540 warnIfCallingFromSystemProcess(); 1541 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), 1542 Process.myUserHandle()); 1543 } private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler 1582 handler, UserHandle user) { 1583 //注意这里得到的一个IServiceConnection在后边会用到 1584 IServiceConnection sd; 1585 ...... 1588 if (mPackageInfo != null) { 1589 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags); 1590 } ...... 1602 int res = ActivityManager.getService().bindService( 1603 mMainThread.getApplicationThread(), getActivityToken(), service, 1604 service.resolveTypeIfNeeded(getContentResolver()), 1605 sd, flags, getOpPackageName(), user.getIdentifier()); 1606 ...... 1614 } 1615 ActivityManager.getService()这行代码我们已经非常熟悉了,前边看activity启动的时候就遇到过 4199 public static IActivityManager getService() { 4200 return IActivityManagerSingleton.get(); 4201 } private static final Singleton<IActivityManager> IActivityManagerSingleton = 4204 new Singleton<IActivityManager>() { 4205 @Override 4206 protected IActivityManager create() { 4207 final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE); 4208 final IActivityManager am = IActivityManager.Stub.asInterface(b); 4209 return am; 4210 } 4211 }; ActivityManager.getService()得到的是IActivityManger,而IActivityManger是一个接口,所以我们要去看它的实现类ActivityManagerService中的bindService方法 ActivityManagerService中 public int bindService(IApplicationThread caller, IBinder token, Intent service, String resolvedType, IServiceConnection connection, int flags, String callingPackage, int userId) throws TransactionTooLargeException { ...... synchronized(this) { return mServices.bindServiceLocked(caller, token, service, resolvedType, connection, flags, callingPackage, userId); } } ActiveServices中 int bindServiceLocked(IApplicationThread caller, IBinder token, Intent service, String resolvedType, final IServiceConnection connection, int flags, String callingPackage, final int userId) throws TransactionTooLargeException { ...... try { //注意,这个方法和下边是殊途同归的 bringUpServiceLocked(serviceRecord, serviceIntent.getFlags(), callerFg, false, false); } catch (RemoteException e) { /* ignore - local call */ } ...... if (s.app != null && b.intent.received) { ...... if (b.intent.apps.size() == 1 && b.intent.doRebind) { requestServiceBindingLocked(s, b.intent, callerFg, true); } } else if (!b.intent.requested) { requestServiceBindingLocked(s, b.intent, callerFg, false); } ...... } private final boolean requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i, boolean execInFg, boolean rebind) throws TransactionTooLargeException { ...... bumpServiceExecutingLocked(r, execInFg, "bind"); r.app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE); r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind, r.app.repProcState); if (!rebind) { i.requested = true; } i.hasBound = true; i.doRebind = false; ...... } 需要找到 r.app.thread.scheduleBindService这个方法究竟是哪个类中的方法,r是ServiceRecord,那么到它里边去找发现app是ProcessRecord类,然后进入到ProcessRecord中去找thread,可以看到IApplicationThread thread,IApplicationThread很明显是一个接口,我们还要找到它的实现类才行,这里也不再卖关子了,我们要找的实现类就是ApplicationThread,而ApplicationThread是ActivityThread的一个内部类,所以去ActivityThread中继续寻找 public final void scheduleBindService(IBinder token, Intent intent, boolean rebind, int processState) { updateProcessState(processState, false); BindServiceData s = new BindServiceData(); s.token = token; s.intent = intent; s.rebind = rebind; if (DEBUG_SERVICE) Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid=" + Binder.getCallingUid() + " pid=" + Binder.getCallingPid()); sendMessage(H.BIND_SERVICE, s); } private void sendMessage(int what, Object obj) { sendMessage(what, obj, 0, 0, false); } 可以看到是通过handler发送了一个消息,找到这个消息的处理 case BIND_SERVICE: Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind"); handleBindService((BindServiceData)msg.obj); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); break; private void handleBindService(BindServiceData data) { //从集合中取出service,可以猜测服务在之前已经被存储起来了 Service s = mServices.get(data.token); ...... if (!data.rebind) { //调用service的onBind方法,这里终于找到了 IBinder binder = s.onBind(data.intent); //这里是回调到onServiceConnected方法的关键 ActivityManager.getService().publishService( data.token, data.intent, binder); } else { s.onRebind(data.intent); ActivityManager.getService().serviceDoneExecuting( data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0); } ...... } } 再次回到ActivityManagerService中 public void publishService(IBinder token, Intent intent, IBinder service) { // Refuse possible leaked file descriptors if (intent != null && intent.hasFileDescriptors() == true) { throw new IllegalArgumentException("File descriptors passed in Intent"); } synchronized(this) { if (!(token instanceof ServiceRecord)) { throw new IllegalArgumentException("Invalid service token"); } mServices.publishServiceLocked((ServiceRecord)token, intent, service); } } 再次回到ActiveServices类中 void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) { final long origId = Binder.clearCallingIdentity(); try { if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "PUBLISHING " + r + " " + intent + ": " + service); if (r != null) { Intent.FilterComparison filter = new Intent.FilterComparison(intent); IntentBindRecord b = r.bindings.get(filter); if (b != null && !b.received) { b.binder = service; b.requested = true; b.received = true; for (int conni=r.connections.size()-1; conni>=0; conni--) { ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni); for (int i=0; i<clist.size(); i++) { ConnectionRecord c = clist.get(i); if (!filter.equals(c.binding.intent.intent)) { if (DEBUG_SERVICE) Slog.v( TAG_SERVICE, "Not publishing to: " + c); if (DEBUG_SERVICE) Slog.v( TAG_SERVICE, "Bound intent: " + c.binding.intent.intent); if (DEBUG_SERVICE) Slog.v( TAG_SERVICE, "Published intent: " + intent); continue; } if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Publishing to: " + c); try { c.conn.connected(r.name, service, false); } catch (Exception e) { Slog.w(TAG, "Failure sending service " + r.name + " to connection " + c.conn.asBinder() + " (in " + c.binding.client.processName + ")", e); } } } } serviceDoneExecutingLocked(r, mDestroyingServices.contains(r), false); } } finally { Binder.restoreCallingIdentity(origId); } } 关键代码 c.conn.connected(r.name, service, false); c表示ConnectionRecord,conn就是IServiceConnection,看到这个IServiceConnection我们很容易把它和ServiceConnection联系起来,是不是这样呢,其实这时候我们可以回到最初的那个地方埋下的伏笔,我们写了一句注释 //注意这里得到的一个IServiceConnection在后边会用到 IServiceConnection sd; 从代码ArrayList<ConnectionRecord> clist = r.connections.valueAt(conni);中就可以看出,每一个ConnectionRecord都是提前存入集合中保存的,那么每个ConnectionRecord中的IServiceConnection也就在那时候已经存在了,IServiceConnection是一个接口,我们同样要找到它的实现类,那么回到ContextImpl中,看看,这个IServiceConnection是如何创建出来的 IServiceConnection sd; if (conn == null) { throw new IllegalArgumentException("connection is null"); } if (mPackageInfo != null) { sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags); @Override 1564 public IServiceConnection getServiceDispatcher(ServiceConnection conn, Handler handler, 1565 int flags) { 1566 return mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags); 1567 } 进入LoadedApk中 public final IServiceConnection getServiceDispatcher(ServiceConnection c, 1397 Context context, Handler handler, int flags) { 1398 synchronized (mServices) { 1399 LoadedApk.ServiceDispatcher sd = null; 1400 ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> map = mServices.get(context); 1401 if (map != null) { 1402 if (DEBUG) Slog.d(TAG, "Returning existing dispatcher " + sd + " for conn " + c); 1403 sd = map.get(c); 1404 } 1405 if (sd == null) { 1406 sd = new ServiceDispatcher(c, context, handler, flags); 1407 if (DEBUG) Slog.d(TAG, "Creating new dispatcher " + sd + " for conn " + c); 1408 if (map == null) { 1409 map = new ArrayMap<>(); 1410 mServices.put(context, map); 1411 } 1412 map.put(c, sd); 1413 } else { 1414 sd.validate(context, handler); 1415 } 1416 return sd.getIServiceConnection(); 1417 } 1418 } 1419 IServiceConnection getIServiceConnection() { 1553 return mIServiceConnection; 1554 } 看到这个 private final ServiceDispatcher.InnerConnection mIServiceConnection; 我们找到了IServiceConnection的实现类,InnerConnection,那么c.conn.connected(r.name, service, false);执行的就是它的connect方法 private static class InnerConnection extends IServiceConnection.Stub { 1489 final WeakReference<LoadedApk.ServiceDispatcher> mDispatcher; 1490 1491 InnerConnection(LoadedApk.ServiceDispatcher sd) { 1492 mDispatcher = new WeakReference<LoadedApk.ServiceDispatcher>(sd); 1493 } 1494 1495 public void connected(ComponentName name, IBinder service, boolean dead) 1496 throws RemoteException { 1497 LoadedApk.ServiceDispatcher sd = mDispatcher.get(); 1498 if (sd != null) { 1499 sd.connected(name, service, dead); 1500 } 1501 } 1502 } public void connected(ComponentName name, IBinder service, boolean dead) { 1569 if (mActivityThread != null) { 1570 mActivityThread.post(new RunConnection(name, service, 0, dead)); 1571 } else { 1572 doConnected(name, service, dead); 1573 } 1574 } public void connected(ComponentName name, IBinder service, boolean dead) { 1569 if (mActivityThread != null) { 1570 mActivityThread.post(new RunConnection(name, service, 0, dead)); 1571 } else { 1572 doConnected(name, service, dead); 1573 } 1574 } 终于找到了,看下边 public void doConnected(ComponentName name, IBinder service, boolean dead) { 1585 ServiceDispatcher.ConnectionInfo old; 1586 ServiceDispatcher.ConnectionInfo info; 1587 1588 ...... 1625 // If there was an old service, it is now disconnected. 1626 if (old != null) { 1627 mConnection.onServiceDisconnected(name); 1628 } 1629 ...... 1632 // If there is a new service, it is now connected. 1633 if (service != null) { //onServiceConnected被调用 1634 mConnection.onServiceConnected(name, service); 1635 } 1636 }

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

ArcGIS API for JavaScript 标注错位问题解决思路

项目需求:完成对点的符号化显示,并将点的某些信息显示在点符号的旁边。 设计 调研开始,展示数据前端一般用GraphicsLayer,再结合Graphic,PictureMarkerSymbol与TextSymbol就可以完成此工作。直接上代码 jidianjing_graphicsLayer: null, jidianjing_text_featureLayer: null, ... this.graphicsLayer = new GraphicsLayer({ id: "graphicsLayer", elevationInfo: { mode: "relative-to-ground", offset: 3 } }); ... view.map.add(this.graphicsLayer); ... let jidianjing_graphics = []; // 处理Graphics $.each(result, function (i, item) { item.widgetModule = 'jidianjingWidgetModule_click'; var stcd = item.STCD; var stnm = item.STNM; let tempPoint = new Point({ type: "point", x: item.LGTD, y: item.LTTD }); let normalsymbol = { type: "picture-marker", url: 'img/menu/081.png', width: "18px", height: "18px" }; if (!StringUtil.isNull(item.WARN_NAME)) { normalsymbol = { type: "picture-marker", url: 'img/menu/083.png', width: "28px", height: "28px" }; } else if (!StringUtil.isNull(item.LL)) { normalsymbol = { type: "picture-marker", url: 'img/menu/082.png', width: "28px", height: "28px" }; } let tempPop = new PopupTemplate({ title: '机电井:' + stnm, content: _self.setjidianjingContentInfo }); let tempGraphic = new Graphic({ geometry: tempPoint, attributes: item, symbol: normalsymbol, popupTemplate: tempPop }); let textGraphic = new Graphic({ geometry: tempPoint, symbol: new TextSymbol({ color: "#FFF", backgroundColor: new Color('red'), borderLineColor: new Color("#fff"), borderLineSize: 2, haloColor: "black", haloSize: "1px", text: '水位:12 水温:11', font: { size: 8, family: "sans-serif" } }) }); _self.jidianjing_graphicsLayer.add(tempGraphic); _self.jidianjing_text_graphicsLayer.add(textGraphic); ... 效果 说明gif图太大,aliyun拒绝了我的上传,所以只能用文字描述;在小比例尺下,符号标注和文字标注之间的距离较小,看着效果还可以,但是有会出现标注覆盖文字或者文字覆盖标注;随着比例尺的放大,符号和标注之间的距离会增大,如第二张图所示。但是拿给项目经理看的时候,不要这种效果,距离和标注与符号之间的距离不能变。so...改改改 新思路-构造FeatureLayer 查看了官网好多例子,发现只有FeatureLayer的标注和符号之间的距离是边的,因为我们拿到的数据是点数据,所以我们可以构造组成FeatureLayer的source,其实就对应ags里的Graphics;还有一个标注LabelClass,这个类就是用来显示标注的。so...又开干了。 jidianjing_featureLayer: null, ... let tempGraphics = []; // 处理Graphics $.each(result, function (i, item) { item.widgetModule = 'jidianjingWidgetModule_click'; var stcd = item.STCD; var stnm = item.STNM; let tempPoint = new Point({ type: "point", x: item.LGTD, y: item.LTTD }); let normalsymbol = { type: "picture-marker", url: 'img/menu/081.png', width: "18px", height: "18px" }; if (!StringUtil.isNull(item.WARN_NAME)) { normalsymbol = { type: "picture-marker", url: 'img/menu/083.png', width: "28px", height: "28px" }; } else if (!StringUtil.isNull(item.LL)) { normalsymbol = { type: "picture-marker", url: 'img/menu/082.png', width: "28px", height: "28px" }; } let tempPop = new PopupTemplate({ title: '机电井:' + stnm, content: _self.setjidianjingContentInfo }); let tempGraphic = new Graphic({ geometry: tempPoint, attributes: item, symbol: normalsymbol, popupTemplate: tempPop }); let textGraphic = new Graphic({ geometry: tempPoint, symbol: new TextSymbol({ color: "#FFF", backgroundColor: new Color('red'), borderLineColor: new Color("#fff"), borderLineSize: 2, haloColor: "black", haloSize: "1px", text: '水位:12 水温:11', font: { size: 8, family: "sans-serif" } }) }); tempGraphics.push(tempGraphic); ... // popup let tempPop = new PopupTemplate({ title: '机电井:' + '{STNM}', content: _self.setjidianjingContentInfo }); // 标注 var labelClass = new LabelClass({ minScale: 20000, symbol: { type: "label-3d", symbolLayers: [{ type: "text", material: { color: "white" }, size: 10, halo: { color: [0, 0, 0, 0.8], size: 0.5 } }], verticalOffset:{ minWorldLength: 20, } }, labelPlacement: "above-center", labelExpression: "水位:[STTP], 水温:[STCD]" // labelExpressionInfo: { // expression: "$feature.STNM" // } }); // 构建feetureLayer并实现标注 this.jidianjing_text_featureLayer = new FeatureLayer({ id: 'jidianjing_featureLayer', fields: [{ name: "STCD", alias: "STCD", type: "oid" }, { name: "LGTD", alias: "LGTD", type: "string" }, { name: "LTTD", alias: "LTTD", type: "string" }, { name: "STCD", alias: "STCD", type: "string" }, { name: "STNM", alias: "STNM", type: "string" }, { name: "STTP", alias: "STTP", type: "string" }, { name: "widgetModule", alias: "widgetModule", type: "string" }], objectIdField: "STCD", geometryType: "point", spatialReference: { wkid: 4326 }, source: tempGraphics, // an array of graphics with geometry and attributes // popupTemplate and symbol are not required in each graphic // since those are handled with the popupTemplate and // renderer properties of the layer popupTemplate: tempPop, // renderer: iconSymbolRenderer, maxScale: 0, minScale: 0, labelsVisible: true, labelingInfo: [labelClass] }); view.map.add(this.jidianjing_text_featureLayer); 效果 说明 可以在上面两张图中看到,标注和符号的位置一直都是固定的,并没有随着距离的变化而变化;这时候拿给项目经理,就这个效果还算满意。 总结 符号和标注之间的距离在GraphicsLayer和FeatureLayer中的表现是不一样的,所以要根据合适的场景进行选择取舍。另外,GraphicsLayer和FeatureLayer标注换行不能实现,对GraphicsLayer可以采用加多个TextSymbol的GraphicsLayer,设置GraphicsLayer相对地面的高度就可以实现,但是距离永远是个问题,在此给一些朋友一些提醒。

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

使用 ES decorators 构建一致性 API

![pic] 重用和一致性是程序设计中经久不衰的两个课题。在最新的 ES Proposal 中,「decorators 语法」为此带来了一定的便利,并且,很适合应用于大型的类库中。 装饰模式 提到 decorator 大家都不会陌生,即「装饰模式」—— 我们可以在「不侵入原有代码」的情况下,为代码增加一些「额外的功能」。 所谓「额外的功能」一般都比较独立,不和原有逻辑耦合,只是做一层包装。你也可以把它看成「包装模式」。形如: // 旧方法 function func() {} // 包装后的新方法 function funcWrapped() { // 有的没的 doSomethingBefore(); // 旧方法的过程本身并不变化 func(); // 这啊那的 doSomethingAfter

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

Android2.2 API 中文文档系列(2) —— EditText

正文 一、结构 java.lang.Object android.view.View android.widget.TextView android.widget.EditText 已知直接子类: AutoCompleteTextView, ExtractEditText 已知间接子类: MultiAutoCompleteTextView 二、xml属性 属性名称 描述 android:autoLink 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)。这里只有在同时设置text时才自动识别链接,后来输入的无法自动识别。 android:autoText 自动拼写帮助。这里单独设置是没有效果的,可能需要其他输入法辅助才行,效果参见视频。 android:bufferType 指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符, 也就是说getText后可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式,参见这里1、这里2。 android:capitalize 设置英文字母大写类型。设置如下值:sentences仅第一个字母大写;words每一个单词首字母大小,用空格区分单词;characters每一个英文字母都大写。在模拟器上用PC键盘直接输入可以出效果,但是用软键盘无效果。 android:cursorVisible 设定光标为显示/隐藏,默认显示。如果设置false,即使选中了也不显示光标栏。 android:digits 设置允许输入哪些字符。如“1234567890.+-*/%\n()” android:drawableTop 在text的正上方输出一个drawable。在EditView中的效果比较搞笑:,居然在文本框里,而且删不了。 android:drawableBottom 在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。 android:drawableLeft 在text的左边输出一个drawable(如图片)。 android:drawablePadding 设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。 android:drawableRight 在text的右边输出一个drawable,如图片。 android:editable 设置是否可编辑。仍然可以获取光标,但是无法输入。 android:editorExtras 指定特定输入法的扩展,如“com.mydomain.im.SOME_FIELD”。源码跟踪至EditorInfo.extras,暂无相关实现代码。 android:ellipsize 设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动) android:freezesText 设置保存文本的内容以及光标的位置。参见:这里。 android:gravity 设置文本位置,如设置成“center”,文本将居中显示。 android:hint Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。 android:imeOptions 设置软键盘的Enter键。有如下值可设置:normal,actionUnspecified,actionNone,actionGo ,actionSearch,actionSend,actionNext,actionDone ,flagNoExtractUi,flagNoAccessoryAction,flagNoEnterAction。可用’|’设置多个。这里仅设置显示图标之用,参见文章末尾例子。 android:imeActionId 设置IME动作ID,在onEditorAction中捕获判断进行逻辑操作。 android:imeActionLabel 设置IME动作标签。但是不能保证一定会使用,猜想在输入法扩展的时候应该有用。 android:includeFontPadding 设置文本是否包含顶部和底部额外空白,默认为true。 android:inputMethod 为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是这里报错找不到。关于自定义输入法参见这里。 android:inputType 设置文本的类型,用于帮助输入法显示合适的键盘类型。有如下值设置:none、text、textCapCharacters字母大小、textCapWords单词首字母大小、textCapSentences仅第一个字母大小、textAutoCorrect、textAutoComplete自动完成、textMultiLine多行输入、textImeMultiLine输入法多行(如果支持)、textNoSuggestions不提示、textEmailAddress电子邮件地址、textEmailSubject邮件主题、textShortMessage短信息(会多一个表情按钮出来,点开如下图: )、textLongMessage长讯息?、textPersonName人名、textPostalAddress地址、textPassword密码、textVisiblePassword可见密码、textWebEditText作为网页表单的文本、textFilte文本筛选过滤、textPhonetic拼音输入、numberSigned有符号数字格式、numberDecimal可带小数点的浮点格式、phone电话号码、datetime时间日期、date日期、time时间。部分参考这里。 android:marqueeRepeatLimit 在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。 android:ems 设置TextView的宽度为N个字符的宽度。参见TextView中此属性的截图。 android:maxEms 设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。 android:minEms 设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。 android:maxLength 限制输入字符数。如设置为5,那么仅可以输入5个汉字/数字/英文字母。 android:lines 设置文本的行数,设置两行就显示两行,即使第二行没有数据。 android:maxLines 设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。 android:minLines 设置文本的最小行数,与lines类似。 android:linksClickable 设置链接是否点击连接,即使设置了autoLink。 android:lineSpacingExtra 设置行间距。 android:lineSpacingMultiplier 设置行间距的倍数。如”1.2” android:numeric 如果被设置,该TextView有一个数字输入法。有如下值设置:integer正整数、signed带符号整数、decimal带小数点浮点数。 android:password 以小点”.”显示文本 android:phoneNumber 设置为电话号码的输入方式。 android:privateImeOptions 提供额外的输入法选项(字符串格式)。依据输入法而决定是否提供,如这里所见。自定义输入法继承 InputMethodService。这篇文章也许有帮助。 android:scrollHorizontally 设置文本超出TextView的宽度的情况下,是否出现横拉条。 android:selectAllOnFocus 如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。TextView中设置后无效果。 android:shadowColor 指定文本阴影的颜色,需要与shadowRadius一起使用。参见TextView中此属性的截图。 android:shadowDx 设置阴影横向坐标开始位置。 android:shadowDy 设置阴影纵向坐标开始位置。 android:shadowRadius 设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。 android:singleLine 设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行 android:text 设置显示文本. android:textAppearance 设置文字外观。如“?android:attr/textAppearanceLargeInverse ”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse android:textColor 设置文本颜色 android:textColorHighlight 被选中文字的底色,默认为蓝色 android:textColorHint 设置提示信息文字的颜色,默认为灰色。与hint一起使用。 android:textColorLink 文字链接的颜色. android:textScaleX 设置文字之间间隔,默认为1.0f。参见TextView的截图。 android:textSize 设置文字大小,推荐度量单位”sp”,如”15sp” android:textStyle 设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2]可以设置一个或多个,用“|”隔开 android:typeface 设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3] android:height 设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米) android:maxHeight 设置文本区域的最大高度 android:minHeight 设置文本区域的最小高度 android:width 设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_width的区别看这里。 android:maxWidth 设置文本区域的最大宽度 android:minWidth 设置文本区域的最小宽度 补充说明: a). 由于是继承自TextView,所以属性是一样的,但是这里重点补充了输入法相关的属性说明和研究,部分注释也做了相应的调整。 b). Word格式下载 http://download.csdn.net/source/2664164 三、例子 3.1 <!--[endif]-->android:imeOptions例子 < EditText android:id ="@+id/txtTest" android:imeOptions ="actionGo" android:layout_width ="100dp" android:layout_height ="wrap_content" ></ EditText > ((EditText)findViewById(R.id.txtTest)).setOnEditorActionListener( new TextView.OnEditorActionListener(){ @Override public boolean onEditorAction(TextViewv, int actionId, KeyEventevent){ if (actionId == EditorInfo.IME_ACTION_GO){ Toast.makeText(TestActivity. this , " 你点了Go! " ,Toast.LENGTH_SHORT).show(); } return false ; } }); 本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/582733,如需转载请自行联系原作者

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

Android2.2 API 中文文档系列(6) —— ImageView

正文 一、结构 java.lang.Object android.view.View android.widget.ImageView 已知直接子类: ImageButton, QuickContactBadge 已知间接子类: ZoomButton 二、类概述 显示任意图像,例如图标。ImageView类可以加载各种来源的图片(如资源或图片库),需要计算图像的尺寸,比便它可以在其他布局中使用,并提供例如缩放和着色(渲染)各种显示选项。 三、XML属性 属性名称 描述 android:adjustViewBounds 是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。 android:cropToPadding 是否截取指定区域用空白代替。单独设置无效果,需要与scrollY一起使用,效果如下,实现代码见代码部分: android:maxHeight 设置View的最大高度,单独使用无效,需要与setAdjustViewBounds一起使用。如果想设置图片固定大小,又想保持图片宽高比,需要如下设置: 1)设置setAdjustViewBounds为true; 2)设置maxWidth、MaxHeight; 3)设置设置layout_width和layout_height为wrap_content。 android:maxWidth 设置View的最大宽度。同上。 android:scaleType 设置图片的填充方式。 matrix 0 用矩阵来绘图 fitXY 1 拉伸图片(不按比例)以填充View的宽高 layout_ height :30px layout_ width :120px fitStart 2 按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边 fitCenter 3 按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间 fitEnd 4 按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边 center 5 按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示 layout_ height :60px layout_ width :80px padding :10px centerCrop 6 按比例放大原图直至等于某边View的宽高显示。 centerInside 7 当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。 android:src 设置View的drawable(如图片,也可以是颜色,但是需要指定View的大小) android:tint 将图片渲染成指定的颜色。见下图: 左边为原图,右边为设置后的效果,见后面代码。 四、代码 4.1 android:tint < ImageView android:background ="@android:color/white" android:src ="@drawable/btn_mode_switch_bg" android:layout_width ="wrap_content" android:layout_height ="wrap_content" ></ ImageView > < ImageView android:layout_marginLeft ="5dp" android:background ="@android:color/white" android:tint ="#ffff00" android:src ="@drawable/btn_mode_switch_bg" android:layout_width ="wrap_content" android:layout_height ="wrap_content" ></ ImageView > 4.2 android:cropToPadding < ImageView android:background ="@android:color/white" android:scrollY ="-10px" android:cropToPadding ="true" android:src ="@drawable/btn_mode_switch_bg" android:layout_width ="wrap_content" android:layout_height ="wrap_content" ></ ImageView > < ImageView android:background ="@android:color/white" android:scrollY ="10px" android:cropToPadding ="true" android:src ="@drawable/btn_mode_switch_bg" android:layout_width ="wrap_content" android:layout_height ="wrap_content" ></ ImageView > < ImageView android:paddingTop ="10px" android:background ="@android:color/white" android:scrollY ="10px" android:cropToPadding ="true" android:src ="@drawable/btn_mode_switch_bg" android:layout_width ="wrap_content" android:layout_height ="wrap_content" ></ ImageView > < ImageView android:paddingTop ="10px" android:background ="@android:color/white" android:scrollY ="10px" android:cropToPadding ="false" android:src ="@drawable/btn_mode_switch_bg" android:layout_width ="wrap_content" android:layout_height ="wrap_content" ></ ImageView > 本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/582712,如需转载请自行联系原作者

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

Android 3.1 r1 中文API文档 (121) —— ResourceCursorTreeAdapter

结构 继承关系 public abstract classtResourceCursorTreeAdapterextendsCursorTreeAdapter java.lang.Object android.widget.BaseExpandableListAdapter android.widget.CursorTreeAdapter android.widget.ResourceCursorTreeAdapter 直接子类 SimpleCursorTreeAdapter 类概述 一个简单的可扩展的ExpandableListAdapter,通过在XML文件来创建views。你可以指定一个定义了views外观的XML文件。 构造函数 publicResourceCursorTreeAdapter(Context context, Cursor cursor, int collapsedGroupLayout, int expandedGroupLayout, int childLayout, int lastChildLayout) 构造函数。 参数 context和正在运行的SimpleListItemFactory关联的ListView的上下文 cursor数据库游标 collapsedGroupLayout定义了收缩组的视图布局文件的资源标识 expandedGroupLayout定义了展开组的视图布局文件的资源标识 childLayout定义了除了最后一个的所有子视图的布局文件的资源标识 lastChildLayout定义了一组中最后一个子视图的布局文件的资源标识 publicResourceCursorTreeAdapter(Context context, Cursor cursor, int collapsedGroupLayout, int expandedGroupLayout, int childLayout) 构造函数。 参数 context和正在运行的SimpleListItemFactory关联的ListView的上下文 cursor数据库游标 collapsedGroupLayout定义了收缩组的视图布局文件的资源标识 expandedGroupLayout定义了展开组的视图布局文件的资源标识 childLayout定义了除了最后一个的所有子视图的布局文件的资源标识 publicResourceCursorTreeAdapter(Context context, Cursor cursor, int groupLayout, int childLayout) 构造函数。 参数 context和正在运行的SimpleListItemFactory关联的ListView的上下文 cursor数据库游标 groupLayout为所有组定义了视图布局文件的资源标识 expandedGroupLayout 定义了展开组的视图布局文件的资源标识 childLayout定义了除了最后一个的所有子视图的布局文件的资源标识 公共方法 protected abstract ViewnewChildView(Context context, Cursor cursor, booleanisLastChild, ViewGroup parent) 创建一个新的子元素视图并持有指向数据的游标cursor。 参数 context应用程序上下文对象 cursor获取数据的游标对象,它已经移动到正确的位置 IsLastChild子元素是否处于组中的最后一个 parent新视图(View)所依附于的父对象。 返回值 新创建的视图 protected abstract ViewnewGroupView(Context context, Cursor cursor, booleanisExpanded, ViewGroup parent) 创建一个新的组视图并持有组中指向数据的游标cursor。 参数 context应用程序上下文对象 cursor获取数据的游标对象,它已经移动到正确的位置 isExpanded该组是否展开状态 parent新视图(View)所依附于的父对象。 返回值 新创建的视图 本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/582335,如需转载请自行联系原作者

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

Hadoop MapReduce编程 API入门系列之FOF(Fund of Fund)

代码 package zhouls.bigdata.myMapReduce.friend; import org.apache.hadoop.io.Text; public class Fof extends Text{//自定义Fof,表示f1和f2关系 public Fof(){//无参构造 super(); } public Fof(String a,String b){//有参构造 super(getFof(a, b)); } public static String getFof(String a,String b){ int r =a.compareTo(b); if(r<0){ return a+"\t"+b; }else{ return b+"\t"+a; } } } package zhouls.bigdata.myMapReduce.friend; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.io.WritableComparable; public class User implements WritableComparable<User>{ //WritableComparable,实现这个方法,要多很多 //readFields是读入,write是写出 private String uname; private int friendsCount; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public int getFriendsCount() { return friendsCount; } public void setFriendsCount(int friendsCount) { this.friendsCount = friendsCount; }//这一大段的get和set,可以右键,source,产生get和set,自动生成。 public User() {//无参构造 } public User(String uname,int friendsCount){//有参构造 this.uname=uname; this.friendsCount=friendsCount; } public void write(DataOutput out) throws IOException { //序列化 out.writeUTF(uname); out.writeInt(friendsCount); } public void readFields(DataInput in) throws IOException {//反序列化 this.uname=in.readUTF(); this.friendsCount=in.readInt(); } public int compareTo(User o) {//核心 int result = this.uname.compareTo(o.getUname()); if(result==0){ return Integer.compare(this.friendsCount, o.getFriendsCount()); } return result; } } package zhouls.bigdata.myMapReduce.friend; import org.apache.hadoop.io.WritableComparable; import org.apache.hadoop.io.WritableComparator; public class FoFSort extends WritableComparator{ public FoFSort() {//把自定义的User,传进了 super(User.class,true); } public int compare(WritableComparable a, WritableComparable b) {//排序核心 User u1 =(User) a; User u2=(User) b; int result =u1.getUname().compareTo(u2.getUname()); if(result==0){ return -Integer.compare(u1.getFriendsCount(), u2.getFriendsCount()); } return result; } } package zhouls.bigdata.myMapReduce.friend; import org.apache.hadoop.io.WritableComparable; import org.apache.hadoop.io.WritableComparator; public class FoFGroup extends WritableComparator{ public FoFGroup() {//把自定义的User,传进了 super(User.class,true); } public int compare(WritableComparable a, WritableComparable b) {//分组核心 User u1 =(User) a; User u2=(User) b; return u1.getUname().compareTo(u2.getUname()); } } package zhouls.bigdata.myMapReduce.friend; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.DoubleWritable; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.StringUtils; public class RunJob { // 小明 老王 如花 林志玲 // 老王 小明 凤姐 排序在FoFSort.java // 如花 小明 李刚 凤姐 // 林志玲 小明 李刚 凤姐 郭美美 分组在FoFGroup.java // 李刚 如花 凤姐 林志玲 // 郭美美 凤姐 林志玲 // 凤姐 如花 老王 林志玲 郭美美 public static void main(String[] args) { Configuration config =new Configuration(); // config.set("fs.defaultFS", "hdfs://HadoopMaster:9000"); // config.set("yarn.resourcemanager.hostname", "HadoopMaster"); // config.set("mapred.jar", "C:\\Users\\Administrator\\Desktop\\wc.jar"); // config.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator", ",");//默认分隔符是制表符"\t",这里自定义,如"," if(run1(config)){ run2(config);//设置两个run,即两个mr。 } } public static void run2(Configuration config) { try { FileSystem fs =FileSystem.get(config); Job job =Job.getInstance(config); job.setJarByClass(RunJob.class); job.setJobName("fof2"); job.setMapperClass(SortMapper.class); job.setReducerClass(SortReducer.class); job.setSortComparatorClass(FoFSort.class); job.setGroupingComparatorClass(FoFGroup.class); job.setMapOutputKeyClass(User.class); job.setMapOutputValueClass(User.class); job.setInputFormatClass(KeyValueTextInputFormat.class); // //设置MR执行的输入文件 // FileInputFormat.addInputPath(job, new Path("hdfs://HadoopMaster:9000/f1")); // // //该目录表示MR执行之后的结果数据所在目录,必须不能存在 // Path outputPath=new Path("hdfs://HadoopMaster:9000/out/f2"); //设置MR执行的输入文件 FileInputFormat.addInputPath(job, new Path("./out/f1")); //该目录表示MR执行之后的结果数据所在目录,必须不能存在 Path outputPath=new Path("./out/f2"); if(fs.exists(outputPath)){ fs.delete(outputPath, true); } FileOutputFormat.setOutputPath(job, outputPath); boolean f =job.waitForCompletion(true); if(f){ System.out.println("job 成功执行"); } } catch (Exception e) { e.printStackTrace(); } } public static boolean run1(Configuration config) { try { FileSystem fs =FileSystem.get(config); Job job =Job.getInstance(config); job.setJarByClass(RunJob.class); job.setJobName("friend"); job.setMapperClass(FofMapper.class); job.setReducerClass(FofReducer.class); job.setMapOutputKeyClass(Fof.class); job.setMapOutputValueClass(IntWritable.class); job.setInputFormatClass(KeyValueTextInputFormat.class); // FileInputFormat.addInputPath(job, new Path("hdfs://HadoopMaster:9000/friend/friend.txt"));//下有friend.txt // // Path outpath =new Path("hdfs://HadoopMaster:9000/out/f1"); FileInputFormat.addInputPath(job, new Path("./data/friend/friend.txt"));//下有friend.txt Path outpath =new Path("./out/f1"); if(fs.exists(outpath)){ fs.delete(outpath, true); } FileOutputFormat.setOutputPath(job, outpath); boolean f= job.waitForCompletion(true); return f; } catch (Exception e) { e.printStackTrace(); } return false; } static class FofMapper extends Mapper<Text, Text, Fof, IntWritable>{ protected void map(Text key, Text value, Context context) throws IOException, InterruptedException { String user =key.toString(); String[] friends =StringUtils.split(value.toString(), '\t'); for (int i = 0; i < friends.length; i++) { String f1 = friends[i]; Fof ofof =new Fof(user, f1); context.write(ofof, new IntWritable(0)); for (int j = i+1; j < friends.length; j++) { String f2 = friends[j]; Fof fof =new Fof(f1, f2); context.write(fof, new IntWritable(1)); } } } } static class FofReducer extends Reducer<Fof, IntWritable, Fof, IntWritable>{ protected void reduce(Fof arg0, Iterable<IntWritable> arg1, Context arg2) throws IOException, InterruptedException { int sum =0; boolean f =true; for(IntWritable i: arg1){ if(i.get()==0){ f=false; break; }else{ sum=sum+i.get(); } } if(f){ arg2.write(arg0, new IntWritable(sum)); } } } static class SortMapper extends Mapper<Text, Text, User, User>{ protected void map(Text key, Text value, Context context) throws IOException, InterruptedException { String[] args=StringUtils.split(value.toString(),'\t'); String other=args[0]; int friendsCount =Integer.parseInt(args[1]); context.write(new User(key.toString(),friendsCount), new User(other,friendsCount)); context.write(new User(other,friendsCount), new User(key.toString(),friendsCount)); } } static class SortReducer extends Reducer<User, User, Text, Text>{ protected void reduce(User arg0, Iterable<User> arg1, Context arg2) throws IOException, InterruptedException { String user =arg0.getUname(); StringBuffer sb =new StringBuffer(); for(User u: arg1 ){ sb.append(u.getUname()+":"+u.getFriendsCount()); sb.append(","); } arg2.write(new Text(user), new Text(sb.toString())); } } 本文转自大数据躺过的坑博客园博客,原文链接:http://www.cnblogs.com/zlslch/p/6166095.html,如需转载请自行联系原作者

资源下载

更多资源
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文件系统,支持十年生命周期更新。

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部分的功能。

用户登录
用户注册