- AsyncTask实现的原理,和适用的优缺点
AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主线程.
以下部分是学习代码:
UI:
1 <RelativeLayout xmlns:andro 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <ImageView 12 android: 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:maxWidth="750dp" 16 android:maxHeight="400dp" 17 android:adjustViewBounds="true" 18 /> 19 20 <Button 21 android: 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_alignParentBottom="true" 25 android:layout_centerHorizontal="true" 26 android:layout_marginBottom="68dp" 27 android:text="下载网络图片" /> 28 29 30 </RelativeLayout>