我有这个代码,我在将一个任务添加到与Volley进行通信的队列之后立即在图像上启动旋转动画。但是,一旦请求完成,我就无法停止动画。
我尝试将视图传递给函数,并且在onResponse
回调中停止动画,虽然没有用。
private void syncDevice(View sync) {
JSONArray data = db.getData();
if (data.length() > 0) {
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(1000);
anim.setRepeatCount(Animation.INFINITE);
sync.startAnimation(anim);
//Send data to the server
sendDataToServer(data);
showSnackbar(getResources().getString(R.string.device_successfully_synced));
} else {
showSnackbar(getResources().getString(R.string.device_already_synced));
}
}
private void sendDataToServer(JSONArray data) {
String url = getResources().getString(R.string.url_base) + getResources().getString(R.string.url_upload_submissions);
JSONObject js = new JSONObject();
try {
js.put("token", token);
js.put("data", data);
} catch (JSONException e) {
//
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST,url, js,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
//Add request to queue
Volley.newRequestQueue(this).add(jsonObjReq);
}