Carina
  • home
  • Notes

Posts

November 30, 2019

Webp解码RGB_565问题记录

最近遇到自定义webp解码使用rgb565时出现颜色错乱的问题,查了比较长时间,总结一下过程。 开始时怀疑代码逻辑有问题,于是写demo测试native代码逻辑,尝试修改Bitmap的一些属性,确认代码逻辑正确。 然后考虑到用系统解码api和官方ndk sample中的webp解码示例都正常,怀疑是不是对libwebp的调用方式有问题,于是去看源码。先从简单的ndk sample开始看,发现调用方式是一样的,然后看了系统webp解码部分源码,发现系统使用的解码函数不一样,尝试换成跟系统一样的函数,问题依然存在。由于官方示例并没有将图片解码成Bitmap,所以接着仔细看系统源码的解码过程,看是不是在某些细节里有特殊处理,最后没有发现有特殊的地方。 看了几天源码无果后,最后想到是不是解码得到的Bitmap的底层像素数据不一样,而不是对Bitmap的设置不一样,于是写测试代码获取Bitmap的像素字节数组,对2种方式解码得到的Bitmap的像素数据进行比较,发现同一像素的2个字节的位置刚好相反。 [74, 105, -125, -18, ...] [105, 74, -18, -125, ...] 到这里终于有点眉目了,后面就去找导致位置相反的原因,然后在libwebp源码定义像素格式的地方发现了一段注释,感觉很可能跟WEBP_SWAP_16BITS_CSP有关,但在代码中没有找到这个变量。 // ... // RGB-565: [r4 r3 r2 r1 r0 g5 g4 g3], [g2 g1 g0 b4 b3 b2 b1 b0], ... // In the case WEBP_SWAP_16BITS_CSP is defined, the bytes are swapped for // these two modes: // RGBA-4444: [b3 b2 b1 b0 a3 a2 a1 a0], [r3 r2 r1 r0 g3 g2 g1 g0], .
September 24, 2017

Android task

singleTask and FLAG_ACTIVITY_NEW_TASK 根据下面引用的官方文档的说法,singleTask和FLAG_ACTIVITY_NEW_TASK的行为是一致的,但是根据实际测试结果,二者的行为是有差别的。 This produces the same behavior as the “singleTask” launchMode value, discussed in the previous section. 二者在决定activity属于哪个task上是一致的,都根据taskAffinity(如果没设,默认为包名)来决定activity属于哪个task,如果当前存在一个名称跟taskAffinity相同的task,则将activity置于已有的这个task,否则创建一个新task,并将activity作为这个task的根activity。 二者在启动activity的行为上是不一致的,如果activity所属的task已存在且task中已存在这个activity的实例,二者都会将整个task移到前台,但singleTask会关闭这个activity上面的所有activity,并调用这个activity的onNewIntent方法,而对于FLAG_ACTIVITY_NEW_TASK,如果启动的activity是这个task的根activity,不做任何事情,否则会创建一个新的activity。 Exported activity 启动其他app的exported activity时,会忽略launchMode,此时只能用FLAG_ACTIVITY_NEW_TASK来创建新task。 startActivityForResult 调用startActivityForResult来启动activity时,如果使用了FLAG_ACTIVITY_NEW_TASK,启动activity后会立即收到onActivityResult回调,且finish启动的activity后不会再收到onActivityResult回调,而使用singleTask和singleInstance则是正常的行为。
September 10, 2017

Android memory leak

Leak because of system bug PhoneStateListener leak Below 7.0 a non static inner class IPhoneStateListener.Stub callback in PhoneStateListener references to outside PhoneStateListener, even caller has been destroyed and “un-registered” the PhoneStateListener, the references coming from: Native Stack –> PhoneStateListener –> Context(Activity). A wrapper class wraps a weak reference of PhoneStateListener can be used to avoid this memory leak. Caution: The original PhoneStateListener object must be referenced by caller class, otherwise the weak reference in the wrapper class will get null.
July 9, 2017

Android Studio 3.0 Device File Explorer & Fonts

Device File Explorer 使用设备的文件系统,可按如下的操作方式: 点击View > Tool Windows > Device File Explorer或者点击右下角位于工具窗口条中的Device File Explorer按钮来打开Device File Explorer。 从上方的下拉列表中选择设备。 在文件浏览窗口中操作设备文件。鼠标右键点击文件或目录来创建一个新文件或目录,保存现在的文件或目录到电脑中,上传、删除或同步文件。鼠标双击文件可以在Android Studio中打开文件。 注意:Android Studio 将用 Device File Explorer 打开的文件保存在工程外的临时目录中,如果在 Device File Explorer 中修改文件,并想把修改保存到设备中,需要手动将修改的文件上传到设备中。 Figure1 Device File Explorer窗口 当浏览设备文件时,以下2个目录是经常访问的: data/data/app_name/:包含存储于内部存储空间的应用数据文件 sdcard/:包含存储于外部存储空间的用户文件 注意:不是所有的文件都在Device File Explorer中可见。例如在**data/data/**目录中,非debuggable应用的文件是不能展开的。 XML Fonts & Downloadable Fonts Android O 引入了XML字体新特性,可以将字体作为资源使用。 XML Fonts Android O 可以通过将字体文件加入res/font目录来将字体作为资源使用。这些字体会被编译到R文件中,并在 Android Studio 中可用。可以通过新的资源类型font来访问字体资源,例如用@font/myfont或者R.font.myfont来访问一个字体资源。 在 Android Studio 中可以通过执行以下步骤来字体资源: 鼠标右键点击res目录,选择New > Android Resource Directory,打开New Resource Directory窗口。 在Resource type列表里选择font,然后点击OK。 Figure2 添加字体目录
March 21, 2017

Create avd and start app with command line

Install sdk, create or delete avd android list sdk -a android update sdk -a -u --filter [id1, id2] android list targets android create avd -n 4.1-x86 -t 1 -b x86 -c 512M android delete avd -n 4.1-x86 Start emulator and launch app android list avd emulator64-x86 -avd 4.1-x86 -no-window -no-boot-anim -qemu -m 1024 -enable-kvm & adb shell am start fm.xiami.main/fm.xiami.bmamba.activity.StartActivity adb forward tcp:11874 tcp:11873 on 64-bits machines you may need install ia32-libs to run adb:
March 14, 2017

VectorDrawable clip path

Function clip path限制在画布上绘制的区域,在clip path所指定的区域外的位置不会被绘制。 我们通过下面的心形图标来展示clip path的效果: <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="56dp" android:height="56dp" android:viewportHeight="56" android:viewportWidth="56"> <path android:fillColor="@android:color/black" android:pathData="M28,39 L26.405,37.5667575 C20.74,32.4713896 17,29.1089918 17,24.9945504 C17,21.6321526 19.6565,19 23.05,19 C24.964,19 26.801,19.8828338 28,21.2724796 C29.199,19.8828338 31.036,19 32.95,19 C36.3435,19 39,21.6321526 39,24.9945504 C39,29.1089918 35.26,32.4713896 29.595,37.5667575 L28,39 L28,39 Z"/> </vector> Figure1 心形图标 下面加入<clip-path>将绘制区域限制在[0, 0, 56, 28]的矩形区域内: <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="56dp" android:height="56dp" android:viewportHeight="56" android:viewportWidth="56"> <clip-path android:name="clip" android:pathData="M0 0 L56 0 L56 28 L0 28 Z"/> <path android:fillColor="@android:color/black" android:pathData="M28,39 L26.405,37.5667575 C20.74,32.4713896 17,29.1089918 17,24.9945504 C17,21.6321526 19.6565,19 23.
© Carina 2023