UI线程阻塞问题
来源:互联网 发布:sql select两个表 编辑:程序博客网 时间:2024/05/22 09:40
两种方法:
1.view.post;
2.AsyncTask;
(1)java文件:
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
public class MyActivity extends Activity {
private Button button2=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button button1=(Button) findViewById(R.id.button1);
TranslateAnimation animation=new TranslateAnimation(0,150,0,0);
animation.setRepeatCount(100);
animation.setDuration(2000);
button1.setAnimation(animation);
final Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread2().execute();
}
/*方法一:v.post 可读性差,不易维护
public void onClick(final View v) {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
v.post(new Runnable() {
@Override
public void run() {
TextView view=(TextView) v;
view.setText(""+10);
}
});
}
}).start();
}*/
//方法二:AsyncTask方法
class Thread2 extends AsyncTask<String, Integer, Integer> {
protected void onPostExecute(Integer sum) {
button2.setText(""+10);
}
protected Integer doInBackground(String... url) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int sum =10;
return sum;
}
}
});
}
}
(2)xml文件:
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="@+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="76dp"
android:layout_marginTop="61dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/button2"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button" />
</RelativeLayout>
- UI线程阻塞问题
- 用AsyncTask处理UI线程阻塞问题
- 关于UI线程的阻塞问题以及处理方式
- Android学习:UI线程阻塞
- UI线程阻塞及优化
- UI线程阻塞以及优化
- 关于多线程中涉及UI操作,导致UI线程阻塞问题
- android 异步回调中操作UI线程,UI同步、卡死阻塞等性能问题
- Android 异步回调中操作UI线程,UI同步、卡死阻塞等性能问题
- Asynctask线程阻塞问题
- android解决UI阻塞问题——创建AsyncTask 子线程
- android解决UI阻塞问题——创建AsyncTask 子线程
- 0顶 jQuery同步Ajax带来的UI线程阻塞问题及...
- jQuery同步Ajax带来的UI线程阻塞问题及解决方法
- jQuery同步Ajax带来的UI线程阻塞问题及解决办法
- jQuery同步Ajax带来的UI线程阻塞问题及解决办法
- 简述Androiod UI线程阻塞及其优化
- android ui线程阻塞和优化
- (图解)Description Resource Path Location Type Java compiler level does not match the version of
- uva 11825 黑客的攻击 状态dp 典型
- javascript 加入收藏
- itoa 函数!!!(window平台下)
- POJ 1028----Web Navigation
- UI线程阻塞问题
- C之诡谲
- hdu2196 Computer 树形DP 树上点到其它点的最远距离
- python K-Means 实例二则
- ViewPager + Fragment 实现页面滑动
- nginx和tomcat之间连接复用要注意的问题
- .NET基础回顾
- C语言学习资源汇总
- 在iOS7中修改状态栏字体的颜色