• 原文链接:http://daemon369.github.io/android/2014/06/12/android-viewdebug-exportedproperty/
  • http://www.eoeandroid.com/thread-55461-1-1.html;

This annotation can be used to mark fields and methods to be dumped by the view server. Only non-void methods with no arguments can be annotated by this annotation. 
这个注释可以用来标记域和方法,该域和方法将会被视服务所“转存”,只有不带参数的非空方法才能被这个注释所诠释;

在查看View源码时,但不清楚@ViewDebug.ExportedProperty的用法:

  1. /**
  2. * The view flags hold various views states.
  3. * {@hide}
  4. */
  5. @ViewDebug.ExportedProperty
  6. int mViewFlags;
  7.  
  8. /**
  9. * The distance in pixels from the left edge of this view's parent
  10. * to the left edge of this view.
  11. * {@hide}
  12. */
  13. @ViewDebug.ExportedProperty(category = "layout")
  14. protected int mLeft;
  15. /**
  16. * The distance in pixels from the left edge of this view's parent
  17. * to the right edge of this view.
  18. * {@hide}
  19. */
  20. @ViewDebug.ExportedProperty(category = "layout")
  21. protected int mRight;
  22. /**
  23. * The distance in pixels from the top edge of this view's parent
  24. * to the top edge of this view.
  25. * {@hide}
  26. */
  27. @ViewDebug.ExportedProperty(category = "layout")
  28. protected int mTop;
  29. /**
  30. * The distance in pixels from the top edge of this view's parent
  31. * to the bottom edge of this view.
  32. * {@hide}
  33. */
  34. @ViewDebug.ExportedProperty(category = "layout")
  35. protected int mBottom;
  36.  
  37. /**
  38. * The offset, in pixels, by which the content of this view is scrolled
  39. * horizontally.
  40. * {@hide}
  41. */
  42. @ViewDebug.ExportedProperty(category = "scrolling")
  43. protected int mScrollX;
  44. /**
  45. * The offset, in pixels, by which the content of this view is scrolled
  46. * vertically.
  47. * {@hide}
  48. */
  49. @ViewDebug.ExportedProperty(category = "scrolling")
  50. protected int mScrollY;
  51.  
  52. /**
  53. * The left padding in pixels, that is the distance in pixels between the
  54. * left edge of this view and the left edge of its content.
  55. * {@hide}
  56. */
  57. @ViewDebug.ExportedProperty(category = "padding")
  58. protected int mPaddingLeft;
  59. /**
  60. * The right padding in pixels, that is the distance in pixels between the
  61. * right edge of this view and the right edge of its content.
  62. * {@hide}
  63. */
  64. @ViewDebug.ExportedProperty(category = "padding")
  65. protected int mPaddingRight;
  66. /**
  67. * The top padding in pixels, that is the distance in pixels between the
  68. * top edge of this view and the top edge of its content.
  69. * {@hide}
  70. */
  71. @ViewDebug.ExportedProperty(category = "padding")
  72. protected int mPaddingTop;
  73. /**
  74. * The bottom padding in pixels, that is the distance in pixels between the
  75. * bottom edge of this view and the bottom edge of its content.
  76. * {@hide}
  77. */
  78. @ViewDebug.ExportedProperty(category = "padding")
  79. protected int mPaddingBottom;
  80.  
  81. /**
  82. * Briefly describes the view and is primarily used for accessibility support.
  83. */
  84. private CharSequence mContentDescription;
  85.  
  86. /**
  87. * Cache the paddingRight set by the user to append to the scrollbar's size.
  88. */
  89. @ViewDebug.ExportedProperty(category = "padding")
  90. int mUserPaddingRight;
  91.  
  92. /**
  93. * Cache the paddingBottom set by the user to append to the scrollbar's size.
  94. */
  95. @ViewDebug.ExportedProperty(category = "padding")
  96. int mUserPaddingBottom;

使用@ViewDebug.ExportedProperty注解,我们可以在android提供的工具Monitor(或已经废弃的DDMS)中的Hierarchy Viewer中调试View的属性。我们可以直接观察View的某个变量或方法的值,实时观察View的状态变化。


ExprotedProperty注解源码

  1. /**
  2. * This annotation can be used to mark fields and methods to be dumped by
  3. * the view server. Only non-void methods with no arguments can be annotated
  4. * by this annotation.
  5. */
  6. @Target({ ElementType.FIELD, ElementType.METHOD })
  7. @Retention(RetentionPolicy.RUNTIME)
  8. public @interface ExportedProperty {
  9. /**
  10. * When resolveId is true, and if the annotated field/method return value
  11. * is an int, the value is converted to an Android's resource name.
  12. *
  13. * @return true if the property's value must be transformed into an Android
  14. * resource name, false otherwise
  15. */
  16. boolean resolveId() default false;
  17. /**
  18. * A mapping can be defined to map int values to specific strings. For
  19. * instance, View.getVisibility() returns 0, 4 or 8. However, these values
  20. * actually mean VISIBLE, INVISIBLE and GONE. A mapping can be used to see
  21. * these human readable values:
  22. *
  23. * <pre>
  24. * @ViewDebug.ExportedProperty(mapping = {
  25. * @ViewDebug.IntToString(from = 0, to = "VISIBLE"),
  26. * @ViewDebug.IntToString(from = 4, to = "INVISIBLE"),
  27. * @ViewDebug.IntToString(from = 8, to = "GONE")
  28. * })
  29. * public int getVisibility() { ...
  30. * <pre>
  31. *
  32. * @return An array of int to String mappings
  33. *
  34. * @see android.view.ViewDebug.IntToString
  35. */
  36. IntToString[] mapping() default { };
  37. /**
  38. * A mapping can be defined to map array indices to specific strings.
  39. * A mapping can be used to see human readable values for the indices
  40. * of an array:
  41. *
  42. * <pre>
  43. * @ViewDebug.ExportedProperty(indexMapping = {
  44. * @ViewDebug.IntToString(from = 0, to = "INVALID"),
  45. * @ViewDebug.IntToString(from = 1, to = "FIRST"),
  46. * @ViewDebug.IntToString(from = 2, to = "SECOND")
  47. * })
  48. * private int[] mElements;
  49. * <pre>
  50. *
  51. * @return An array of int to String mappings
  52. *
  53. * @see android.view.ViewDebug.IntToString
  54. * @see #mapping()
  55. */
  56. IntToString[] indexMapping() default { };
  57. /**
  58. * A flags mapping can be defined to map flags encoded in an integer to
  59. * specific strings. A mapping can be used to see human readable values
  60. * for the flags of an integer:
  61. *
  62. * <pre>
  63. * @ViewDebug.ExportedProperty(flagMapping = {
  64. * @ViewDebug.FlagToString(mask = ENABLED_MASK, equals = ENABLED, name = "ENABLED"),
  65. * @ViewDebug.FlagToString(mask = ENABLED_MASK, equals = DISABLED, name = "DISABLED"),
  66. * })
  67. * private int mFlags;
  68. * <pre>
  69. *
  70. * A specified String is output when the following is true:
  71. *
  72. * @return An array of int to String mappings
  73. */
  74. FlagToString[] flagMapping() default { };
  75. /**
  76. * When deep export is turned on, this property is not dumped. Instead, the
  77. * properties contained in this property are dumped. Each child property
  78. * is prefixed with the name of this property.
  79. *
  80. * @return true if the properties of this property should be dumped
  81. *
  82. * @see #prefix()
  83. */
  84. boolean deepExport() default false;
  85. /**
  86. * The prefix to use on child properties when deep export is enabled
  87. *
  88. * @return a prefix as a String
  89. *
  90. * @see #deepExport()
  91. */
  92. String prefix() default "";
  93. /**
  94. * Specifies the category the property falls into, such as measurement,
  95. * layout, drawing, etc.
  96. *
  97. * @return the category as String
  98. */
  99. String category() default "";
  100. }

  


使用

下面是@ViewDebug.ExportedProperty注解的部分属性的使用介绍:

1.category

category用来指定属性的类别,例如measurement, layout, drawing等。我们在自定义View中为使用@ViewDebug.ExportedProperty注解的变量或方法指定category:

  1. @ExportedProperty(category = "marquee")
  2. int x = 1;
  3. @Override
  4. @ExportedProperty(category = "marquee")
  5. public boolean isFocused() {
  6. return true;
  7. }

运行程序,在Hierarchy Viewer中查看View的属性如下:

2.resolveId

当resolveId为true时,如果使用注解的变量或方法的值为int数据,那么这个值会被转化为对应的Android资源的名称。

在R.java中找到一个资源ID:

  1. public static final int activity_main=0x7f030000;

使用注解属性resolveId:

  1. @ExportedProperty(category = "marquee", resolveId = true)
  2. int b = 0x7f030000;

结果如下:

3.mapping

mapping可以将int值映射到指定的字符串值,例如View.getVisibility()返回的值是int值,View中使用注解将其映射为字符串,其中0为”VISIBLE”,4为”INVISIBLE”,8为”GONE”。我们重载View.getVisibility()并加上我们自己定制的映射:

  1. @Override
  2. @ViewDebug.ExportedProperty(category = "marquee",
  3. mapping = {
  4. @ViewDebug.IntToString(from = VISIBLE, to = "MARQUEE_VISIBLE"),
  5. @ViewDebug.IntToString(from = INVISIBLE, to = "MARQUEE_INVISIBLE"),
  6. @ViewDebug.IntToString(from = GONE, to = "MARQUEE_GONE")
  7. })
  8. public int getVisibility() {
  9. return super.getVisibility();
  10. }

结果如下:

4.indexMapping

indexMapping可以将数组的序号映射为字符串。

  1. @ExportedProperty(category = "marquee",
  2. indexMapping = {
  3. @ViewDebug.IntToString(from = 0, to = "MARQUEE_FIRST"),
  4. @ViewDebug.IntToString(from = 1, to = "MARQUEE_SECOND"),
  5. @ViewDebug.IntToString(from = 2, to = "MARQUEE_THIRD")
  6. })
  7. int[] elements = new int[] {
  8. 111, 222, 333
  9. };

结果如下:


Demo

  1. package com.daemon.demo;
  2. import android.content.Context;
  3. import android.graphics.Rect;
  4. import android.util.AttributeSet;
  5. import android.view.ViewDebug;
  6. import android.view.ViewDebug.ExportedProperty;
  7. import android.widget.TextView;
  8. public class MarqueeText extends TextView {
  9. public MarqueeText(Context context) {
  10. super(context);
  11. }
  12. public MarqueeText(Context context, AttributeSet attrs) {
  13. super(context, attrs);
  14. }
  15. public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
  16. super(context, attrs, defStyle);
  17. }
  18. @Override
  19. protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
  20. }
  21. @ExportedProperty(category = "marquee")
  22. int x = 1;
  23. @Override
  24. @ExportedProperty(category = "marquee")
  25. public boolean isFocused() {
  26. return true;
  27. }
  28. @ExportedProperty(category = "marquee", resolveId = true)
  29. int b = 0x7f030000;
  30. @Override
  31. @ViewDebug.ExportedProperty(category = "marquee",
  32. mapping = {
  33. @ViewDebug.IntToString(from = VISIBLE, to = "MARQUEE_VISIBLE"),
  34. @ViewDebug.IntToString(from = INVISIBLE, to = "MARQUEE_INVISIBLE"),
  35. @ViewDebug.IntToString(from = GONE, to = "MARQUEE_GONE")
  36. })
  37. public int getVisibility() {
  38. return super.getVisibility();
  39. }
  40. @ExportedProperty(category = "marquee",
  41. indexMapping = {
  42. @ViewDebug.IntToString(from = 0, to = "MARQUEE_FIRST"),
  43. @ViewDebug.IntToString(from = 1, to = "MARQUEE_SECOND"),
  44. @ViewDebug.IntToString(from = 2, to = "MARQUEE_THIRD")
  45. })
  46. int[] elements = new int[] {
  47. 111, 222, 333
  48. };
  49. }

 

@ViewDebug.ExportedProperty的使用的更多相关文章

  1. 和我一起看API(一)你所不知道的LinearLayout补充

    楼主英语水平差,翻译的不好的话请多多指正,嘿嘿... A Layout that arranges its children in a single column or a single row. T ...

  2. View Focus的处理过程及ViewGroup的mFocused字段分析

    通过上篇的介绍,我们知道在对KeyEvent的处理中有非常重要的一环,那就是KeyEvent在focus view的path上自上而下的分发, 换句话说只有focus的view才有资格参与KeyEve ...

  3. scrollTo 和 scrollBy

      涉及到滑动,就涉及到VIEW,大家都知道,Android的UI界面都是由一个一个的View以及View的派生类组成,View作为基类,而常用的布局里面的各种布局就是它派生出来的ViewGroup的 ...

  4. 滑动的Button

    在介绍SwitchButton之前,先来看一下系统Button是如何实现的.源码如下: @RemoteView public class Button extends TextView { publi ...

  5. 简单研究Android View绘制二 LayoutParams

    2015-07-28 17:23:20 本篇是关于LayoutParams相关 ViewGroup.LayoutParams文档解释如下: LayoutParams are used by views ...

  6. Android 实现多行文本跑马灯效果

    Android TextView 实现跑马灯的效果很简单,只要加三个属性就可以了. android:ellipsize="marquee" android:focusable=&q ...

  7. Android开发之Theme、Style探索及源码浅析

    1 背景 前段时间群里有伙伴问到了关于Android开发中Theme与Style的问题,当然,这类东西在网上随便一搜一大把模板,所以关于怎么用的问题我想这里也就不做太多的说明了,我们这里把重点放在理解 ...

  8. 向左对齐的Gallery

    系统自带的Gallery选中的item总是在组件的中间.但是有些时候我们需要把选中的元素放在左边或者是Gallery一出来就要放在左边.修改Gallery靠左对齐的思路:1.Gellary总是对cen ...

  9. android 布局之滑动探究 scrollTo 和 scrollBy 方法使用说明

    涉及到滑动,就涉及到VIEW,大家都知道,Android的UI界面都是由一个一个的View以及View的派生类组成,View作为基类,而常用的布局里面的各种布局就是它派生出来的ViewGroup的子类 ...

随机推荐

  1. yum命令——安装、卸载、查询等

    --常用命令 1.安装软件 yum install 软件名称 2.卸载软件 yum remove 软件名称 3.更新软件 yum update 软件名称 4.列出所有可安装的软件包 yum list ...

  2. Java cookie和session介绍与区别

    一.cookie机制和session机制的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. 同时我们也看到,由于才服务器端保持状态的 ...

  3. 搞定迅雷固件在TP-LINK WR720N,127.0.0.1 9000 获取不到激活码

    本帖最后由 exzzzipad 于 2014-7-2 22:33 编辑 基本情况:设备:TP-LINK WR720N目前固件:[antclan][20120825]720N-4M-NAS-withSA ...

  4. C#获取网页内容的三种方式

    C#通常有三种方法获取网页内容,使用WebClient.WebBrowser或者HttpWebRequest/HttpWebResponse... 方法一:使用WebClient (引用自:http: ...

  5. C# 多线程线程池( 一 )

    我们将在这里进一步讨论一些.NET类,以及他们在多线程编程中扮演的角色和怎么编程.它们是: System.Threading.ThreadPool 类 System.Threading.Timer 类 ...

  6. 服务器RAS性能

    服务器的安全性能要求非常高,主要体现在RAS性能上.RAS性能指的是机器的可靠性(Reliability).可用性(Availability)和可服务性(Serviceability).RAS能力主要 ...

  7. ACM知识点

    基础算法 高精 模拟 分治 贪心 排序 DFS 迭代加深搜索 BFS 双向BFS 动态规划 DAG上DP 树上DP 线性DP 图算法 最短路 FLYD DJATL BF 最大流 Dinic ISAP ...

  8. NOIP2007 矩阵取数游戏

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

  9. 1、基于MFC的OpenGL程序

    首先,使用的库是GLUT以及GLAUX,先下载两者,添加查找路径以及链接   一.单文本文件   工程openGLMFC 1.创建单文本文件   2.添加路径.链接 方法如之前篇章所示, 链接库为op ...

  10. Debian7下初次尝试Nginx+Uwsgi部署Django开发环境

    之前一直都用的是新浪的SAE,但是由于各种限制,各种不爽,终于下定决心开始折腾VPS,于是在搬瓦工上买了个年付VPS,开始折腾之旅. 由于对Linux一窍不通,所以不知道如何在Linux上部署开发环境 ...