天气 App 中常见的 Android 自定义控件,可以用来动态展示日出和日落

Posted Wei_Leng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了天气 App 中常见的 Android 自定义控件,可以用来动态展示日出和日落相关的知识,希望对你有一定的参考价值。

SunriseSunsetView

项目地址:tianma8023/SunriseSunsetView 

简介: :sunny: An Android view to show sunrise and sunset dynamically which is common seen in Weather App. / 天气 App 中常见的 Android 自定义控件,可以用来动态展示日出和日落。 更多:作者   提 Bug    标签:

An android view to show sunrise and sunset dynamically which is common seen in Weather App.

天气 App 中常见的 Android 自定义控件,可以用来动态展示日出和日落。

Screenshot

Import

  1. To use this lib you need add jitpack.io repository to your root build.gradle:

     allprojects 
         repositories 
             jcenter()
             maven  url 'https://jitpack.io'  // add this line
         
     
    

    Note: don't add the jitpack.io repository under buildscript closure.

  2. Add SunriseSunsetView dependency in your module build.gradle:

     // ...
     // if Android Gradle Plugin after v3.0
     implementation 'com.github.tianma8023:SunriseSunsetView:0.0.2' 
     // if Android Gradle Plugin before v3.0
     // compile 'com.github.tianma8023:SunriseSunsetView:0.0.2'
    

Usage

  1. SunriseSunsetView can be placed in layout xml file:
     <com.github.tianma8023.ssv.SunriseSunsetView
         android:id="@+id/ssv"
         android:layout_height="wrap_content"
         android:layout_margin="10dp"
         android:layout_width="match_parent"
         app:ssv_label_text_color="@color/colorAccent"
         app:ssv_label_text_size="16sp"
         app:ssv_label_horizontal_gap="12dp"
         app:ssv_label_vertical_gap="2dp"
         app:ssv_shadow_color="@color/lightGreen"
         app:ssv_sun_color="@color/amber"
         app:ssv_sun_radius="10dp"
         app:ssv_track_color="@color/teal"
         app:ssv_track_width="1.5dp"
     />
    
  2. You need to set sunrise and sunset time before starting animation:
     mSunriseSunsetView = (SunriseSunsetView) findViewById(R.id.ssv);
     // ...
     mSunriseSunsetView.setSunriseTime(new Time(sunriseHour, sunriseMinute));
     mSunriseSunsetView.setSunsetTime(new Time(sunsetHour, sunsetMinute));
     // start animation
     mSunriseSunsetView.startAnimate();
    

Custom attributes are supported:

attributeformatdefaultdescription
ssv_track_colorcolor\\reference#FFFFFFtrack color
ssv_track_widthdimension4pxtrack width
ssv_sun_radiusdimension20pxsun radius
ssv_sun_colorcolor\\reference#FFFF00sun color
ssv_shadow_colorcolor\\reference#32FFFFFFshadow color
ssv_label_text_sizedimension40pxlabel text size
ssv_label_text_colorcolor\\reference#FFFFFFlabel text color
ssv_label_vertical_offsetdimension5pxlabel vertical offset
ssv_label_horizontal_offsetdimension20pxlabel horizontal offset

Other api:

// fotmat label by set label formatter
mSunriseSunsetView.setLabelFormatter(new SunriseSunsetLabelFormatter() 
    @Override
    public String formatSunriseLabel(@NonNull Time sunrise) 
        return formatLabel(sunrise);
    

    @Override
    public String formatSunsetLabel(@NonNull Time sunset) 
        return formatLabel(sunset);
    

    private String formatLabel(Time time) 
        return String.format(Locale.getDefault(), "%02dh %02dm", time.hour, time.minute);
    
);

Sample

There is a sample project in this repo, or you can download demo.apk directly.

License

SunriseSunsetView    
Copyright 2018 Tianma

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

以上是关于天气 App 中常见的 Android 自定义控件,可以用来动态展示日出和日落的主要内容,如果未能解决你的问题,请参考以下文章

Android 天气APP城市切换 之 自定义弹窗与使用

Android仿华为天气绘制刻度盘

Android自定义控件:进度条的四种实现方式

如何打造Android自定义的下拉列表框控件

怎么自定义有navigation drawer效果的Android控件

Android 天气APP获取定位信息