Android——一个简单的智能家居系统

Android——一个简单的智能家居系统,第1张

概述一个简单的智能家居系统效果展示启动应用界面登陆界面导航界面温度界面湿度界面烟雾传感器界面人体红外传感器界面效果展示以下为整个程序的 *** 作流程,因为CSDN不能上传太大文件,所以画质比较模糊。启动应用界面先来看一下启动界面:效果图如下:这是一个比较简单的布

一个简单的智能家居系统效果展示启动应用界面登陆界面导航界面温度界面湿度界面烟雾传感器界面人体红外传感器界面

效果展示

以下为整个程序的 *** 作流程,因为CSDN不能上传太大文件,所以画质比较模糊。

启动应用界面

先来看一下启动界面:效果图如下:


这是一个比较简单的布局由一个ImageVIEw,TextvIEw,Switch组成
其中Switch组件的样式由俩个文件组成,thumb.xml,track.xml(都在Drawable文件里面创建)
thumb.xml文件代码:
其实两个Item都是一样的效果,因为thumb代表的滑动的轨迹,可以理解为滑块。一个是被按下的状态,一个是普通时的状态

<selector xmlns:androID="http://schemas.androID.com/apk/res/androID"><item androID:state_checked="true" androID:drawable="@drawable/open_thumb"/><item androID:drawable="@drawable/shut_thumb"/></selector>

因为两个item都是一样的,那我们就分析其中一个item,下面为滑块的效果


open_thumb.xml代码如下:

    <shape xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:shape="rectangle" ><!-- 高度40 --><size androID:height="40dp" androID:wIDth="40dp"/><!-- 圆角弧度 20 --><corners androID:radius="20dp"/><!-- 渐变色 --><gradIEnt    androID:endcolor="#eeeeee"    androID:startcolor="#eeeeee" />    <!--描边的大小和颜色--><stroke androID:wIDth="1dp"    androID:color="#33da33"/></shape>

我们接下来看一下承载滑块容易的track.xml他们同样表示两种状态,但他们是不一样的效果,因为为了突出滑动与未滑动的区别,所以背景色不一样,代码如下:

<selector xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item androID:state_checked="true" androID:drawable="@drawable/open_track"/>    <item androID:drawable="@drawable/shut_track"/></selector>

我们看一个open_track.xml文件效果和代码

<shape xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:shape="rectangle">        <!-- 高度30   此处设置宽度无效-->        <size androID:height="40dp"/>        <!-- 圆角弧度 15 -->        <corners androID:radius="15dp"/>        <!-- 变化率  定义从左到右的颜色不变 -->        <gradIEnt            androID:endcolor="#66ff33"            androID:startcolor="#66ff33" /></shape>

看一下未滑动时的状态布局文件shut_track.xml

<shape xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:shape="rectangle">    <size androID:height="40dp" androID:wIDth="40dp"/>    <corners androID:radius="20dp"/>    <gradIEnt androID:startcolor="#eeeeee"        androID:endcolor="#eeeeee"/>    <stroke androID:wIDth="1dp"            androID:color="#666666"/></shape>

看一下整体welcome.xml布局文件的代码如下:

<linearLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    androID:background="@drawable/background1">    <ImageVIEw        androID:ID="@+ID/image"        androID:layout_wIDth="150dp"        androID:layout_height="150dp"        androID:src="@drawable/home"        androID:layout_gravity="center"        androID:layout_margintop="100dp"/>    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="智能家居"        androID:textSize="30sp"        androID:layout_gravity="center"        androID:layout_marginleft="0dp"        androID:textcolor="#00ffcc"        androID:layout_margintop="20dp"        />    <Switch        androID:ID="@+ID/Open"        androID:layout_wIDth="100dp"        androID:layout_height="100dp"        androID:thumb="@drawable/thumb"        androID:track="@drawable/track"        androID:layout_gravity="center"        androID:layout_margintop="50dp"        androID:layout_marginRight="15dp"/></linearLayout>

然后分析一下Welcome.java代码
代码组成也比较简单,主要是在标题栏添加添加一个back键,并设置此键的功能为返回桌面(这种方式并为杀死进程,只是退出到界面,一个finish()方法也仅仅结束当前页,如果栈里面存在多个实例,并不会杀死进程,只会反复在几个页面跳转,杀死进程可以使用广播的方式,此处略过)。然后就是声明Switch控件,并对他进行监听,然后进行一个判断,如果滑动了就跳转到登陆界面

public class Welcome extends AppCompatActivity {    private Switch Open;    private ImageVIEw imageVIEw;    private List<Activity> List = new ArrayList<>();    private  ExitAllProcess exit = new ExitAllProcess();    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_welcom);       // exit.addActivity(this);//        imageVIEw = findVIEwByID(R.ID.image);//        Intent intent = getIntent();//        Bundle bundle = intent.getExtras();//        int vIEw = bundle.getInt("CustomvIEw");//        imageVIEw.setimageResource(vIEw);        SetTitle();        Open = findVIEwByID(R.ID.Open);        Open.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) {                if (isChecked){                    Intent intent = new Intent(Welcome.this,Login.class);                    startActivity(intent);                }            }        });    }    private voID SetTitle() {        Actionbar actionbar = getSupportActionbar();        if (actionbar != null) {            actionbar.setdisplayHomeAsUpEnabled(true);            actionbar.setHomebuttonEnabled(true);        }    }    @OverrIDe    public boolean onoptionsItemSelected(@NonNull MenuItem item) {        switch (item.getItemID()){            case androID.R.ID.home:                Intent MyIntent = new Intent(Intent.ACTION_MAIN);                MyIntent.addcategory(Intent.category_HOME);                startActivity(MyIntent);                finish();                break;        }        return super.onoptionsItemSelected(item);    }}
登陆界面

界面如下:


login.xml布局文件代码如下:
布局较为简单,那个小眼睛就显示密码和隐藏密码,然后采用一些自定义Drawable,把EditText弄的圆一点,美观一点,其余没什么。

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    androID:background="@drawable/background">    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_gravity="center"        androID:orIEntation="vertical">        <ImageVIEw            androID:layout_wIDth="150dp"            androID:layout_height="100dp"            androID:layout_gravity="center"            androID:layout_margintop="50dp"            androID:layout_marginleft="30dp"            androID:src="@drawable/home"            androID:scaleType="fitStart"/>        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="智能家居"            androID:gravity="center"            androID:layout_gravity="center"            androID:textSize="30sp"            androID:textcolor="#000000"            androID:layout_marginBottom="30dp"            androID:layout_margintop="10dp"            androID:layout_marginleft="10dp"/>    </linearLayout>    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:orIEntation="horizontal"        androID:layout_marginBottom="10dp">        <TextVIEw            androID:layout_wIDth="70dp"            androID:layout_height="50dp"            androID:text="账 号:"            androID:textSize="20sp"            androID:textcolor="#000000"            androID:layout_marginleft="20dp"/>        <EditText            androID:ID="@+ID/username"            androID:layout_wIDth="260dp"            androID:layout_height="40dp"            androID:background="@drawable/passwordBox"            androID:hint="用户名"            androID:paddingleft="10dp"            androID:textcolor="#ff000000"            androID:textSize="20sp"            androID:layout_marginleft="0dp"/>    </linearLayout>    <VIEw        androID:layout_wIDth="match_parent"        androID:layout_height="1dp"        androID:background="#000000" />    <relativeLayout        androID:layout_wIDth="match_parent"        androID:layout_height="60dp"        >        <TextVIEw            androID:ID="@+ID/TipsPassWord"            androID:layout_wIDth="70dp"            androID:layout_height="50dp"            androID:text="密 码:"            androID:textSize="20sp"            androID:textcolor="#000000"            androID:layout_margintop="25dp"            androID:layout_marginleft="20dp"/>        <EditText            androID:ID="@+ID/password"            androID:layout_wIDth="260dp"            androID:layout_height="40dp"            androID:background="@drawable/passwordBox"            androID:hint="密   码"            androID:paddingleft="10dp"            androID:textcolor="#ff000000"            androID:textSize="20sp"            androID:password="true"            androID:layout_toRightOf="@+ID/TipsPassWord"            androID:layout_margintop="20dp"/>        <ImageVIEw            androID:ID="@+ID/notseethepassword"            androID:layout_wIDth="50dp"            androID:layout_height="50dp"            androID:scaleType="fitCenter"            androID:background="@drawable/passwordBox"            androID:layout_marginleft="300dp"            androID:layout_margintop="20dp"/>        <!--<Imagebutton            androID:ID="@+ID/seethepassword"            androID:layout_wIDth="60dp"            androID:layout_height="50dp"            androID:src="@mipmap/openeye"            androID:scaleType="fitCenter"            androID:background="#ffffff"            androID:layout_marginleft="320dp"            androID:visibility="invisible"            />-->    </relativeLayout>    <button        androID:ID="@+ID/login"        androID:layout_wIDth="150dp"        androID:layout_height="wrap_content"        androID:layout_marginleft="130dp"        androID:text="登 陆"        androID:textcolor="#ffffffff"        androID:textSize="20sp"        androID:background="@drawable/login"        androID:layout_margintop="40dp"        /></linearLayout>

那我们现在看一下Login.java里面的代码:
其中一部分登陆是登陆新大陆云平台的方法,因为底层硬件获取的数据都是上传到云平台的,但是为了方便看效果,后面均以获取随机数据为基准。
其中的 SetTitle();方法和Welcome界面一样,添加一个back键,但是触发事件不一样,这个的功能是返回到Welcome界面。然后就是小眼睛那部分代码,实际就是给ImagevIEw注册一个点击事件,然后点击之后换一张图片,然后利用EditText的属性将密码隐藏和显示,新大陆那一块可以省略,然后点击登陆跳转到导航界面。

//隐藏密码 PassWord.settransformationMethod(PasswordtransformationMethod.getInstance()); //显示密码 PassWord.settransformationMethod(HIDeReturnstransformationMethod.getInstance());
public class Login extends AppCompatActivity {    private button Login;    private EditText Username,PassWord;    private ImageVIEw NotSeePassWord;    private boolean Smalleye = true;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_login2);        InitVIEw();        OnClick();        SetTitle();       NotSeePassWord.setimageResource(R.drawable.closeeye);//设置初始化密码为不可见图片    }    private voID InitVIEw(){        Login = findVIEwByID(R.ID.login);        Username = findVIEwByID(R.ID.username);        PassWord = findVIEwByID(R.ID.password);       // SeeThePassWord = findVIEwByID(R.ID.seethepassword);        NotSeePassWord = findVIEwByID(R.ID.notseethepassword);    }    private voID OnClick(){        OnClick onClick = new OnClick();        Login.setonClickListener(onClick);        //SeeThePassWord.setonClickListener(onClick);        NotSeePassWord.setonClickListener(onClick);    }    private class OnClick implements VIEw.OnClickListener{        @OverrIDe        public voID onClick(VIEw v) {            switch (v.getID()){                case R.ID.login:                    SignIn();                   break;                case R.ID.notseethepassword:                    if (Smalleye == true)                    {                        SeeThePassWordMethod();                        Smalleye = !Smalleye;                    }else {                        NotSeeThePassWordMethod();                        Smalleye = !Smalleye;                    }                    break;            }        }    }    private voID NotSeeThePassWordMethod(){        NotSeePassWord.setimageResource(R.drawable.closeeye);        PassWord.settransformationMethod(PasswordtransformationMethod.getInstance());    }    private voID SeeThePassWordMethod(){        NotSeePassWord.setimageResource(R.drawable.openeye);      PassWord.settransformationMethod(HIDeReturnstransformationMethod.getInstance());    }    private voID SignIn(){        String platformAddress = NewloadParameter.IP_DEFAulT_VALUE;  //网址加端口号        String LoginUsername = Username.getText().toString();        String LoginPassWord = PassWord.getText().toString();        if (TextUtils.isEmpty(platformAddress)){            Toast.makeText(this,"请配置新大陆云平台信息",Toast.LENGTH_SHORT).show();            return;        }        if (TextUtils.isEmpty(LoginUsername) || TextUtils.isEmpty(LoginPassWord)){            Toast.makeText(this,"账号或者密码不能为空",Toast.LENGTH_SHORT).show();            return;        }        if (!LoginPassWord.equals("123456")){            Toast.makeText(this,"账号或者密码错误",Toast.LENGTH_SHORT).show();            return;        }        if (!LoginUsername.equals("admin")){            Toast.makeText(this,"账号或者密码错误",Toast.LENGTH_SHORT).show();            return;        }        if (LoginPassWord.equals("123456") && LoginUsername.equals("admin")){            Intent intent = new Intent(Login.this,ChooseInterface.class);            startActivity(intent);        }        NetWorkBusiness netWorkBusiness = new NetWorkBusiness("",platformAddress);        netWorkBusiness.signIn(new SignIn(LoginUsername, LoginPassWord), new NCallBack<BaseResponseEntity<User>>(getApplicationContext()) {            @OverrIDe            protected voID onResponse(BaseResponseEntity<User> response) {            }            @OverrIDe            public voID onResponse(Call<BaseResponseEntity<User>> call, Response<BaseResponseEntity<User>> response) {                super.onResponse(call, response);                BaseResponseEntity<User> baseResponseEntity = response.body(); //获取请求                if (baseResponseEntity != null){                    //获取访问令牌                    String accestoken = baseResponseEntity.getResultObj().getAccesstoken();                    Intent intent = new Intent(Login.this,ChooseInterface.class);                    Bundle bundle = new Bundle();                    bundle.putString("accestoken",accestoken);                    intent.putExtras(bundle); // 传递令牌                    startActivity(intent);                    finish();                }            }        });  }    private voID SetTitle() {        Actionbar actionbar = getSupportActionbar();        if (actionbar != null) {            actionbar.setdisplayHomeAsUpEnabled(true);            actionbar.setHomebuttonEnabled(true);        }    }    @OverrIDe    public boolean onoptionsItemSelected(@NonNull MenuItem item) {        switch (item.getItemID()){            case androID.R.ID.home:                Intent intent = new Intent(Login.this, Welcome.class);                startActivity(intent);                break;        }        return super.onoptionsItemSelected(item);    }}
导航界面

效果图如下:


代码较为简单,并不复杂,同样是使用自定义Drawable优化界面,然后一个搜索栏,这个搜索栏也是比较low的,并没有去自定义view,而是搜索那几个关键字,然后给那个图片这个一个点击事件,然后进行页面跳转。
布局文件代码如下:

<relativeLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"xmlns:ndroID="http://schemas.androID.com/apk/res-auto"tools:context=".ChooseInterface"><!--返回按钮<button    androID:ID="@+ID/back"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:visibility="gone"/>--><!--温度导航--><!--搜索栏--><EditText    androID:ID="@+ID/Searchbar"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:background="@drawable/searchBox"    androID:layout_margintop="20dp"    androID:layout_marginleft="10dp"    androID:hint="搜索"    androID:gravity="center"    /><ImageVIEw    androID:ID="@+ID/SearchImage"    androID:layout_wIDth="35dp"    androID:layout_height="35dp"    androID:src="@drawable/search"    androID:layout_margintop="20dp"    androID:layout_marginleft="30dp"/>    <GrIDLayout        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:columnCount="3"        androID:rowCount="3"        androID:layout_margintop="80dp"><linearLayout    androID:ID="@+ID/TmpBox"    androID:layout_wIDth="150dp"    androID:layout_height="160dp"    androID:background="@drawable/tmpBox"    androID:layout_marginleft="20dp"    androID:layout_margintop="20dp"    androID:orIEntation="vertical"    androID:layout_row="0"    androID:layout_column="0">    <ImageVIEw        androID:layout_wIDth="70dp"        androID:layout_height="70dp"        androID:src="@drawable/tmp"        androID:layout_margintop="10dp"        androID:layout_marginleft="40dp"/>    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="温度"        androID:layout_margintop="20dp"        androID:layout_marginleft="55dp"        androID:textSize="20sp"        androID:textcolor="#B22222"/>    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginleft="45dp"        androID:layout_margintop="10dp"        androID:text="空调冷暖气设置"        androID:textcolor="#B22222"        androID:textSize="10sp" /></linearLayout><!--湿度导航--><linearLayout    androID:ID="@+ID/HumBox"    androID:layout_wIDth="150dp"    androID:layout_height="160dp"    androID:background="@drawable/humBox"    androID:orIEntation="vertical"    androID:layout_marginleft="30dp"    androID:layout_margintop="20dp"    androID:layout_row="0"    androID:layout_column="1">    <ImageVIEw        androID:layout_wIDth="70dp"        androID:layout_height="70dp"        androID:src="@drawable/hum"        androID:layout_marginleft="40dp"        androID:layout_margintop="10dp"/>    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="湿度"        androID:layout_margintop="20dp"        androID:layout_marginleft="55dp"        androID:textSize="20sp"        androID:textcolor="#B22222"/>    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="有关天气情况"        androID:layout_margintop="10dp"        androID:layout_marginleft="45dp"        androID:textSize="10sp"        androID:textcolor="#B22222" /></linearLayout><!--烟雾导航-->        <!--红外传感器导航-->        <linearLayout            androID:ID="@+ID/SmokeBox"            androID:layout_wIDth="150dp"            androID:layout_height="160dp"            androID:layout_row="1"            androID:layout_column="0"            androID:layout_below="@+ID/TmpBox"            androID:layout_marginleft="20dp"            androID:layout_margintop="30dp"            androID:background="@drawable/smokeBox"            androID:orIEntation="vertical">            <ImageVIEw                androID:layout_wIDth="70dp"                androID:layout_height="70dp"                androID:layout_marginleft="40dp"                androID:layout_margintop="15dp"                androID:src="@drawable/yanwu" />            <TextVIEw                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_marginleft="55dp"                androID:layout_margintop="20dp"                androID:text="烟雾"                androID:textcolor="#000000"                androID:textSize="20sp" />            <TextVIEw                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:layout_marginleft="45dp"                androID:layout_margintop="10dp"                androID:text="查看烟雾传感器"                androID:textcolor="#000000"                androID:textSize="10sp" />        </linearLayout>        <linearLayout    androID:ID="@+ID/InfraredBox"    androID:layout_wIDth="150dp"    androID:layout_height="160dp"    androID:background="@drawable/infraredBox"    androID:layout_below="@+ID/HumBox"    androID:orIEntation="vertical"    androID:layout_marginleft="30dp"    androID:layout_margintop="30dp"    androID:layout_row="1"    androID:layout_column="1">    <ImageVIEw        androID:layout_wIDth="70dp"        androID:layout_height="70dp"        androID:src="@drawable/hongwai"        androID:layout_marginleft="40dp"        androID:layout_margintop="15dp"/>    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="红外"        androID:layout_margintop="20dp"        androID:layout_marginleft="55dp"        androID:textSize="20sp"        androID:textcolor="#ffffff"/>    <TextVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="查看红外传感器"        androID:layout_margintop="10dp"        androID:layout_marginleft="45dp"        androID:textSize="10sp"        androID:textcolor="#ffffff" /></linearLayout>    </GrIDLayout></relativeLayout>

看一下java部分代码:
SetTitle();和前面一样,此处省略,这个比较简单,就是几个页面的跳转,此处也省略。

public class ChooseInterface extends AppCompatActivity {    private linearLayout TmpVIEw,HumVIEw,SmokeVIEw,InfraredVIEw;    private EditText Searchbar;    private String SearchbarContent;    private ImageVIEw SearchImage;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_choose_interface);        InitVIEw();        setListener();        SearchContent();        SetTitle();    }    private voID SetTitle(){        Actionbar actionbar = getSupportActionbar();        if (actionbar != null){            actionbar.setdisplayHomeAsUpEnabled(true);            actionbar.setHomebuttonEnabled(true);        }    }    @OverrIDe    public boolean onoptionsItemSelected(@NonNull MenuItem item) {        switch (item.getItemID()){            case androID.R.ID.home:               Intent intent = new Intent(ChooseInterface.this,Login.class);               startActivity(intent);                break;        }        return super.onoptionsItemSelected(item);    }    private class OnClick implements VIEw.OnClickListener{        @OverrIDe        public voID onClick(VIEw v) {            Intent intent = null;            switch (v.getID()){                case R.ID.TmpBox:                    intent = new Intent(ChooseInterface.this,TmpInterface.class);                    break;                case R.ID.HumBox:                    intent = new Intent(ChooseInterface.this,HumInterface.class);                    break;                case R.ID.SmokeBox:                    intent = new Intent(ChooseInterface.this,MainActivity.class);                    break;                case R.ID.InfraredBox:                    intent = new Intent(ChooseInterface.this,InfraredInterface.class);                    break;            }            startActivity(intent);        }    }    private voID setListener(){        OnClick onClick = new OnClick();        TmpVIEw.setonClickListener(onClick);        HumVIEw.setonClickListener(onClick);        SmokeVIEw.setonClickListener(onClick);        InfraredVIEw.setonClickListener(onClick);    }    private voID InitVIEw(){        TmpVIEw = findVIEwByID(R.ID.TmpBox);        HumVIEw = findVIEwByID(R.ID.HumBox);        SmokeVIEw = findVIEwByID(R.ID.SmokeBox);        InfraredVIEw = findVIEwByID(R.ID.InfraredBox);        Searchbar = findVIEwByID(R.ID.Searchbar);        SearchImage = findVIEwByID(R.ID.SearchImage);    }    private voID SearchContent() {        SearchImage.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Intent intent = null;                SearchbarContent = Searchbar.getText().toString().trim();                switch (SearchbarContent){                    case "温度":                        intent = new Intent(ChooseInterface.this,TmpInterface.class);                        break;                    case "湿度":                        intent = new Intent(ChooseInterface.this,HumInterface.class);                        break;                    case "烟雾":                        intent = new Intent(ChooseInterface.this,MainActivity.class);                        break;                    case "红外":                        intent = new Intent(ChooseInterface.this,InfraredInterface.class);                        break;                }                startActivity(intent);            }        });    }}
温度界面

先看一下效果:
两个Switch,一个控制风扇的旋转,一个控制灯泡的亮和灭,下面是一个Seekbar,下面那个Seekbar是自定义过的,和Switch一样改一下滑块和背景,根据滑动的大小,表示一个温度值,温度大小控制风扇和灯,风扇的旋转是采用动画,在java部分详细介绍。


看一下java部分:
其余比较简单,我们就看一下InitAnimation();部分:
逐句解释:
第一句获取资源文件,有两个参数,第一个是Context上下文,第二个是图片要变化的文件,一般创建一个anim包,然后里面存放一些文件,比如我这里建了一个rotate文件,里面主要写了从那个角度开始旋转,旋转模式等
第二句:设置持续时间
第三句:设置重复模式(“restart” =从头开始 或者 “reverse”=从末尾开始)
第四句:是否当旋转完之后,继续从当前方向旋转
下面为插值器,因为我要的效果并不是很好,所以并没有使用。

 private voID InitAnimation(){        animation = AnimationUtils.loadAnimation(TmpInterface.this,R.anim.rotate);        animation.setDuration(1000);        //animation.setRepeatCount(-1); //无限旋转        animation.setRepeatMode(1);        animation.setFillAfter(true);//            CycleInterpolator interpolator = new CycleInterpolator(1);//            animation.setInterpolator(interpolator);    }
public class TmpInterface extends AppCompatActivity {    private TextVIEw TmpValue;    private Seekbar seekbar;    private Switch ControlFan,ControlLamp;    private ImageVIEw Fan,CloseLamp,OpenLamp;    private Animation animation;    private static int size;    private button Back;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_tmp_interface);        SetTitle();        InitVIEw();        InitAnimation();        seekbar.setMax(50);//设置最大值        seekbar.setProgress(0);//设置当前值        seekbar.setonSeekbarchangelistener(new Seekbar.OnSeekbarchangelistener() {            @OverrIDe            public voID onProgressChanged(Seekbar seekbar, int progress, boolean fromUser) {                 size = progress;                TmpValue.setText("温度: "+size+"℃");                if (size > 30){                    Fan.startAnimation(animation);                }else {                    Fan.clearanimation();                }                if (size < 15){                    OpenLamp.setVisibility(VIEw.VISIBLE);                    CloseLamp.setVisibility(VIEw.INVISIBLE);                }else {                    OpenLamp.setVisibility(VIEw.INVISIBLE);                    CloseLamp.setVisibility(VIEw.VISIBLE);                }            }            @OverrIDe            public voID onStartTrackingtouch(Seekbar seekbar) {            }            @OverrIDe            public voID onStopTrackingtouch(Seekbar seekbar) {            }        });        ControlFan.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) {                if (ControlFan.isChecked()){                    Fan.startAnimation(animation);                }else {                    Fan.clearanimation();                }            }        });        ControlLamp.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) {                if (ControlLamp.isChecked()){                    OpenLamp.setVisibility(VIEw.VISIBLE);                    CloseLamp.setVisibility(VIEw.INVISIBLE);                }else {                    OpenLamp.setVisibility(VIEw.INVISIBLE);                    CloseLamp.setVisibility(VIEw.VISIBLE);                }            }        });    }    //设置标题栏返回按钮    private voID SetTitle(){        Actionbar actionbar = getSupportActionbar();        if (actionbar != null){            actionbar.setdisplayHomeAsUpEnabled(true);            actionbar.setHomebuttonEnabled(true);        }    }    private voID InitVIEw(){        seekbar = findVIEwByID(R.ID.CustomSeekbar);        TmpValue = findVIEwByID(R.ID.TmpText);        ControlFan = findVIEwByID(R.ID.FansSwitch);        ControlLamp = findVIEwByID(R.ID.LampSwitch);        CloseLamp = findVIEwByID(R.ID.ShutLamp);        OpenLamp = findVIEwByID(R.ID.OpenLamp);        Fan = findVIEwByID(R.ID.fans);        Back = findVIEwByID(R.ID.back);    }    private voID InitAnimation(){        animation = AnimationUtils.loadAnimation(TmpInterface.this,R.anim.rotate);        animation.setDuration(1000);        //animation.setRepeatCount(-1); //无限旋转        animation.setRepeatMode(1);        animation.setFillAfter(true);//            CycleInterpolator interpolator = new CycleInterpolator(1);//            animation.setInterpolator(interpolator);    }//    @OverrIDe//    public boolean onCreateOptionsMenu(Menu menu) {//        MenuInflater inflater = getMenuInflater();//        inflater.inflate(R.menu.test_menu, menu);//        return super.onCreateOptionsMenu(menu);//    }    //对Menu中菜单中子项进行控制    @OverrIDe    public boolean onoptionsItemSelected(@NonNull MenuItem item) {        switch (item.getItemID()){            case androID.R.ID.home:                Intent intent = new Intent(TmpInterface.this,ChooseInterface.class);                startActivity(intent);                break;        }        return super.onoptionsItemSelected(item);    }}
湿度界面

效果如下:
此界面比较简单,一个RecyclerVIEw控件,然后一个button跳转到地图界面,关于地图界面请参考前面文章百度地图跳转链接


看一下布局文件代码:

<linearLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="vertical"    tools:context=".HumInterface"    androID:background="@drawable/background8"    >    <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:orIEntation="horizontal"        androID:layout_margintop="20dp">        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="今天当前:"            androID:layout_marginleft="10dp"            androID:textSize="17sp"/>        <!--显示天气,例如下雨,开太阳-->        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="阵雨"            androID:textSize="17sp"/>        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="气温:"            androID:textSize="17sp"/>        <!--显示当前气温-->        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="9℃"            androID:textSize="17sp"/>        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="预计今天最高气温:"            androID:textSize="17sp"/>        <!--显示今天最高气温-->        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="12℃"            androID:textSize="17sp"/></linearLayout>    <VIEw        androID:layout_wIDth="match_parent"        androID:layout_height="1dp"        androID:background="#000000"        androID:layout_margintop="10dp"/>    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:orIEntation="horizontal">        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="空气质量"            androID:layout_margintop="10dp"            androID:layout_marginleft="40dp"/>        <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="AQI(CN)"            androID:layout_marginleft="200dp"            androID:layout_margintop="10dp"            />    </linearLayout>    <VIEw        androID:layout_wIDth="match_parent"        androID:layout_height="1dp"        androID:background="#000000"        androID:layout_margintop="10dp"/>        <linearLayout            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:orIEntation="horizontal">            <TextVIEw                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:text="22"                androID:layout_marginleft="10dp"                androID:layout_margintop="10dp"                androID:textSize="25sp"/>            <TextVIEw                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:text="——"                androID:layout_marginleft="10dp"                androID:layout_margintop="10dp"                androID:textSize="20sp"/>            <TextVIEw                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:text="优"                androID:layout_marginleft="10dp"                androID:layout_margintop="10dp"                androID:textSize="25sp"                />    </linearLayout>    <VIEw        androID:layout_wIDth="match_parent"        androID:layout_height="1dp"        androID:background="#000000"        androID:layout_margintop="10dp"/>        <linearLayout            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:orIEntation="horizontal"            androID:layout_margintop="10dp">           <TextVIEw               androID:layout_wIDth="wrap_content"               androID:layout_height="wrap_content"               androID:text="株洲市的监测站读数。"               androID:layout_marginleft="20dp"               androID:textSize="17sp"               />            <TextVIEw                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:text="上次更新:1小时之内"/>         </linearLayout>    <VIEw        androID:layout_wIDth="match_parent"        androID:layout_height="1dp"        androID:background="#000000"        androID:layout_margintop="10dp"/>    <relativeLayout        androID:layout_wIDth="match_parent"        androID:layout_height="400dp"        androID:layout_margintop="0dp">    <androIDx.recyclervIEw.Widget.RecyclerVIEw        androID:ID="@+ID/MyRecycler"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        />    </relativeLayout>    <button        androID:ID="@+ID/ToMap"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="点击此处:在地图中打开"        androID:textSize="20sp"        androID:background="#00000000"        androID:layout_gravity="center" /></linearLayout>

java部分:

public class HumInterface extends AppCompatActivity {    private RecyclerVIEw MyRecycler;    private MyRecyclerVIEw Adapter;    private button ToMap;    private List<WeatherData> List = new ArrayList<>();    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_hum_interface);        MyRecycler = findVIEwByID(R.ID.MyRecycler);        ToMap = findVIEwByID(R.ID.ToMap);        linearlayoutmanager manager = new linearlayoutmanager(HumInterface.this);        MyRecycler.setLayoutManager(manager);        Adapter = new MyRecyclerVIEw(List);        MyRecycler.setAdapter(Adapter);        ListData();        SetTitle();        ToMap.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                Intent intent = new Intent(HumInterface.this,Map.class);                startActivity(intent);            }        });    }    private voID ListData(){        String[] GetWeatherData = {"日出","日落","06:31","18:38","降雨概率","湿度","50%","96%","风向","体感温度","北","6℃","降雨量","气压","5.8mm","1016百帕","能见度","紫外线指数","8.1Km","0"};        for (int i = 0; i < 10 ; i=i+2) {            for(int j = 0; j < 1; j++){                WeatherData data = new WeatherData(GetWeatherData[i],GetWeatherData[i+1]);                List.add(data);            }        }    }    //对标题栏返回按钮进行显示    private voID SetTitle(){        Actionbar actionbar = getSupportActionbar();        if (actionbar != null){            actionbar.setdisplayHomeAsUpEnabled(true);            actionbar.setHomebuttonEnabled(true);        }    }    //对返回按钮进行 *** 作    @OverrIDe    public boolean onoptionsItemSelected(@NonNull MenuItem item) {        switch (item.getItemID()){            case androID.R.ID.home:                Intent intent = new Intent(HumInterface.this,ChooseInterface.class);                startActivity(intent);                break;        }        return super.onoptionsItemSelected(item);    }}
烟雾传感器界面

此界面与上面的界面雷同,因为实在不知道构造一个什么界面了,数据均为随机获取


看一下布局代码:

<ScrollVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"    androID:background="@drawable/background5"><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context=".MainActivity">    <TextVIEw        androID:ID="@+ID/Title"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_gravity="center"        androID:gravity="center"        androID:text="智能家居"        androID:textSize="30sp"        androID:layout_margintop="10dp"        androID:textcolor="#000000"        />    <VIEw        androID:layout_margintop="50dp"        androID:layout_wIDth="400dp"        androID:layout_height="1dp"             androID:background="#000000"        />    <linearLayout        androID:ID="@+ID/Layout_1"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:orIEntation="horizontal"        androID:layout_margintop="10dp">        <ImageVIEw            androID:ID="@+ID/Online"            androID:layout_wIDth="60dp"            androID:layout_height="60dp"            androID:src="@drawable/notonline"            androID:layout_margintop="55dp"            androID:layout_marginleft="15dp"/>        <!--        <TextVIEw            androID:ID="@+ID/Whetheronline"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="是否在线:"            androID:textSize="25dp"            androID:layout_margintop="70dp"            androID:layout_marginleft="20dp"            androID:enabled="false"            androID:textcolor="#000"/>-->        <TextVIEw            androID:ID="@+ID/Status"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="状态  :"            androID:textSize="20dp"            androID:layout_margintop="70dp"            androID:layout_marginleft="30dp"            androID:enabled="false"            androID:textcolor="#000"/>        <TextVIEw            androID:ID="@+ID/Whetheronline"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:text="离线"            androID:textSize="20dp"            androID:layout_margintop="70dp"            androID:layout_marginleft="30dp"            androID:enabled="false"            androID:textcolor="#C2C2C2"/>        <Imagebutton            androID:ID="@+ID/bindNetWork"            androID:layout_wIDth="60dp"            androID:layout_height="50dp"            androID:src="@drawable/bingonline"            androID:layout_margintop="60dp"            androID:layout_marginleft="30dp"            androID:background="#00000000"            androID:scaleType="fitCenter" />    </linearLayout>    <ImageVIEw        androID:ID="@+ID/NotOnline"        androID:layout_wIDth="80dp"        androID:layout_height="100dp"        androID:src="@drawable/online"        androID:layout_margintop="45dp"        androID:layout_marginleft="15dp"        androID:visibility="invisible"/><linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="horizontal"    androID:layout_margintop="110dp"> <ImageVIEw     androID:ID="@+ID/tmp"     androID:layout_wIDth="60dp"     androID:layout_height="70dp"     androID:src="@drawable/tmp"     androID:layout_marginleft="15dp"     androID:layout_margintop="20dp"/>    <TextVIEw        androID:ID="@+ID/tmp_text"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_margintop="40dp"        androID:enabled="false"        androID:text="温度:"        androID:textcolor="#000"        androID:textSize="20dp" />    <TextVIEw        androID:ID="@+ID/TmpValue"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="20"        androID:textSize="20dp"        androID:layout_margintop="40dp"        androID:textcolor="#ff0000"/>    <!--    <TextVIEw        androID:ID="@+ID/sheshIDu"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text=""        androID:textSize="20dp"        androID:layout_margintop="28dp"        androID:textcolor="#000"        androID:layout_marginleft="5dp"/>-->    <!---->    <ImageVIEw        androID:ID="@+ID/hum"        androID:layout_wIDth="60dp"        androID:layout_height="60dp"        androID:src="@drawable/hum"        androID:layout_marginleft="10dp"        androID:layout_margintop="20dp"/>    <TextVIEw        androID:ID="@+ID/hum_text"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="湿度:"        androID:textSize="20dp"        androID:layout_margintop="40dp"        androID:enabled="false"        androID:textcolor="#000"/>    <TextVIEw        androID:ID="@+ID/HumValue"        androID:layout_wIDth="80dp"        androID:layout_height="50dp"        androID:text="25"        androID:textSize="20dp"        androID:layout_margintop="40dp"        androID:textcolor="#ff0000"/>    <!--    <TextVIEw        androID:ID="@+ID/RH"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="φ"        androID:textSize="20dp"        androID:layout_margintop="30dp"        androID:textcolor="#000"        androID:layout_marginleft="5dp"/>--></linearLayout><linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:layout_margintop="200dp">    <ImageVIEw        androID:ID="@+ID/yanwu"        androID:layout_wIDth="70dp"        androID:layout_height="70dp"        androID:src="@drawable/yanwu"        androID:layout_marginleft="20dp"        androID:layout_margintop="20dp"/>    <TextVIEw        androID:ID="@+ID/yanwu_text"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="烟雾:"        androID:textSize="20dp"        androID:layout_margintop="40dp"        androID:enabled="false"        androID:textcolor="#000"        androID:layout_marginleft="5dp"/>    <TextVIEw        androID:ID="@+ID/Smoketotal"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="20"        androID:textSize="20dp"        androID:layout_margintop="40dp"        androID:textcolor="#ff0000"/>    <!---->    <ImageVIEw        androID:ID="@+ID/hongai"        androID:layout_wIDth="60dp"        androID:layout_height="60dp"        androID:src="@drawable/hongwai"        androID:layout_marginleft="25dp"        androID:layout_margintop="20dp"/>    <TextVIEw        androID:ID="@+ID/hongwai_text"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="红外:"        androID:textSize="20dp"        androID:layout_margintop="40dp"        androID:enabled="false"        androID:textcolor="#000"        androID:layout_marginleft="5dp"/>    <TextVIEw        androID:ID="@+ID/InfraredTotal"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:text="2"        androID:textSize="20dp"        androID:layout_margintop="40dp"        androID:textcolor="#ff0000"/></linearLayout>    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_margintop="300dp"        androID:orIEntation="horizontal"        >        <ImageVIEw            androID:ID="@+ID/tmpMax"            androID:layout_wIDth="70dp"            androID:layout_height="70dp"            androID:src="@drawable/tmp_up"            androID:layout_margintop="20dp"            androID:layout_marginleft="25dp"/>        <TextVIEw            androID:ID="@+ID/MaxSeekbarText"            androID:layout_wIDth="50dp"            androID:layout_height="wrap_content"            androID:text="0"            androID:textSize="25sp"            androID:textcolor="#ff0000"            androID:layout_margintop="40dp"            androID:layout_marginleft="20dp"/>        <Seekbar            androID:ID="@+ID/tmpMax_Seekbar"            androID:layout_wIDth="200dp"            androID:layout_height="50dp"            androID:layout_margintop="40dp"            androID:layout_marginleft="20dp"            androID:max="50"            androID:progress="0"/>    </linearLayout>    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_margintop="400dp"        androID:orIEntation="horizontal"        >    <ImageVIEw        androID:ID="@+ID/tmpMin"        androID:layout_wIDth="70dp"        androID:layout_height="70dp"        androID:src="@drawable/tmp_down"        androID:layout_margintop="20dp"        androID:layout_marginleft="25dp"/>        <TextVIEw            androID:ID="@+ID/MinSeekbarText"            androID:layout_wIDth="50dp"            androID:layout_height="wrap_content"            androID:text="0"            androID:textSize="25sp"            androID:textcolor="#ff0000"            androID:layout_margintop="40dp"            androID:layout_marginleft="20dp"/>    <Seekbar        androID:ID="@+ID/tmpMIN_Seekbar"        androID:layout_wIDth="200dp"        androID:layout_height="50dp"        androID:layout_margintop="40dp"        androID:layout_marginleft="20dp"        androID:max="50"        androID:progress="0"/>    </linearLayout> <linearLayout     androID:layout_margintop="500dp"     androID:layout_wIDth="match_parent"     androID:layout_height="match_parent"     >     <ImageVIEw         androID:ID="@+ID/ShutLamp"         androID:layout_wIDth="100dp"         androID:layout_height="100dp"         androID:src="@drawable/mIE"         androID:layout_marginleft="20dp"         androID:visibility="visible"/>     <Switch         androID:ID="@+ID/LampSwitch"         androID:layout_wIDth="200dp"         androID:layout_height="102dp"         androID:thumb="@drawable/thumb"         androID:track="@drawable/track" /> </linearLayout>    <ImageVIEw        androID:ID="@+ID/OpenLamp"        androID:layout_wIDth="100dp"        androID:layout_height="100dp"        androID:src="@drawable/dengliang"        androID:layout_marginRight="50dp"        androID:layout_margintop="500dp"        androID:layout_marginleft="20dp"        androID:visibility="invisible"        />    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:layout_margintop="600dp">    <ImageVIEw        androID:ID="@+ID/fans"        androID:layout_wIDth="100dp"        androID:layout_height="100dp"        androID:src="@drawable/fans"        androID:layout_marginleft="20dp"        androID:layout_margintop="10dp"/>        <Switch            androID:ID="@+ID/FansSwitch"            androID:layout_wIDth="200dp"            androID:layout_height="102dp"            androID:thumb="@drawable/thumb"            androID:track="@drawable/track" />    </linearLayout></relativeLayout></ScrollVIEw>

java部分:
讲一下灯泡灭和亮那部分,其实就是在布局的时候两张图片放在同一个位置,一开始亮的图片先设置为不可见,然后通过滑动Switch判断是否切换图片,切换图片的过程就是,设置两张图片的setVisibility属性。

   ShutLamp.setVisibility(VIEw.INVISIBLE);   OpenLamp.setVisibility(VIEw.VISIBLE);
public class MainActivity extends AppCompatActivity {    private Switch LampSwitch,FansSwitch;    private ImageVIEw OpenLamp,ShutLamp,Fans;    private Seekbar MaxSeekbar,MinSeekbar;    private TextVIEw MaxTmpValue,MinTmpValue,TmpValue,HumVale,Smoketotal,InfraredTotal;    private Animation animation;    private int MaxValue;    private int MinValue;    private static  int RandomInfrared;    private static  int RandomSmoke;    private static  int RandomHumValue;    private static int RandomMaxTmpValue ;  //-100-100   // private  static int RandomMinTmpValue = 20 - (int)(Math.random()*101);       //-80 --- 0    //private static Boolean BooleanFans = false;  //用于判断是否开启风扇    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        InitVIEw();        InitAnimation();        //FirstDefaultExecute();        Delay();        SetTitle();        //灯的开关        LampSwitch.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) {                if (LampSwitch.isChecked())                {                  ShutLamp.setVisibility(VIEw.INVISIBLE);                  OpenLamp.setVisibility(VIEw.VISIBLE);                // OpenLamp.setimageDrawable(getResources().getDrawable(R.drawable.dengliang));                }else{                    //灭                    OpenLamp.setVisibility(VIEw.INVISIBLE);                    ShutLamp.setVisibility(VIEw.VISIBLE);                   // ShutLamp.setBackgroundDrawable(getResources().getDrawable(R.drawable.mIE));                }            }        });        //风扇开关        FansSwitch.setonCheckedchangelistener(new Compoundbutton.OnCheckedchangelistener() {            @OverrIDe            public voID onCheckedChanged(Compoundbutton buttonVIEw, boolean isChecked) {                if (FansSwitch.isChecked()){                    Fans.startAnimation(animation);                }else {  Fans.clearanimation();                }            }        });        //温度最大值滑动条        MaxSeekbar.setonSeekbarchangelistener(new Seekbar.OnSeekbarchangelistener() {            @OverrIDe            public voID onProgressChanged(Seekbar seekbar, int progress, boolean fromUser) {                MaxValue = progress;                MaxTmpValue.setText(MaxValue+"");                Delay();                if (RandomMaxTmpValue > MaxValue){                    Fans.startAnimation(animation);                    FansSwitch.setChecked(true);                    ShutLamp.setVisibility(VIEw.VISIBLE);                    OpenLamp.setVisibility(VIEw.INVISIBLE);                    LampSwitch.setChecked(false);                }else if (RandomMaxTmpValue < MinValue){                    Fans.clearanimation();                    FansSwitch.setChecked(false);                    ShutLamp.setVisibility(VIEw.INVISIBLE);                    OpenLamp.setVisibility(VIEw.VISIBLE);                    LampSwitch.setChecked(true);                }            }            @OverrIDe            public voID onStartTrackingtouch(Seekbar seekbar) {            }            @OverrIDe            public voID onStopTrackingtouch(Seekbar seekbar) {            }        });        //温度最小值滑动条        MinSeekbar.setonSeekbarchangelistener(new Seekbar.OnSeekbarchangelistener() {            @OverrIDe            public voID onProgressChanged(Seekbar seekbar, int progress, boolean fromUser) {                MinValue = progress * (-1);                MinTmpValue.setText(MinValue+"");                Delay();                if (RandomMaxTmpValue < MinValue ){                    Fans.clearanimation();                    FansSwitch.setChecked(false);                    ShutLamp.setVisibility(VIEw.INVISIBLE);                    OpenLamp.setVisibility(VIEw.VISIBLE);                    LampSwitch.setChecked(true);                }else if (RandomMaxTmpValue > MaxValue){                    Fans.startAnimation(animation);                    FansSwitch.setChecked(true);                    ShutLamp.setVisibility(VIEw.VISIBLE);                    OpenLamp.setVisibility(VIEw.INVISIBLE);                    LampSwitch.setChecked(false);                }            }            @OverrIDe            public voID onStartTrackingtouch(Seekbar seekbar) {            }            @OverrIDe            public voID onStopTrackingtouch(Seekbar seekbar) {            }        });//        //String StringTmpValue = TmpValue.getText().toString();  //当前温度值 -- string//        String StringMinTmpValue = MinTmpValue.getText().toString();  //温度下限值//        String StringMaxTmpValue = MaxTmpValue.getText().toString();  //温度上限值//       // int StringToIntTmpValue = Integer.parseInt(StringTmpValue);   //当前温度值--int//        int StringToIntTmpMinValue = Integer.parseInt(StringMinTmpValue); //温度下限值//        int StringToIntTmpMaxValue = Integer.parseInt(StringMaxTmpValue);  //温度上限值//        //设获取温度区间位0---100之内的随机数//      //  int RandomTmpValue = (int)(Math.random()*(100+1)); //随机获取当前温度值//        TmpValue.setText(RandomTmpValue+"");//        //当前大于额定最高温度,开启风扇//        if (RandomTmpValue > StringToIntTmpMaxValue){//            Fans.startAnimation(animation);//            FansSwitch.setChecked(true);//        }//        //当前大于额定最小温度,开启灯泡//        if (RandomTmpValue < StringToIntTmpMinValue) {//            Fans.clearanimation();//            FansSwitch.setChecked(false);//            ShutLamp.setVisibility(VIEw.INVISIBLE);//            OpenLamp.setVisibility(VIEw.VISIBLE);//            LampSwitch.setChecked(true);//        }    }    private voID InitVIEw(){        LampSwitch = findVIEwByID(R.ID.LampSwitch);  //灯泡开关按钮        FansSwitch = findVIEwByID(R.ID.FansSwitch);  //风扇开关按钮        OpenLamp = findVIEwByID(R.ID.OpenLamp);      //灯开的图片        ShutLamp = findVIEwByID(R.ID.ShutLamp);      //灯关的图片        MaxSeekbar = findVIEwByID(R.ID.tmpMax_Seekbar);  //滑动条温度的上限值        MinSeekbar = findVIEwByID(R.ID.tmpMIN_Seekbar);  //滑动条温度的下限值        MaxTmpValue = findVIEwByID(R.ID.MaxSeekbarText);   //温度的上限值        MinTmpValue = findVIEwByID(R.ID.MinSeekbarText);  //温度的下限值        Fans = findVIEwByID(R.ID.fans);     //风扇图片        TmpValue = findVIEwByID(R.ID.TmpValue);  //当前温度值        HumVale = findVIEwByID(R.ID.HumValue);   //当前湿度值        Smoketotal = findVIEwByID(R.ID.Smoketotal);  //烟雾传感器总数        InfraredTotal = findVIEwByID(R.ID.InfraredTotal); //红外传感器总数    }//      //开启风扇//       private  voID StartRotateFans(){//       Fans.startAnimation(animation);////        }//        //关闭风扇//        private voID EndRotateFans(){//           Fans.clearanimation();////        }        private voID InitAnimation(){            animation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate);            animation.setDuration(1000);            //animation.setRepeatCount(-1); //无限旋转            animation.setRepeatMode(1);            animation.setFillAfter(true);//            CycleInterpolator interpolator = new CycleInterpolator(1);//            animation.setInterpolator(interpolator);        }        private voID Delay(){        new Handler().postDelayed(new Runnable() {            @OverrIDe            public voID run() {                RandomMaxTmpValue = 50- (int)(Math.random()*(100+1));                RandomHumValue = (int) (Math.random()*100+1);                RandomSmoke = (int) (Math.random()*11);                RandomInfrared = (int) (Math.random()*11);                TmpValue.setText(RandomMaxTmpValue+"℃");                HumVale.setText(RandomHumValue+"%");                Smoketotal.setText(RandomSmoke+"");                InfraredTotal.setText(RandomInfrared+"");            }        },10); ///每隔0.01秒重新获取随机温度值    }    //第一次允许程序,判断温度值是大于0还是小于;    private voID FirstDefaultExecute(){        RandomMaxTmpValue = 50- (int)(Math.random()*(100+1)); //获取当前温度值        //开启风扇        if (RandomMaxTmpValue > 0 ){            Fans.startAnimation(animation);            FansSwitch.setChecked(true);            ShutLamp.setVisibility(VIEw.VISIBLE);            OpenLamp.setVisibility(VIEw.INVISIBLE);            LampSwitch.setChecked(false);        }else if (RandomMaxTmpValue < 0){  //开启灯泡            Fans.clearanimation();            FansSwitch.setChecked(false);            ShutLamp.setVisibility(VIEw.INVISIBLE);            OpenLamp.setVisibility(VIEw.VISIBLE);            LampSwitch.setChecked(true);        }else{  //等于0时,都不开启            Fans.clearanimation();            FansSwitch.setChecked(false);            ShutLamp.setVisibility(VIEw.VISIBLE);            LampSwitch.setChecked(false);        }    }    private voID SetTitle() {        Actionbar actionbar = getSupportActionbar();        if (actionbar != null) {            actionbar.setdisplayHomeAsUpEnabled(true);            actionbar.setHomebuttonEnabled(true);        }    }    @OverrIDe    public boolean onoptionsItemSelected(@NonNull MenuItem item) {        switch (item.getItemID()){            case androID.R.ID.home:                Intent intent = new Intent(MainActivity.this,ChooseInterface.class);                startActivity(intent);                break;        }        return super.onoptionsItemSelected(item);    }}
人体红外传感器界面

先看一下布局效果


代码如下:
布局简单,其中可以动的圈是自定义view,java部分详解

<linearLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context=".InfraredInterface"    androID:orIEntation="vertical">   <relativeLayout       androID:layout_wIDth="match_parent"       androID:layout_height="wrap_content">   <com.example.myapplication.CustomProgressbar       androID:ID="@+ID/CustomProgressbar"       androID:layout_wIDth="200dp"       androID:layout_height="200dp"       androID:layout_margintop="80dp"       androID:layout_marginleft="90dp"/>   <TextVIEw       androID:ID="@+ID/TipsWord"       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:text="正在探索红外设备中..."       androID:layout_margintop="200dp"       androID:layout_marginleft="120dp"       androID:layout_gravity="center"       androID:textcolor="#ff0000"       />   </relativeLayout>   <linearLayout       androID:layout_wIDth="match_parent"       androID:layout_height="wrap_content"       androID:layout_margintop="70dp">   <TextVIEw       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:text="探索到周围红外设备:  "       androID:layout_marginleft="65dp"       androID:textSize="25sp"      />   <TextVIEw       androID:ID="@+ID/Infrarednumber"       androID:layout_wIDth="wrap_content"       androID:layout_height="wrap_content"       androID:textSize="25sp"       androID:text="             "       androID:textcolor="#ff0000"       />   </linearLayout>   <Imagebutton       androID:ID="@+ID/Start"       androID:layout_wIDth="70dp"       androID:layout_height="70dp"       androID:text="点击开始探测"       androID:src="@drawable/start"       androID:background="#00000000"       androID:scaleType="fitCenter"       androID:layout_margintop="40dp"       androID:layout_marginleft="150dp"/></linearLayout>

java部分:
将俩部分吧,一个是Handler部分,一个是自定义view部分
Handler部分:实现Handler.Callback接口,主要工作流程是,延迟一会,然后这个进度值,把进度值通过线程方式将这个进度值发过去,然后自定义view就把自己的进度值设置为自己的进度值。
自定义view部分:
主要是先画一个背景圆,然后在上面画一层描边(圆弧),这个是通过进度值的大小来改变的,然后关于在values下创建一个attrs.xml文件(名字不可改),然后设置圆的颜色,大小等。

public class CustomProgressbar extends VIEw {    private Paint BackgroundPaint;  //底层的画笔    private Paint BufferPaint;      //上层画笔    private int Backgroundcolor;    //底层的颜色    private int Buffercolor;       //上层的颜色    private float RingWIDth;    private int Max;    private int CurrentProgress;    public CustomProgressbar(Context context) {        this(context, null);    }    public CustomProgressbar(Context context, @Nullable AttributeSet attrs) {        this(context, attrs, 0);    }    public CustomProgressbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        InitAttrs(context, attrs);        InitPaint();    }    private voID InitPaint() {        BackgroundPaint = new Paint();        BackgroundPaint.setcolor(Backgroundcolor);        BackgroundPaint.setStyle(Paint.Style.stroke);  //描边样式        BackgroundPaint.setstrokeWIDth(RingWIDth);        BackgroundPaint.setAntiAlias(true); //抗锯齿        /**         * paint的属性         * ANTI_AliAS_FLAG,抗锯齿         * UNDERliNE_TEXT_FLAG,文字下划线         * STRIKE_THRU_TEXT_FLAG,文字中间穿过线         * FAKE_BolD_TEXT_FLAG,文字粗体         * VERTICAL_TEXT_FLAG,字体垂直摆放的属性(被隐藏不可见)         */        /**         * Paint.Style.FILL设置只绘制图形内容         * *Paint.Style.stroke设置只绘制图形的边         *Paint.Style.FILL_AND_stroke设置都绘制         * */        /**         * paint的线帽外形         * Paint.Cap.BUTT 没有线帽         * Paint.Cap.ROUND 圆形线帽         * Paint.Cap.SQUARE 方形线帽         */        /**         * Paint.Join.ROUND 圆角         * Paint.Join.MITER 锐角(默认值)         * Paint.Join.BEVEL 直角         */        BufferPaint = new Paint();        BufferPaint.setcolor(Buffercolor);        BufferPaint.setstrokeWIDth(RingWIDth);        BufferPaint.setstrokeCap(Paint.Cap.ROUND);        BufferPaint.setAntiAlias(true);        BufferPaint.setStyle(Paint.Style.stroke);    }    private voID InitAttrs(Context context, AttributeSet attrs) {        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressbar);        Backgroundcolor = typedArray.getcolor(R.styleable.CustomProgressbar_Backgroundcolor, color.GRAY);        Buffercolor = typedArray.getcolor(R.styleable.CustomProgressbar_Buffercolor, color.GREEN);        RingWIDth = typedArray.getDimension(R.styleable.CustomProgressbar_RingWIDth, 20);        Max = typedArray.getInteger(R.styleable.CustomProgressbar_ProgressbarMax, 100);        //资源回收        typedArray.recycle();    }    @OverrIDe    protected voID onDraw(Canvas canvas) {        super.onDraw(canvas);        int X = getWIDth() / 2;        int Y = getHeight() / 2;        int Radius = (int) (X - RingWIDth / 2);        // 绘制背景圆        canvas.drawCircle(X, Y, Radius, BackgroundPaint);        //绘制圆的大小,left,top,Right,Bottom        RectF rectF = new RectF(X - Radius, Y - Radius, X + Radius, Y + Radius);        //ovel,开始绘制的角度,结束角度(每一个Progress等于3.6角度),是否经过圆形(因为我们前面设置了描边属性,所以即使设置为true也看不出效果,所以为false);        canvas.drawArc(rectF, -90, CurrentProgress * 360 / Max, false, BufferPaint);    }    public synchronized int getMax() {        return Max;    }    public synchronized voID setMax(int max) {        if (max < 0) {            throw new IllegalArgumentException("最大进度不能小于0");        }        this.Max = max;    }    public synchronized int getProgress() {        return CurrentProgress;    }    public synchronized voID setProgress(int progress) {        if (progress < 0) {            throw new IllegalArgumentException("进度不能小于0");        }        if (progress > Max) {            progress = Max;        }        if (progress <= Max) {            this.CurrentProgress = progress;            postInvalIDate();        }    }    public Paint getBgPaint() {        return BackgroundPaint;    }    public voID setBgPaint(Paint bgPaint) {        this.BackgroundPaint = bgPaint;    }    public int getBgcolor() {        return Backgroundcolor;    }    public voID setBgcolor(int bgcolor) {        this.Backgroundcolor = bgcolor;    }    public Paint getRingProgresspaint() {        return BufferPaint;    }    public voID setRingProgresspaint(Paint ringProgresspaint) {        this.BufferPaint = ringProgresspaint;    }    public int getRingProgresscolor() {        return Buffercolor;    }    public voID setRingProgresscolor(int ringProgresscolor) {        this.Buffercolor = ringProgresscolor;    }    public float getRingWIDth() {        return RingWIDth;    }    public voID setRingWIDth(float ringWIDth) {        this.RingWIDth = ringWIDth;    }}
public class InfraredInterface extends AppCompatActivity implements  Handler.Callback{    private CustomProgressbar MyProgressbar;    private int Progress;    private Handler handler;    private TextVIEw TipsWord,Infrarednumber;    private Imagebutton Start;    private static int num = (int) (Math.random()*(11-1)); //0-10;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_infrared_interface);        SetTitle();        InitVIEw();        handler = new Handler(this);        Start.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                new Thread(new Runnable() {                    @OverrIDe                    public voID run() {                        while (true) {                            try {                                Thread.sleep(10);                                Progress++;                                handler.sendEmptyMessage(1);                                if (Progress >= 100) {                                    test();                                }                            } catch (Exception e) {                                e.printstacktrace();                            }                        }                    }                }).start();            }        });    }    @OverrIDe    public boolean handleMessage(Message msg) {        switch (msg.what) {            case 1:                MyProgressbar.setProgress(Progress);                MyProgressbar.invalIDate();                break;            default:                break;        }        return false;    }    private voID test(){        TipsWord.setText("        探索完毕!");        Infrarednumber.setText(num+"个");    }    private voID SetTitle(){        Actionbar actionbar = getSupportActionbar();        if (actionbar != null){            actionbar.setdisplayHomeAsUpEnabled(true);            actionbar.setHomebuttonEnabled(true);        }    }    @OverrIDe    public boolean onoptionsItemSelected(@NonNull MenuItem item) {        switch (item.getItemID()){            case androID.R.ID.home:                Intent intent = new Intent(InfraredInterface.this,ChooseInterface.class);                startActivity(intent);                break;        }        return super.onoptionsItemSelected(item);    }    private voID InitVIEw(){        MyProgressbar = findVIEwByID(R.ID.CustomProgressbar);        TipsWord = findVIEwByID(R.ID.TipsWord);        Infrarednumber = findVIEwByID(R.ID.Infrarednumber);        Start = findVIEwByID(R.ID.Start);    }}
总结

以上是内存溢出为你收集整理的Android——一个简单的智能家居系统全部内容,希望文章能够帮你解决Android——一个简单的智能家居系统所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1026882.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存