<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Android仿華為手機時鐘功能

          共 8161字,需瀏覽 17分鐘

           ·

          2021-03-07 08:46

          最近看到華為手機自帶的時鐘挺漂亮的,就想著自己做一個


          先上效果圖:




          實現(xiàn)步驟:


          1、添加依賴

           implementation "io.reactivex.rxjava2:rxjava:2.2.3" implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'


          2、自定義屬性(其實可以有更多自定義屬性)

          <!--仿華為時鐘-->    <declare-styleable name="HuaWeiClock">        <!--背景顏色-->        <attr name="backgroundColor" format="color" />        <!--豎線顏色-->        <attr name="secondLineColor" format="color" />        <!--秒針點的顏色-->        <attr name="secondPointColor" format="color" />        <!--時間的顏色-->        <attr name="timeColor" format="color" />    </declare-styleable>


          3、自定義控件實現(xiàn)

          public class HuaWeiClock extends View {    /**     * 背景顏色     */    private int backgroundColor;    /**     * 秒針豎線的顏色     */    private int secondLineColor;    /**     * 秒針紅色點的顏色     */    private int secondPointColor;    /**     * 時間的顏色     */    private int timeColor;
          private Paint mPaint;
          /** * 背景寬度 */ private int widthBg; /** * 背景高度 */ private int heightBg; /** * 鐘表半徑 */ private int watchRadius; /** * 秒針紅點的半徑 */ private static final int SECOND_POINT_RADIUS = 20; /** * 線最長的長度 */ private static final int LINE_MAX_LENGTH = 60; /** * 線最短的長度 */ private static final int LINE_MIN_LENGTH = 30; /** * 秒刻度白線的數(shù)量 */ private static final int LINE_COUNT = 120;
          /** * 每根針占的角度 */ private static final int ONE_LINE_ANGLE = 360 / LINE_COUNT;
          /** * 秒針的角度 */ private float angle;
          private String time = "00:00";
          /** * 長于普通秒針刻度的駝峰一側的條數(shù) */ private static final int LONG_LINE_COUNT = 7; /** * 一側7根長針的角度 */ private static final int LONG_LINES_ANGLE = ONE_LINE_ANGLE * LONG_LINE_COUNT;

          public HuaWeiClock(Context context) { this(context, null); }
          public HuaWeiClock(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); }
          public HuaWeiClock(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HuaWeiClock); backgroundColor = typedArray.getColor(R.styleable.HuaWeiClock_backgroundColor, Color.parseColor("#003560")); secondLineColor = typedArray.getColor(R.styleable.HuaWeiClock_secondLineColor, Color.WHITE); secondPointColor = typedArray.getColor(R.styleable.HuaWeiClock_secondPointColor, Color.parseColor("#CE3228")); timeColor = typedArray.getColor(R.styleable.HuaWeiClock_timeColor, Color.WHITE); typedArray.recycle(); initPaint(); startTime(); }

          /** * 初始化畫筆 */ private void initPaint() { mPaint = new Paint(); mPaint.setAntiAlias(true); }
          /** * 獲取時間,并通過RxJava實現(xiàn)動畫 */ @SuppressLint("CheckResult") private void startTime() { //延時0秒后每隔100毫秒,刷新一次UI Observable.interval(0, 100, TimeUnit.MILLISECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new io.reactivex.functions.Consumer<Long>() { @Override public void accept(Long aLong) throws Exception { Calendar calendar = Calendar.getInstance(); int second = calendar.get(Calendar.SECOND); int milliSecond = calendar.get(Calendar.MILLISECOND); angle = (float) (second * 1000 + milliSecond) * 6 / 1000f; SimpleDateFormat minuteFormat = new SimpleDateFormat("HH:mm"); time = minuteFormat.format(calendar.getTime()); postInvalidate(); } }); }
          @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = 0; int height = 0; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec);
          switch (widthMode) { case MeasureSpec.AT_MOST: width = widthSize * 3 / 4; break; case MeasureSpec.EXACTLY: width = widthSize; break; case MeasureSpec.UNSPECIFIED: break; default: break; } switch (heightMode) { case MeasureSpec.AT_MOST: height = heightSize / 2; break; case MeasureSpec.EXACTLY: height = heightSize; break; case MeasureSpec.UNSPECIFIED: break; default: break; } widthBg = width; heightBg = height; watchRadius = height * 2 / 5; setMeasuredDimension(width, height); }
          @Override protected void onDraw(Canvas canvas) { drawBg(canvas); drawTickMark(canvas); drawSecondPoint(canvas); drawTime(canvas); }
          /** * 畫背景 * * @param canvas 畫板 */ private void drawBg(Canvas canvas) { mPaint.setColor(backgroundColor); mPaint.setStyle(Paint.Style.FILL); RectF rectBg = new RectF(0, 0, widthBg, heightBg); canvas.drawRect(rectBg, mPaint); }
          /** * 畫刻度線 * * @param canvas 畫板 */ private void drawTickMark(Canvas canvas) { mPaint.setColor(secondLineColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(3); for (int i = 0; i < LINE_COUNT; i++) { //角度差的絕對值 float absoluteValue = Math.abs(i * ONE_LINE_ANGLE - angle); //求 刻度針線 和 秒針 之間的角度(小于180度) float differenceAngle = absoluteValue <= 180 ? absoluteValue : 360 - absoluteValue; //大于七根長線刻度的線,那么長度固定 if (differenceAngle > LONG_LINES_ANGLE) { canvas.drawLine(widthBg / 2, heightBg / 2 - watchRadius, widthBg / 2, heightBg / 2 - watchRadius + LINE_MIN_LENGTH, mPaint); canvas.rotate(3, widthBg / 2, heightBg / 2); } else { canvas.drawLine(widthBg / 2, heightBg / 2 - watchRadius - (1 - differenceAngle / LONG_LINES_ANGLE) * (LINE_MAX_LENGTH - LINE_MIN_LENGTH), widthBg / 2, heightBg / 2 - watchRadius + LINE_MIN_LENGTH, mPaint); canvas.rotate(3, widthBg / 2, heightBg / 2); } } }
          /** * 畫秒表的紅點 * * @param canvas 畫布 */ private void drawSecondPoint(Canvas canvas) { mPaint.setColor(secondPointColor); mPaint.setStyle(Paint.Style.FILL); int secondPointRadiusBig = watchRadius - LINE_MIN_LENGTH - SECOND_POINT_RADIUS - 20; double pointAngle = angle * Math.PI / 180; canvas.drawCircle((float) (widthBg / 2 + secondPointRadiusBig * Math.sin(pointAngle)), (float) (heightBg / 2 - secondPointRadiusBig * Math.cos(pointAngle)), SECOND_POINT_RADIUS, mPaint); }
          /** * 畫時間 * * @param canvas 畫布 */ private void drawTime(Canvas canvas) { mPaint.setColor(timeColor); mPaint.setStyle(Paint.Style.FILL); mPaint.setTextSize(widthBg / 7); Rect bounds = new Rect(); mPaint.getTextBounds(time, 0, time.length(), bounds); Paint.FontMetricsInt fontMetrics = mPaint.getFontMetricsInt(); int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top; canvas.drawText(time, getMeasuredWidth() / 2 - bounds.width() / 2, baseline, mPaint); }}


          4、布局文件中引入

           <com.sjl.keeplive.huawei.HuaWeiClock        android:id="@+id/huaWeiClock"        android:layout_width="match_parent"        android:layout_height="wrap_content"        app:backgroundColor="#003560"        app:secondLineColor="@android:color/white"        app:secondPointColor="#CE3228"        app:timeColor="@android:color/white" />


          到這里就完成了,代碼都貼出來啦.


          點擊這里留言交流哦


          瀏覽 79
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美suv无码 | 日本一级 黄 色 片图片视频 | 人人看人人玩人人摸 | 午夜影院操 | 翔田千里无码视频 |