Android小知识10则(下)
2018-08-04 本文已影响20人
sean_depp
Android小知识10则(上)
github传送门 注: 在目录中点击可以跳转到具体代码页
目录
- Chronometer的使用
- CountDownTimer的使用
- 矩形
- 椭圆
- 线
- 环
Chronometer和CountDownTimer计时器
Android也是提供了计时器的, 虽然功能比较简单, 但是有些场景下也还是够用的...吗?(手动滑稽) CountDownTimer是倒计时计时器. Chronometer的话, 看怎么用了, 正着倒着都行...吗?(再次滑稽)
Chronometer的使用
礼貌性给下官方文档. 然后上效果图:
![](https://img.haomeiwen.com/i5319256/7570d4289e8ec923.gif)
mTimer.setBase(-60000 + SystemClock.elapsedRealtime());
mTimer.setCountDown(false);
mTimer.start();
我们以+1m(也就是从1分钟开始计时)为例:
- 先看xml代码,
android:format="%s"
是要点, 后面会说. 然后它继承自TextView, 属性设置什么的就很简单了:
<Chronometer
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="@dimen/sixteen_dp"
android:format="%s"
android:textColor="@android:color/darker_gray"
android:textSize="@dimen/thirty_sp" />
- (-60000 + SystemClock.elapsedRealtime())的出现会让你一下子懵了, 所以先说SystemClock.elapsedRealtime(). Chronometer实例是需要设置基线的, 然后用SystemClock.elapsedRealtime()减去你设置的基线值, 换句话说, 如果你写mTimer.setBase(SystemClock.elapsedRealtime());就意味着从零开始. 然后单位是ms, 一分钟就是60000ms, 所以想从一分钟开始就是(-60000 + SystemClock.elapsedRealtime())了.
- mTimer.setCountDown();代表是倒计时还是正常计时, false就是正常计时, true计时倒计时.
- 你可能会提问, 为什么我没有格式化字符串它也正常显示了. 看xml中的
android:format="%s"
, 这就是代表用默认的格式. 官方文档有这么一段: By default it will display the current timer value in the form "MM:SS" or "H:MM:SS", or you can use setFormat(String) to format the timer value into an arbitrary string. 也就是说默认"MM:SS", 超过1小时"H:MM:SS", 你可以用setFormat(String)设置你的style儿(手动滑稽).- 然后mTimer.start();是开始. mTimer.stop();是停止. 这很好理解了.
也许你会觉得它还挺好用, 但事实很残酷, 倒计时的功能要7.0才能使用, 其它的倒是兼容低版本, 但是废了一半了不是. 但是配合CountDownTimer, 意外地解决了麻烦.
CountDownTimer的使用
效果图:
![](https://img.haomeiwen.com/i5319256/32bbd585a26a08db.gif)
- 转变为静态数组
首先是ArrayList转变为静态数组, 这个算是个小知识点吧, toArray方法中的参数要写对. 代码如下:
ArrayList<String> list = new ArrayList<>();
list.add("java");
list.add("c");
list.add("c++");
String[] strings = list.toArray(new String[0]);
Log.i("tag", Arrays.toString(strings));
- 使用
使用起来也比较简单, 可以看官方文档, 或者看下我的源码, 改动改动体验下.
shape绘制
在没有UI设计师的时候, 或者是想简单看下效果的时候, 用shape进行快速绘制是极好的! 大家如果之前有关注我, 会知道这是之前一个单独的篇章, 当然不是为了凑数放在这里的, 和下一个知识点有关. 官方文档
一共有四种shape: rectangle, oval, line, ring.
矩形
我们一个一个来看, 首先是矩形:
![](https://img.haomeiwen.com/i5319256/d0e9073a7bebfebe.gif)
- 实现
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/eight_dp"
android:max="200"
android:maxHeight="@dimen/eight_dp"
android:minHeight="@dimen/eight_dp"
android:progressDrawable="@drawable/layout_progress"
android:thumb="@drawable/shape_circle" />
简单解释下几个要点属性:
- max代表进度条最大的值.
- maxHeight, minHeight可以设置进度条宽度, 我喜欢稍微宽一点的.
- thumb设置滑块, 可以是图片, 可以是shape写的设置.
- progressDrawable代表进度条的外观, 可以是图片, 可以是shape写的设置.
再来看看滑块和进度条外观具体代码, 进度条可以设置背景, 进度, 和第二进度. 滑块的话, 你想画成什么样都行.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="@dimen/four_dp" />
<solid android:color="@android:color/darker_gray" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="@dimen/four_dp" />
<solid android:color="@color/colorAccent" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="@dimen/four_dp" />
<solid android:color="@android:color/holo_blue_light" />
</shape>
</clip>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/holo_blue_light" />
<stroke
android:width="@dimen/one_dp"
android:color="@android:color/holo_blue_light" />
<size
android:width="@dimen/sixteen_dp"
android:height="@dimen/sixteen_dp" />
</shape>
java部分的话, 用Handler实例postDelayed方法让进度条跑起来就可以看到效果了. 这里设定50ms发一次消息.
findViewById(R.id.cv_start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mRunnable == null) {
mRunnable = new MyRunnable();
mHandler.postDelayed(mRunnable, 0);
}
}
});
findViewById(R.id.cv_stop).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHandler.removeCallbacks(mRunnable);
mRunnable = null;
}
});
private class MyRunnable implements Runnable {
@Override
public void run() {
int progress = mSbTest.getProgress();
mTvProgress.setText(String.valueOf(progress));
mSbTest.setProgress(++progress);
mSbTest.setSecondaryProgress(progress + 10);
int progress2 = mSbTest2.getProgress();
mTvProgress2.setText(String.valueOf(progress2));
mSbTest2.setProgress(++progress2);
mSbTest2.setSecondaryProgress(progress2 + 20);
mHandler.postDelayed(this, 50);
}
}
最后
这样就写完10个知识点了, 这样之后很多文章扩展起来就会很方便了. 喜欢记得点赞, 有意见或者建议评论区见, 暗中关注我也是可以的哦~