2014-06-19 34 views
0
顯示URL圖像

最後,我改變我的代碼,並使用萬能圖像裝載機庫如何使用的AsyncTask在PaperAdapter

我用paperadapter像代碼顯示網址的圖像,它工作正常。

@Override 
public Object instantiateItem(final ViewGroup container, final int position) { 
Drawable[] imageDrawable = new Drawable[3]; 
for (int i = 0; i < 3; i++) { 
imageDrawable[i] = LoadImageFromWebOperations(server_url+ image_name.replace(" ","")+ "_0" +String.valueOf(i + 1) + ".jpg"); 
} 
imageView = new ImageView(mContext); 
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
imageView.setImageDrawable(imageDrawable[position]); 
container.addView(imageView, 0); 
return imageView; 
} 

,但我想用的AsyncTask要做到這一點,如代碼

@Override 
public Object instantiateItem(final ViewGroup container, final int position) { 

    AsyncTask<String, Void, Drawable[]> loadingImage = new AsyncTask<String, Void, Drawable[]>(){ 

     @Override 
     protected Drawable[] doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      Drawable imageDrawable[] = new Drawable[3]; 
      for (int i = 0; i < 3; i++) { 
       imageDrawable[i] = LoadImageFromWebOperations(server_url 
         + image_name.replace(" ","")+ "_0" + String.valueOf(i + 1) + ".jpg"); 
       System.out.println("doInBackground="+position); 
      } 
      return imageDrawable; 
     } 
     @Override 
     protected void onPostExecute(Drawable[] result) { 
      System.out.println("onPostExecute="+position); 
      imageView = new ImageView(getBaseContext()); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setImageDrawable(result[position]); 
      container.addView(imageView, 0); 
       } 
      }); 
     } 

    }; loadingImage.execute(); 
    return imageView; 
} 

它不工作fine.i發現該位置「1」爲空沒有圖像,但位置0 2不爲空,等待一些建議,謝謝!

+0

爲什麼你還要加載多個圖像,每頁的循環?上面哪個代碼是你想要使用的代碼,你想要做什麼? –

+0

@androiddeveloper感謝您的答覆。我使用viewpaper指示器顯示來自url的圖像。使用循環,因爲3個圖像具有相似的文件名,只是不同的數字 – Giggle

+0

,但instantiateItem並不意味着你目前在這個頁面上。它只是加載頁面。例如,如果您轉到第1頁,這意味着頁面0,1,2需要調用此功能(以便用戶在屏幕滾動時能夠看到它們)。如果您希望監聽正在更改的頁面,則需要調用setOnPageChangeListener。 instantiateItem用於初始化頁面,類似於AdapterView的getView(但沒有重新使用視圖)。另外,請回答我問過的問題(或者修改你的帖子,哪個更好),以便我可以幫助你... –

回答

0

這是所有代碼:

public class ProductDetailActivity1 extends FragmentActivity { 

     ImageAdapter mAdapter; 
     ViewPager mPager; 
     CirclePageIndicator mIndicator; 
     private String server_url; 
     ImageView imageView;  
     protected ScrollView mScrollView; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_product_detail1); 

      mScrollView = (ScrollView)findViewById(R.id.card_scrollview); 


      getActionBar(); 
      getActionBar().setHomeButtonEnabled(true); 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
      getActionBar().setDisplayShowTitleEnabled(true); 
      getActionBar().setTitle(R.string.product_detail); 

      Intent intent = getIntent(); 
      server_url = intent.getStringExtra("SERVER_URL"); 

      mAdapter = new ImageAdapter(getApplicationContext()); 
      mPager = (ViewPager) findViewById(R.id.imageViewpager); 
      mPager.setAdapter(mAdapter); 


      mIndicator = (CirclePageIndicator) findViewById(R.id.indicator); 
      mIndicator.setViewPager(mPager); 
      mIndicator.setFillColor(0xFFFFFFFF); 
      mIndicator.setStrokeColor(0xFFFFFFFF); 
      mIndicator.setStrokeWidth(1); 
      mIndicator.setRadius(6 * getResources().getDisplayMetrics().density); 
      ((CirclePageIndicator) mIndicator).setSnap(true); 
      mIndicator 
        .setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
         @Override 
         public void onPageSelected(int position) { 
         } 

         @Override 
         public void onPageScrolled(int position, 
           float positionOffset, int positionOffsetPixels) { 
         } 

         @Override 
         public void onPageScrollStateChanged(int state) { 
         } 
        }); 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 

      switch (item.getItemId()) { 
      case android.R.id.home: 
       Intent intent = new Intent(ProductDetailActivity1.this, 
         com.wangrui.ams.MainActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(intent); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
      } 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.product_detail, menu); 
      return true; 
     } 

     public class ImageAdapter extends PagerAdapter { 
      private Context mContext; 
      private Drawable[] imageDrawable = new Drawable[3];; 

      public ImageAdapter(Context cx) { 
       mContext = cx.getApplicationContext(); 
      } 

      @Override 
      public Object instantiateItem(final ViewGroup container, final int position) { 
       AsyncTask<Void, Void, Drawable[]> loadingImage = new AsyncTask<Void, Void, Drawable[]>(){ 

        @Override 
        protected Drawable[] doInBackground(Void... params) { 
         // TODO Auto-generated method stub 

         for (int i = 0; i < 3; i++) { 
          imageDrawable[i] = LoadImageFromWebOperations(server_url 
            + image_name.replace(" ","")+ "_0" + String.valueOf(i + 1) + ".jpg"); 
          System.out.println("position="+i); 
         } 
         return imageDrawable; 
        } 
        @Override 
        protected void onPostExecute(Drawable[] result) { 
         imageView = new ImageView(mContext); 
         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
        imageView.setImageDrawable(result[position]); 
         mPager.getAdapter().notifyDataSetChanged(); 
         ((ViewPager) container).addView(imageView, position); 

        }    
       }; loadingImage.execute(); 

       return imageView; 
      } 

      @Override 
      public int getCount() { 
       // TODO Auto-generated method stub 
       return 3; 
      } 

      @Override 
      public boolean isViewFromObject(final View view, final Object object) { 
       // TODO Auto-generated method stub 
       //return arg0 == (View) arg1; 
       return view == ((ImageView) object); 
      } 

      @Override 
      public void destroyItem(ViewGroup container, int position, Object object) { 
       container.removeView((ImageView) object); 
      } 

     } 

     private void showAlertMessage(String msg){ 

      SuperToast superToast = new SuperToast(getApplicationContext()); 
      superToast.setAnimations(SuperToast.Animations.FLYIN); 
      superToast.setDuration(SuperToast.Duration.LONG); 
      superToast.setBackground(SuperToast.Background.RED); 
      superToast.setTextSize(SuperToast.TextSize.EXTRA_SMALL); 
      superToast.setText(msg); 
      superToast.show(); 
     } 

     private Drawable LoadImageFromWebOperations(String url) { 
      try { 
       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
         .permitAll().build(); 
       StrictMode.setThreadPolicy(policy); 
       InputStream is = (InputStream) new URL(url).getContent(); 
       Drawable d = Drawable.createFromStream(is, "src name"); 
       return d; 
      } catch (Exception e) { 
       System.out.println("Exc=" + e); 
       return null; 
      } 
     } 
    } 
+0

你爲什麼把它寫在答案中,而不是編輯你原來的問題?此外,您仍然沒有清楚說明問題所在以及您正在嘗試做什麼。你是否希望每頁顯示一個圖像,當它從互聯網上下載時會顯示出來? –

+0

@androiddeveloper對不起我的錯誤,我是一個beginner.yes,我想在每頁使用viewpaper指標庫顯示不同的圖像,只顯示,不下載。 – Giggle

+0

如果一切都是本地的,爲什麼你有URL和名爲「LoadImageFromWebOperations」的函數?無論如何,如果文件在應用程序中,您可以使用「getIdentifier」,如下所示:http://stackoverflow.com/a/3476470/878126然後調用getResources()。getDrawable(...)從中獲利。 –