forked from a2633063/SmartControl_Android_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
357 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
app/src/main/java/com/zyc/zcontrol/deviceItem/a1/A1LedActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
package com.zyc.zcontrol.deviceItem.a1; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.graphics.Color; | ||
import android.graphics.drawable.BitmapDrawable; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.view.MenuItem; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.widget.ImageView; | ||
import android.widget.SeekBar; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.zyc.zcontrol.MainApplication; | ||
import com.zyc.zcontrol.R; | ||
import com.zyc.zcontrol.ServiceActivity; | ||
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceA1; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
public class A1LedActivity extends ServiceActivity { | ||
public final static String Tag = "A1LedActivity"; | ||
|
||
|
||
private ArrayList<SeekBar> seekBar = new ArrayList<SeekBar>(); | ||
private ArrayList<TextView> textView = new ArrayList<TextView>(); | ||
ImageView imageView; | ||
Bitmap bitmap; | ||
|
||
DeviceA1 device; | ||
|
||
Boolean Sflag = false; | ||
@SuppressLint("HandlerLeak") | ||
Handler handler = new Handler() { | ||
@Override | ||
public void handleMessage(Message msg) {// handler接收到消息后就会执行此方法 | ||
switch (msg.what) { | ||
case 1: | ||
Send("{\"mac\": \"" + device.getMac() + "\",\"color\":null}"); | ||
break; | ||
case 2: | ||
if (Sflag) { | ||
Sflag=false; | ||
int a = seekBar.get(0).getProgress() * 255 | ||
+ seekBar.get(1).getProgress() | ||
+ seekBar.get(2).getProgress() * 255 * 255; | ||
Send("{\"mac\": \"" + device.getMac() + "\",\"color\":" + a + "}"); | ||
} | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_a1_led); | ||
|
||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标 | ||
getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用 | ||
|
||
//region 设备 | ||
Intent intent = this.getIntent(); | ||
try { | ||
device = (DeviceA1) ((MainApplication) getApplication()).getDevice(intent.getStringExtra("mac")); | ||
if (device == null) { | ||
throw new Exception("获取数据出错:" + intent.getStringExtra("mac")); // 异常信息 | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
Toast.makeText(A1LedActivity.this, "数据错误!请联系开发者", Toast.LENGTH_SHORT).show(); | ||
finish(); | ||
} | ||
//endregion | ||
|
||
this.setTitle(device.getName()); | ||
|
||
//region 控件初始化 | ||
seekBar.add((SeekBar) findViewById(R.id.seekBarR)); | ||
seekBar.add((SeekBar) findViewById(R.id.seekBarG)); | ||
seekBar.add((SeekBar) findViewById(R.id.seekBarB)); | ||
seekBar.get(0).setOnSeekBarChangeListener(seekListener); | ||
seekBar.get(1).setOnSeekBarChangeListener(seekListener); | ||
seekBar.get(2).setOnSeekBarChangeListener(seekListener); | ||
|
||
textView.add((TextView) findViewById(R.id.textViewR)); | ||
textView.add((TextView) findViewById(R.id.textViewG)); | ||
textView.add((TextView) findViewById(R.id.textViewB)); | ||
|
||
imageView = findViewById(R.id.hsl); | ||
bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); | ||
|
||
imageView.setOnTouchListener(InamgeViewListener); | ||
findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
if(seekBar.get(0).getProgress()==0 && seekBar.get(1).getProgress()==0 &&seekBar.get(2).getProgress()==0){ | ||
seekBar.get(0).setProgress(255); | ||
seekBar.get(1).setProgress(255); | ||
seekBar.get(2).setProgress(255); | ||
}else{ | ||
seekBar.get(0).setProgress(0); | ||
seekBar.get(1).setProgress(0); | ||
seekBar.get(2).setProgress(0); | ||
} | ||
Sflag=true; | ||
} | ||
}); | ||
//endregion | ||
|
||
//启动定时器 | ||
TimerTask mTimerTask = new TimerTask() { | ||
public void run() { | ||
handler.sendEmptyMessageDelayed(2, 0); | ||
} | ||
}; | ||
Timer timer = new Timer(true); | ||
timer.schedule(mTimerTask, 200, 200); //延时200ms后执行,200ms执行一次 | ||
//timer.cancel(); //退出计时器 | ||
} | ||
|
||
public boolean onOptionsItemSelected(MenuItem item) { | ||
switch (item.getItemId()) { | ||
case android.R.id.home: | ||
this.finish(); | ||
return true; | ||
} | ||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
//滚动条监视事件 | ||
private SeekBar.OnSeekBarChangeListener seekListener = new SeekBar.OnSeekBarChangeListener() { | ||
|
||
@Override | ||
public void onProgressChanged(SeekBar arg, int progress, boolean fromUser) { | ||
|
||
|
||
textView.get(0).setText("R:" + String.format("%03d", seekBar.get(0).getProgress())); | ||
textView.get(1).setText("G:" + String.format("%03d", seekBar.get(1).getProgress())); | ||
textView.get(2).setText("B:" + String.format("%03d", seekBar.get(2).getProgress())); | ||
|
||
if(fromUser) | ||
Sflag = true;//sendRGB(); | ||
|
||
} | ||
|
||
@Override | ||
public void onStartTrackingTouch(SeekBar arg0) { | ||
} | ||
|
||
@Override | ||
public void onStopTrackingTouch(SeekBar arg0) { | ||
Sflag = true;//sendRGB(); | ||
} | ||
|
||
}; | ||
|
||
|
||
//ImageView触摸事件 | ||
private View.OnTouchListener InamgeViewListener = new View.OnTouchListener() { | ||
|
||
@Override | ||
public boolean onTouch(View arg0, MotionEvent arg1) { | ||
|
||
imageView.getParent().requestDisallowInterceptTouchEvent(true); | ||
|
||
int x = (int) arg1.getX(); | ||
int y = (int) arg1.getY(); | ||
|
||
|
||
try { | ||
int pixel = bitmap.getPixel(x, y);//获取颜色 | ||
int redValue = Color.red(pixel); | ||
int greenValue = Color.green(pixel); | ||
int blueValue = Color.blue(pixel); | ||
|
||
if (redValue != 255 && greenValue != 255 && blueValue != 255) return true; | ||
if (pixel == 0 || (redValue == 0 && greenValue == 0 && blueValue == 0)) | ||
return true; //仅判断pixel会偶尔无法跳出 | ||
seekBar.get(0).setProgress(redValue); | ||
seekBar.get(1).setProgress(greenValue); | ||
seekBar.get(2).setProgress(blueValue); | ||
|
||
// HSL = RGBtoHSL(redValue, greenValue, blueValue); | ||
Sflag = true;//sendRGB(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return true; | ||
|
||
} | ||
|
||
}; | ||
|
||
|
||
//region 数据接收发送处理函数 | ||
void Send(String message) { | ||
boolean b = getSharedPreferences("Setting_" + device.getMac(), 0).getBoolean("always_UDP", false); | ||
super.Send(b, device.getSendMqttTopic(), message); | ||
} | ||
|
||
public void Receive(String ip, int port, String topic, String message) { | ||
|
||
try { | ||
JSONObject jsonObject = new JSONObject(message); | ||
if (!jsonObject.has("mac") || !jsonObject.getString("mac").equals(device.getMac())) { | ||
return; | ||
} | ||
|
||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
//endregion | ||
//region 事件监听调用函数,主要为在子类中重写此函数实现在service建立成功/mqtt连接成功/失败时执行功能 | ||
//Service建立成功时调用 此函数需要时在子类中重写 | ||
public void ServiceConnected() { | ||
handler.sendEmptyMessageDelayed(1, 0); | ||
} | ||
|
||
//mqtt连接成功时调用 此函数需要时在子类中重写 | ||
public void MqttConnected() { | ||
handler.sendEmptyMessageDelayed(1, 0); | ||
} | ||
|
||
//mqtt连接断开时调用 此函数需要时在子类中重写 | ||
public void MqttDisconnected() { | ||
handler.sendEmptyMessageDelayed(1, 0); | ||
} | ||
//endregion | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="32dp" | ||
tools:context=".deviceItem.a1.A1LedActivity"> | ||
|
||
<TextView | ||
android:id="@+id/textViewR" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="32dp" | ||
android:text="R:000" | ||
android:textSize="22sp" | ||
app:layout_constraintEnd_toStartOf="@+id/seekBarR" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<SeekBar | ||
android:id="@+id/seekBarR" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:max="255" | ||
android:progress="0" | ||
android:thumb="@drawable/seekbar_thumb" | ||
app:layout_constraintBottom_toBottomOf="@+id/textViewR" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toEndOf="@+id/textViewR" | ||
app:layout_constraintTop_toTopOf="@+id/textViewR" /> | ||
|
||
|
||
<TextView | ||
android:id="@+id/textViewG" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="8dp" | ||
android:text="G:000" | ||
android:textSize="22sp" | ||
app:layout_constraintEnd_toStartOf="@+id/seekBarG" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/textViewR" /> | ||
|
||
<SeekBar | ||
android:id="@+id/seekBarG" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:max="255" | ||
android:progress="0" | ||
android:thumb="@drawable/seekbar_thumb" | ||
app:layout_constraintBottom_toBottomOf="@+id/textViewG" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toEndOf="@+id/textViewG" | ||
app:layout_constraintTop_toTopOf="@+id/textViewG" /> | ||
|
||
<TextView | ||
android:id="@+id/textViewB" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="8dp" | ||
android:text="B:000" | ||
android:textSize="22sp" | ||
app:layout_constraintEnd_toStartOf="@+id/seekBarB" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/textViewG" /> | ||
|
||
<SeekBar | ||
android:id="@+id/seekBarB" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:max="255" | ||
android:progress="0" | ||
android:thumb="@drawable/seekbar_thumb" | ||
app:layout_constraintBottom_toBottomOf="@+id/textViewB" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toEndOf="@+id/textViewB" | ||
app:layout_constraintTop_toTopOf="@+id/textViewB" /> | ||
|
||
<ImageView | ||
android:id="@+id/hsl" | ||
android:layout_width="wrap_content" | ||
android:scaleType="fitCenter" | ||
android:layout_height="0dp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/textViewB" | ||
app:srcCompat="@drawable/hsl" /> | ||
|
||
<ImageView | ||
android:id="@+id/ok" | ||
android:layout_width="120dp" | ||
|
||
android:layout_height="120dp" | ||
app:layout_constraintBottom_toBottomOf="@+id/hsl" | ||
app:layout_constraintEnd_toEndOf="@+id/hsl" | ||
app:layout_constraintStart_toStartOf="@+id/hsl" | ||
app:layout_constraintTop_toTopOf="@+id/hsl" | ||
app:srcCompat="@drawable/ok" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |