Menu Home

Author Archives

lnmcc

Swift:属性监视器

class StepCounter { var totalSteps: Int = 0 { //可省略newTotalSteps,swift默认为newValue,在didSet中的oldValue就是使用了swift的默认值 willSet(newTotalSteps) { println(“TotalSteps = \(newTotalSteps)”) } didSet { if totalSteps > oldValue { println(“Added \(totalSteps – oldValue) steps”) } } } } let stepCounter = StepCounter() stepCounter.totalSteps = 100 stepCounter.totalSteps = 200 stepCounter.totalSteps = 300 Output TotalSteps = 100 Added […]

Android:关于ViewGroup的ClipChildren

ClipChildren属性对需要做动画的View非常有用,特别是对ScaleAnimation。 需要注意的是ClipChildren = false只是告诉他的子View可以超出他本身的大小,并不是说他自己可以超出他的父View的大小,所以如果你想允许某个View超过其父View的边界,你需要一直找到最顶层的父View并设置ClipChildren = false。 下面需要提一下的是一个关于ClipChildren的坑,在4.2的系统上(4.3未测试,4.4及5.0版本没有这个 BUG),在启用了hardware accelerated的情况下,ScaleAnimation会出现撕裂等不流畅的现象,处理方法如下: if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { parentView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); } 这里的parentView一定要是你启用了ClipChildren = false的那个ViewGroup,否则没有效果,最佳实践是只在需要做ScaleAnimation的时候才关闭硬件加速,做完动画后应该重新开启。 关于这个BUG的更多信息请移步:这里 关于更多Android Hardware Accelerated信息请阅读:这里

Android:监控电量

获取初始电量 Android系统发送的电量广播是一个sticky broadcast,所以可以通过给registerReceiver传递一个null参数来获取上次系统发送的电量广播。 private int getLastBatteryLevel() { Intent batteryIntent = registerReceiver(null, new IntentFilter( Intent.ACTION_BATTERY_CHANGED)); return batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); } 监控电量 mBatteryReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final int level = intent.getIntExtra( BatteryManager.EXTRA_LEVEL, -1); Runnable updateBatteryLevel = new Runnable() { @Override public void run() { […]

Android:时间显示

private void initUpdateTimeThread() { Calendar calendar = Calendar.getInstance(); mHour = calendar.get(Calendar.HOUR_OF_DAY); mMinute = calendar.get(Calendar.MINUTE); setTimeString(mHour, mMinute); mUpdateTimeThread = new Runnable() { @Override public void run() { if (++mMinute >= 60) { mMinute = 0; ++mHour; } if (mHour >= 24) { mHour = 0; } setTimeString(mHour, mMinute); mHandle.postDelayed(this, 60000); } […]

java的坑:ProcessBuilder处理通配符

喝着可乐听着歌;敲着代码哼着曲。没有一点点防备,就这样掉进了坑里。 下面的代码是用通过ProcessBuilder调用命令来删除以system_开头的文件。 因为之前删除具体文件的时候这个方法非常好用,于是就想当然了。 ArrayList<String> cmd = new ArrayList<String>(); cmd.add(“rm”); cmd.add(“-vrf”); cmd.add(“/tmp/system_*”); ProcessBuilder pb = new ProcessBuilder(cmd); pb.redirectErrorStream(true); try { Process process = pb.start(); InputStream is = process.getInputStream(); BufferedReader read = new BufferedReader(new InputStreamReader(is)); String line; while ((line = read.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); […]

重写Android Viewgroup的addView方法

Viewgroup.addView()有5个重载方法: void addView(View child, int index, ViewGroup.LayoutParams params) void addView(View child, ViewGroup.LayoutParams params) void addView(View child, int index) void addView(View child) void addView(View child, int width, int height) 如果需要想重写addView()实际只需要重写addView(View child, int index, ViewGroup.LayoutParams params)即可。 看一下addView的源码: public void addView(View child) { addView(child, -1); } public void addView(View child, int index) { […]

swift: Trailing Closure

func someFunctionThatTakesAClosure(closure: () -> () ) { } Not use trailing closure someFunctionThatTakesAClosure({ }) Use trailing closure someFunctionThatTakesAClosure() { } Example let digitNames = [ 0: “Zero”, 1: “One”, 2: “two”, 3: “Three”, 4: “Foure”, 5: “Five”, 6: “Six”, 7: “Seven”, 8: “Eight”, 9: “Nine” ] let numbers = [16, […]

swift: switch where filter

好吧,swift这回要逆天了 let yetAnotherPoint = (1, -1) switch yetAnotherPoint { case let(x, y) where x == y: println(“(\(x), \(y)) is on the line x == y”) case let(x, y) where x == -y: println(“(\(x), \(y)) is on the line x == -y”) case let(x, y): println(“(\(x), \(y)) is just some arbitary […]

Android OTA升级中的一些零碎知识

OTA升级脚本 build/tools/releasetools recovery-resource.dat # Recovery is generated as a patch using both the boot image # (which contains the same linux kernel as recovery) and the file # /system/etc/recovery-resource.dat (which contains all the images # used in the recovery UI) as sources. This lets us minimize the # size of […]