gpt4 book ai didi

Android 动画仅在 AsyncTask 完成后开始

转载 作者:行者123 更新时间:2023-11-29 15:22:15 25 4
gpt4 key购买 nike

我正在尝试创建一个 native 登录屏幕,其中用户名和密码框在处理请求时从屏幕上滑出(如果登录不成功,则滑回屏幕)。

为了实现这一点,我定义了我的动画 (DropDownAnimation) 并将其分配给我的 LinearLayout(页脚)。当用户单击“登录”按钮时,我启动动画,然后调用启动 AsyncTask 的函数 (tryLogin())。 AsyncTask 处理创建和发送登录请求以及获取 JSONObject 响应的所有工作。

但是,我的问题是 slideDown 动画直到 AsyncTask 完成后才开始。这在成功登录时看起来还不错,但在登录失败时,这意味着 LinearLayout 永远不会向下滑动 - 它会跳到屏幕底部,开始 slideUp 动画回到原来的位置。

这似乎是一个与 this question 类似的问题,但我没有使用 bindService() 并且我的所有非 UI 代码(对我而言)似乎已经包含在 AsyncTask 中。 LogCat 告诉我:

06-24 04:37:35.141: I/Choreographer(5347): 跳过 137 帧!应用程序可能在其主线程上做了太多工作。

我假设这些是页脚将向下滑动的框架 - 但我无法弄清楚我在主线程上执行任务的位置。这是我的 LoginPage 和 LoginTask 代码。

登录页面.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);

login = (Button) findViewById(R.id.login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
footer = (LinearLayout) findViewById(R.id.footer);

// We must wait for the layout to be finalised before trying to find heights.
ViewTreeObserver vto = footer.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
initAnimations();
}
});

loading = (TextView) findViewById(R.id.loading);

login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mUsername = username.getText().toString();
String mPassword = password.getText().toString();

// Neither of these two things happen until after LoginTask is done.
footer.startAnimation(slideDown);
loading.setVisibility(TextView.VISIBLE);

tryLogin(mUsername, mPassword);
}
});
}

protected void tryLogin(String mUsername, String mPassword) {
Exception e;
String loginUrl = getString(R.string.login_url);
String clientId = getString(R.string.client_id);
String clientSecret = getString(R.string.client_secret);
LoginTask loginTask = (LoginTask) new LoginTask().execute(mUsername, mPassword, loginUrl, clientId, clientSecret);
if ((e = loginTask.getException()) != null) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
} else {
JSONObject response;
try {
response = loginTask.get();
Log.d("login", response.toString());
if (!response.has("access_token")) {
loading.setVisibility(TextView.INVISIBLE);
footer.startAnimation(slideUp);
Toast.makeText(this, "Login error", Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(this, FullscreenWebView.class);
i.putExtra("accessToken", response.get("access_token").toString());
startActivity(i);
overridePendingTransition(0, 0);
}
} catch (InterruptedException e1) {
e1.printStackTrace();
Thread.currentThread().interrupt();
} catch (ExecutionException e1) {
e1.printStackTrace();
} catch (JSONException e1) {
e1.printStackTrace();
throw new RuntimeException(e);
}
}
}

LoginTask.java

class LoginTask extends AsyncTask<String, Void, JSONObject> {
private Exception exception;

@Override
protected JSONObject doInBackground(String... params) {
HttpURLConnection connection;
OutputStreamWriter request = null;

URL url = null;
JSONObject response = null;
String parameters = "grant_type=password&username="+params[0]+"&password="+params[1]+"&client_id="+params[3]+"&client_secret="+params[4];

try {
url = new URL(params[2]);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");

request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();

// username or password is probably wrong
Log.d("login", ""+connection.getResponseCode());
if (connection.getResponseCode() != 200) {
return new JSONObject();
}
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}

Log.d("login", sb.toString());
response = new JSONObject(sb.toString());

isr.close();
reader.close();
} catch (Exception e) {
this.exception = e;
}

return response;
}
}

我也试过让 LoginTask 成为 LoginPage 的成员类,并在 onPreExecute() 方法中启动动画,但是这并没有改变任何东西。

非常感谢任何帮助!

最佳答案

当您使用 AsyncTask.get() 时,您将阻塞 UI 线程。当动画在 UI 线程上运行时,它看起来好像没有运行(但实际上它被长时间运行的 tryLogin 方法阻止)。

相反,您应该将依赖于 LoginTask 结果的代码移动到它的 onPostExecute 方法:

protected void tryLogin(String mUsername, String mPassword) {
String loginUrl = getString(R.string.login_url);
String clientId = getString(R.string.client_id);
String clientSecret = getString(R.string.client_secret);
new LoginTask().execute(mUsername, mPassword,
loginUrl, clientId, clientSecret);
}

LoginTask.java

class LoginTask extends AsyncTask<String, Void, JSONObject> {
private Exception exception;

@Override
protected JSONObject doInBackground(String... params) {
// Unchanged
}

public void onPostExecute(JSONObject response) {
if (exception != null) {
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
} else {
Log.d("login", response.toString());
if (!response.has("access_token")) {
loading.setVisibility(TextView.INVISIBLE);
footer.startAnimation(slideUp);
Toast.makeText(this, "Login error", Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(this, FullscreenWebView.class);
i.putExtra("accessToken", response.get("access_token").toString());
startActivity(i);
overridePendingTransition(0, 0);
}
}
}
}

关于Android 动画仅在 AsyncTask 完成后开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17268140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com