android学习 豆瓣 自定义Progressbar 异步任务加载数据等
1.自定义progressbar
xml
<RelativeLayout android:layout_above="@id/mainRL" android:id="@+id/loading" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="invisible"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#AA111111" android:padding="8dip" android:gravity="center"> <ProgressBar android:id="@+id/progress" android:layout_width="24dip" android:layout_height="24dip" android:layout_marginTop="6dip" android:layout_marginBottom="6dip" android:indeterminate="true" /> <TextView android:textColor="#FFFFFF" android:shadowColor="#000000" android:id="@+id/txt_loading" android:shadowDx="0" android:shadowDy=".5" android:shadowRadius="1" android:layout_toRightOf="@id/progress" android:layout_marginLeft="8dip" android:text="正在加载" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" /> </LinearLayout> </RelativeLayout>
界面中如果他 直接include比较好
然后是显示和隐藏的方法 含有动画
public void showLoading(){ mRelativeLoading.setVisibility(View.VISIBLE); AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f); aa.setDuration(1000); ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f); sa.setDuration(1000); AnimationSet set = new AnimationSet(false); set.addAnimation(sa); set.addAnimation(aa); mRelativeLoading.setAnimation(set); mRelativeLoading.startAnimation(set); } public void hideLoading(){ AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f); aa.setDuration(1000); ScaleAnimation sa = new ScaleAnimation(1.0f, 0.0f, 1.0f,0.0f); sa.setDuration(1000); AnimationSet set = new AnimationSet(false); set.addAnimation(sa); set.addAnimation(aa); mRelativeLoading.setAnimation(set); mRelativeLoading.startAnimation(set); mRelativeLoading.setVisibility(View.INVISIBLE); }
这个写在baseactivity中谁用谁调用就可以了
2.异步加载任务
@Override public void fillData() { new AsyncTask<Void, Void, Void>() { //onPreExecute 在异步任务执行之前调用的方法 // 运行在主线程里面的 // 初始化ui的操作 @Override protected void onPreExecute() { showLoading(); super.onPreExecute(); } // onPostExecute 在异步任务(后台任务)执行之后调用的方法 // 运行在ui线程中 , // @Override protected void onPostExecute(Void result) { hideLoading(); super.onPostExecute(result); tv_info.setText(content); tv_location.setText(location); tv_name.setText(name); //设置用户的头像 LoadImageAsynTask task = new LoadImageAsynTask(new LoadImageAsynTaskCallback() { public void beforeLoadImage() { iv_icon.setImageResource(R.drawable.ic_launcher); } public void afterLoadImage(Bitmap bitmap) { if (bitmap!=null) { iv_icon.setImageBitmap(bitmap); }else{ iv_icon.setImageResource(R.drawable.ic_launcher); } } }); task.execute(iconurl); } // doInBackground 后台执行的任务 // 方法运行在一个子线程当中 @Override protected Void doInBackground(Void... params) { // 执行耗时的操作 try { UserEntry ue = myService.getAuthorizedUser(); name = ue.getTitle().getPlainText(); location = ue.getLocation(); content = ((TextContent) ue.getContent()).getContent().getPlainText(); for (Link link : ue.getLinks()) { if("icon".equals(link.getRel())){ iconurl = link.getHref(); } } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(); }
注意其中异步加载图片的做法 使用了回调方法 非常好的图片异步加载
package cn.itcast.douban.util; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; /** * * 第一个参数 就是图片下载路径的url * 第二个参数是 下载的进度 * 第三个参数就是异步任务执行完毕后的返回值 * @author Administrator * */ public class LoadImageAsynTask extends AsyncTask<String, Void, Bitmap> { LoadImageAsynTaskCallback loadImageAsynTaskCallback; public LoadImageAsynTask(LoadImageAsynTaskCallback loadImageAsynTaskCallback) { this.loadImageAsynTaskCallback = loadImageAsynTaskCallback; } public interface LoadImageAsynTaskCallback{ public void beforeLoadImage(); public void afterLoadImage(Bitmap bitmap); } /** * 当异步任务执行之前调用 */ @Override protected void onPreExecute() { //初始化的操作具体怎么去实现, LoadImageAsynTask 不知道 // 需要让调用这个 LoadImageAsynTask 的人 去实现 loadImageAsynTaskCallback.beforeLoadImage(); super.onPreExecute(); } /** * 异步任务执行之后调用 */ @Override protected void onPostExecute(Bitmap result) { loadImageAsynTaskCallback.afterLoadImage(result); super.onPostExecute(result); } /** * 后台子线程运行的异步任务 * String... params 可变长度的参数 */ @Override protected Bitmap doInBackground(String... params) { try { String path = params[0]; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is = conn.getInputStream(); return BitmapFactory.decodeStream(is); } catch (Exception e) { e.printStackTrace(); return null; } } }