<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="104dp"
android:onClick="doAsyncTask"
android:text="开始" />
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("onCreate" + Thread.currentThread().getId());
}
public void doAsyncTask(View view){
new InnerAsyncTask().execute("");
}
private class InnerAsyncTask extends AsyncTask<Object, Object, Object>{
@Override
protected Object doInBackground(Object... params) {
for(int i = 0; i < 30;i++){
System.out.println("InnerAsyncTask" + Thread.currentThread().getId());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
}
}
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="104dp" android:onClick="doAsyncTask" android:text="开始" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginTop="22dp" android:src="@drawable/abs" />
public class MainActivity extends Activity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imageView1);
// System.out.println("onCreate" + Thread.currentThread().getId());
}
public void doAsyncTask(View view){
new InnerAsyncTask().execute("");
}
private class InnerAsyncTask extends AsyncTask<String,Integer, Bitmap>{
@Override
protected Bitmap doInBackground(String... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return BitmapFactory.decodeResource(getResources(), R.drawable.abc);
}
@Override
protected void onPostExecute(Bitmap result) {
image.setImageBitmap(result);
}
}
}
<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" >
<TextView
android:id="@+id/tv_pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100%"
android:visibility="gone"
android:textSize="16sp"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="104dp"
android:onClick="doAsyncTask"
android:text="开始" />
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_update"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:src="@drawable/abs" />
<ProgressBar
android:id="@+id/pb_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:max="100"
android:visibility="gone"
android:layout_alignRight="@+id/btn_update"
android:layout_marginTop="32dp" />
</RelativeLayout>
public class LoadImage extends AsyncTask<String, Integer, Object> {
private Context context;
private ImageView imageview;
private Bitmap image;
private Button button;
private ProgressBar pg;
private TextView tv;
public LoadImage(Context context, Button button, ImageView imageview,
ProgressBar pg, TextView tv) {
this.context = context;
this.imageview = imageview;
this.button = button;
this.pg = pg;
this.tv = tv;
}
@Override
protected Object doInBackground(String... params) {
for (int i = 0; i <= 100; i++) {
publishProgress(i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
image = BitmapFactory.decodeResource(context.getResources(),
R.drawable.abc);
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
pg.setProgress(values[0]);
tv.setText(values[0] + "%");
}
@Override
protected void onPostExecute(Object result) {
imageview.setImageBitmap(image);
button.setEnabled(true);
pg.setVisibility(View.GONE);
tv.setVisibility(View.GONE);
}
}
public class MainActivity extends Activity {
private ImageView image;
private Button button;
private ProgressBar pg;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.iv_image);
button = (Button) findViewById(R.id.btn_update);
pg = (ProgressBar) findViewById(R.id.pb_progress);
tv = (TextView) findViewById(R.id.tv_pb);
}
public void doAsyncTask(View view){
button.setEnabled(false);
pg.setVisibility(View.VISIBLE);
tv.setVisibility(View.VISIBLE);
new LoadImage(this,button,image,pg,tv).execute("");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有