2013-06-21 28 views
-7

任何人都可以请张贴的源代码创建与图像的URL动态编程的ImageView在android..please帮我如何创建与编程图像的URL中的Android

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
    TextView label = new TextView(this); 
    label.setText(R.string.my_text_label); 
    label.setTextSize(20); 
    label.setGravity(Gravity.CENTER_HORIZONTAL); 
    ImageView images = new ImageView(this); 
    images.setImageResource(R.drawable.raise); 
    // images.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    images.setAdjustViewBounds(true); 
    images.setScaleType(ScaleType.FIT_XY); 
    images.setMaxHeight(250); 
    images.setMaxWidth(250); 
    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    // ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    ll.setGravity(Gravity.CENTER); 
    ll.addView(label); 
    ll.addView(images); 
    setContentView(ll); 

我的代码是静态动态图像视图如何在动态的方式实现..

+0

请先试试自己。 – sebastian

+3

SO是不是提供源代码..它可以帮助你的错误.. – Riser

+0

这可能会帮助你沙..... http://stackoverflow.com/questions/12923903/how-to-populate-dynamically- created-imageview-through-urls-asyncronously-in-an – Aarun

回答

0

编辑:

变量:

private ArrayList<HashMap<String, String>> image_list; 
private Context mContext; 
private ArrayList<String> image_name; 
private static final String TAG_Image_Name = "Your Image Tag Name In the Json File"; 
public static String url_single_image = "Your Single Image Url String"; 
public static String textCard_url = "Url for Your Json File"; 

得到所有的JSON文件图像URL字符串到一个ArrayList中

public void getallthumbimages() { 
    image_list = new ArrayList<HashMap<String, String>>(); 
    image_name = new ArrayList<String>(); 

    JSONArray json = getJSONfromURL(Common.textCard_url); 

    try { 

     for (int i = 0; i < json.length(); i++) { 
      JSONObject c = json.getJSONObject(i); 

      String full_Image_Url = c.getString(TAG_Image_Name); 

      Common.Text_card_name.add(full_Image_Url); 
      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put(TAG_Image_Name, full_Image_Url); 

      image_list.add(map); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

负载单幅图像的ImageView的

public View Load_Image_from_Url(int position) { 
    ImageView imageView = new ImageView(mContext); 

    String url_image_string = url_single_image + "/" 
      + image_name.get(position); 
    imageView 
      .setImageDrawable(LoadImageFromWebOperations(url_image_string)); 

    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 

    return imageView; 
} 

载入图像从网络

public static Drawable LoadImageFromWebOperations(String url) { 
    try { 
     InputStream is = (InputStream) new URL(url).getContent(); 
     Drawable d = Drawable.createFromStream(is, "src name"); 
     return d; 
    } catch (Exception e) { 
     return null; 
    } 
} 

获取JsonArray从网页URL

public static JSONArray getJSONfromURL(String url) { 
    InputStream is = null; 
    String result = ""; 
    JSONArray jArray = null; 

    HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet(url); 
    HttpResponse response = null; 
    try { 
     response = httpclient.execute(httpget); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     HttpEntity entity = response.getEntity(); 

     is = entity.getContent(); 

     BufferedReader reader = null; 

     reader = new BufferedReader(
       new InputStreamReader(is, "iso-8859-1"), 8); 

     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 

     result = sb.toString(); 

     jArray = new JSONArray(result); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return jArray; 
} 

我希望这是你想要的......宰迪马塔.....

+0

嗨,该网址用于静态图像,我想从服务器发布图像.. – Sandy

+0

确定您正在使用webService。所以告诉我你正在使用JSon,XMl等什么合成你正在使用 – Aarun

+0

我使用JSon解析器我不想在xml文件中声明任何东西。我们应该设计图像views..please帮助我.. – Sandy

相关问题