ITeye博客

Android Jamendo开源在线音乐播放器源码分析九 ViewFlipper及自定义布局控件的分析

bolutes · 2011-12-02 10:59 · Markdown

在之前分析过这个ViewFlipper代码:

备注一:ViewFlipper

界面的最上面的mViewFlipper中包含了mGallery、mProgressBar、mFailureBar,刚开始以为最上面部分的显示是通过不同状态下设置View的visibility的VISIBLE,INVISIBLE, orGONE.来显示的,看完代码之后才知道是用的ViewFlipper这个空间实现的,代码显得更加清楚。

view plain

  1. <com.teleca.jamendo.util.FixedViewFlipper
  2. android:orientation="vertical"android:id="@+id/ViewFlipper"
  3. android:layout_width="fill_parent"android:layout_height="75dip"
  4. android:background="@drawable/gradient_dark_purple">
  5. <!--(0)Loading-->
  6. <LinearLayoutandroid:orientation="vertical"
  7. android:layout_width="fill_parent"android:layout_height="fill_parent"
  8. android:layout_marginLeft="15dip"android:gravity="left|center_vertical">
  9. <com.teleca.jamendo.widget.ProgressBar
  10. android:id="@+id/ProgressBar"android:layout_width="wrap_content"
  11. android:layout_height="wrap_content">
  12. </com.teleca.jamendo.widget.ProgressBar>
  13. </LinearLayout>
  14. <!--(1)Gallery-->
  15. <LinearLayoutandroid:orientation="vertical"
  16. android:layout_width="fill_parent"android:layout_height="fill_parent"
  17. android:gravity="center">
  18. <Galleryandroid:id="@+id/Gallery"android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"android:spacing="0px"/>
  20. </LinearLayout>
  21. <!--(2)Failure-->
  22. <LinearLayoutandroid:orientation="vertical"
  23. android:layout_width="fill_parent"android:layout_height="fill_parent"
  24. android:layout_marginLeft="15dip"android:gravity="left|center_vertical">
  25. <com.teleca.jamendo.widget.FailureBar
  26. android:id="@+id/FailureBar"android:layout_width="wrap_content"
  27. android:layout_height="wrap_content">
  28. </com.teleca.jamendo.widget.FailureBar>
  29. </LinearLayout>
  30. </com.teleca.jamendo.util.FixedViewFlipper>

在onCreate()里面启动一个AsyncTask来加载,根据不同的结果决定显示ViewFlipper的哪部分内容

view plain

  1. privateclassNewsTaskextendsAsyncTask<Void,WSError,Album[]>{
  2. @Override
  3. publicvoidonPreExecute(){
  4. mViewFlipper.setDisplayedChild(0);
  5. mProgressBar.setText(R.string.loading_news);
  6. super.onPreExecute();
  7. }
  8. @Override
  9. publicAlbum[]doInBackground(Void...params){
  10. JamendoGet2Apiserver=newJamendoGet2ApiImpl();
  11. Album[]albums=null;
  12. try{
  13. albums=server.getPopularAlbumsWeek();
  14. }catch(JSONExceptione){
  15. e.printStackTrace();
  16. }catch(WSErrore){
  17. publishProgress(e);
  18. }
  19. returnalbums;
  20. }
  21. @Override
  22. publicvoidonPostExecute(Album[]albums){
  23. if(albums!=null&&albums.length>0){
  24. mViewFlipper.setDisplayedChild(1);
  25. ImageAdapteralbumsAdapter=newImageAdapter(HomeActivity.this);
  26. albumsAdapter.setList(albums);
  27. mGallery.setAdapter(albumsAdapter);
  28. mGallery.setOnItemClickListener(mGalleryListener);
  29. mGallery.setSelection(albums.length/2,true);//animatetocenter
  30. }else{
  31. mViewFlipper.setDisplayedChild(2);
  32. mFailureBar.setOnRetryListener(newOnClickListener(){
  33. @Override
  34. publicvoidonClick(Viewv){
  35. newNewsTask().execute((Void)null);
  36. }
  37. });
  38. mFailureBar.setText(R.string.connection_fail);
  39. }
  40. super.onPostExecute(albums);
  41. }
  42. @Override
  43. protectedvoidonProgressUpdate(WSError...values){
  44. Toast.makeText(HomeActivity.this,values[0].getMessage(),Toast.LENGTH_LONG).show();
  45. super.onProgressUpdate(values);
  46. }
  47. }

在这个AsyncTask中进行专辑的加载,在加载时onPreExecute()中mViewFlipper.setDisplayedChild(0);也就是上面xml代码中的(0) Loading部分,然后在doInBackground()中进行专辑的加载,当加载完之后onPostExecute(Album[] albums),然后根据加载专辑是否成功,选择相应的界面,是显示加载成功之后的(1) Gallery还是加载失败之后的(2) Failure。其实这里关键是这个空间ViewFlipper,在API中的解释是:

android.widget.ViewFlipper

SimpleViewAnimatorthat will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval.

备注二:自定义布局、控件

在com.teleca.jamendo.widget这个包中都是一些自定义的布局或者组件,这些布局和组件都是可以复用的。

ProgressBar

这个是当程序加载的时候显示的

progress_bar.xml

我使用的时候只要mProgressBar.setText(R.string.loading_news);就可以设置他显示的文字信息

RemoteImageView

正如作者所说ImageView extended class allowing easy downloadingof remote images。这个很好的获取图片的封装类,使我想显示图片的时候不用关心怎么从网络获取图片以及怎么生成图片,我只要将图片的URL地址传进来就可以了,获取完图片调用父类ImageView的setImageBitmap就可以显示想要的图片了。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。