버튼을 누르면
http요청후 받은 response를 파싱해 3개의 이미지 뷰가 움직이는 스레드입니다.
핸들러에 인자로 보내는 runnable 을 보시면 3개의 이미지뷰를 위한 애니매이션이 있는데
주석처리 하고 살펴보니 1개의 애니메이션은 괜챃았으나
2개 이상애니메이션을 실행하면 심하게 버벅이기 시작합니다.
문제가 있는 코드인가요?
protected String doInBackground(String... params) {
while (runnig){
HttpUrl url = new HttpUrl.Builder()
.scheme("http") //http
.host("xxx")
.port(3000)
.build();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
try {
Response response = client.newCall(request).execute();
responseStr = response.body().string();
JSONArray jsonOutput = null;
try {
jsonOutput = new JSONArray(responseStr);
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject tem = jsonOutput.getJSONObject(0);
temStr = tem.getString("tem");
speedStr = tem.getString("speed");
rpmStr = tem.getString("rpm");
valtageStr = tem.getString("valtage");
airflowStr = tem.getString("airflow");
timeStr = tem.getString("lastTime");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() { // Runnable 의 Run() 메소드에서 UI 접근
PID1.setText("Vatage: " +valtageStr);
PID2.setText("Temperature : "+temStr);
PID3.setText("Speed : "+speedStr);
PID4.setText("Rpm : "+rpmStr);
PID5.setText("Airflow : "+airflowStr);
if(valtageStr!=null && airflowStr != null) {
progressBarValtage.setPos(Integer.parseInt(valtageStr));
progressBarAirflow.setPos(Integer.parseInt(airflowStr));
}
int rpmint = Integer.parseInt(rpmStr);
rpmint = rpmint*2;
RotateAnimation ani1 = new RotateAnimation(beforeDgreeRpm,rpmint,RotateAnimation.RELATIVE_TO_SELF,0.85f,RotateAnimation.RELATIVE_TO_SELF,0.3f);
ani1.setFillAfter(true);
ani1.setDuration(1000);
ani1.setRepeatCount(0);
rpmView.setAnimation(ani1);
rpmView.startAnimation(ani1);
beforeDgreeRpm = rpmint;
//Tem은 50~100의 값이 들어온다
int temint = Integer.parseInt(temStr);
temint = temint/2;
RotateAnimation ani2 = new RotateAnimation(beforeDgreeTem,temint,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.85f);
ani2.setFillAfter(true);
ani2.setDuration(1000);
ani2.setRepeatCount(0);
temView.setAnimation(ani2);
temView.startAnimation(ani2);
beforeDgreeTem = temint;
//speed는 20~80이 들어온다
int speedint = Integer.parseInt(speedStr);
speedint = speedint*2;
RotateAnimation ani3 = new RotateAnimation(beforeDgreeSpeed,speedint,RotateAnimation.RELATIVE_TO_SELF,0.85f,RotateAnimation.RELATIVE_TO_SELF,0.3f);
ani3.setFillAfter(true);
ani3.setDuration(1000);
ani3.setRepeatCount(0);
speedView.setAnimation(ani3);
speedView.startAnimation(ani3);
beforeDgreeSpeed = speedint;
}
});
if(isCancelled()){runnig=false;}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}