如何在android中以编程方式创建带有图像URL的动态图像视图

时间:2013-06-21 06:59:06

标签: android

任何人都可以发布源代码,用于在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); 

我的代码是静态如何以动态方式实现..

1 个答案:

答案 0 :(得分: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";
  

将所有图像Url字符串从Json文件转换为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

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;
}

我希望这是你想要的...... Jai Mata Di .....