2017-03-01 51 views
0

我有多个图像网址,我需要将所有此图像转换为android中的图像滑块。我有多个图像网址,我需要将所有此图像转换为android的图像滑块

例如,我有两个图像:

enter image description here

enter image description here

,需要这些图像动态地添加到图像滑块。

+0

真好!现在我们该如何帮助你?请将此添加到您的问题中。 – Olayinka

+0

试着在你身边实施...有很多可用的例子,执行它们......并且比你在遇到任何问题之前都不让我们知道SO –

+0

请发布你试过的东西 – Massimo

回答

0

你可以使用这个库的图像加载到一个滑块:https://github.com/daimajia/AndroidImageSlider

在您的布局:

<com.daimajia.slider.library.SliderLayout 
     android:id="@+id/slider" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
/> 

然后输入:

 mDemoSlider = (SliderLayout)findViewById(R.id.slider); 

     HashMap<String,String> url_maps = new HashMap<String, String>(); 
     url_maps.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg"); 
     url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png"); 
     url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg"); 
     url_maps.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg"); 

     for(String name : url_maps.keySet()){ 
      TextSliderView textSliderView = new TextSliderView(this); 
      // initialize a SliderLayout 
      textSliderView 
        .description(name) 
        .image(url_maps.get(name)) 
        .setScaleType(BaseSliderView.ScaleType.Fit) 
        .setOnSliderClickListener(this); 

      //add your extra information 
      textSliderView.bundle(new Bundle()); 
      textSliderView.getBundle() 
         .putString("extra",name); 

      mDemoSlider.addSlider(textSliderView); 
     } 
     mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion); 
     mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom); 
     mDemoSlider.setCustomAnimation(new DescriptionAnimation()); 
     mDemoSlider.setDuration(4000); 
     mDemoSlider.addOnPageChangeListener(this); 
     ListView l = (ListView)findViewById(R.id.transformers); 
     l.setAdapter(new TransformerAdapter(this)); 
     l.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       mDemoSlider.setPresetTransformer(((TextView) view).getText().toString()); 
       Toast.makeText(MainActivity.this, ((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
1

中的AsyncTask的doInBackground试试这个()方法

InputStream in = new URL(IMAGE_URL).openStream(); 
Bitmap bmp = BitmapFactory.decodeStream(in); 

和onPostExecute()方法将它设置为图像视图

imageView.setImageBitmap(bmp); 

OR试试这个:

private Bitmap bmp; 
new AsyncTask<Void, Void, Void>() { 
    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      InputStream in = new URL(IMAGE_URL).openStream(); 
      bmp = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     if (bmp != null) 
      imageView.setImageBitmap(bmp); 
    } 

}.execute(); 
+0

谢谢你们工作良好 –