一個簡單的智能家居系統
- 效果展示
- 啟動應用界面
- 登陸界面
- 導航界面
- 溫度界面
- 濕度界面
- 煙霧傳感器界面
- 人體紅外傳感器界面
效果展示
以下為整個程式的操作流程,因為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,而是搜索那幾個關鍵字,然后給那個圖片這個一個點擊事件,然后進行頁面跳轉,
布局檔案代碼如下:
<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"
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/271385.html
標籤:其他
