如何在android中更改4个图像从url加载?

时间:2016-05-10 17:22:42

标签: android

我有四个按钮....                                                                                             每个按钮都是一张图片....                                                                                               我按下下一个按钮时想改变图片,,,                                                                                                  怎么办呢?因为我的项目是关于更改从url>>>>>>加载的10张照片,感谢您的帮助:)

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                 new LoadImage().execute("http://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");
              new LoadImag().execute("http://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");
              new LoadIm().execute("http://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");
              new LoadIma().execute("http://pbs.twimg.com/profile_images/630285593268752384/iD1MkFQ0.png");


            }
        });


    }

    private class LoadImage extends AsyncTask<String, String, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading Image ....");
            pDialog.show();

        }
         protected Bitmap doInBackground(String... args) {
             try {
                   bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());

            } catch (Exception e) {
                  e.printStackTrace();
            }
            return bitmap;
         }

         protected void onPostExecute(Bitmap image) {

             if(image != null){
             img.setImageBitmap(image);


             pDialog.dismiss();

             }else{

             pDialog.dismiss();
             Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();

             }
         }
     }
    private class LoadImag extends AsyncTask<String, String, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading Image ....");
            pDialog.show();

        }
         protected Bitmap doInBackground(String... args) {
             try {
                   bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());

            } catch (Exception e) {
                  e.printStackTrace();
            }
            return bitmap;
         }

         protected void onPostExecute(Bitmap image) {

             if(image != null){
             b1.setImageBitmap(image);


             pDialog.dismiss();

             }
         }
     }
    private class LoadIm extends AsyncTask<String, String, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading Image ....");
            pDialog.show();

        }
         protected Bitmap doInBackground(String... args) {
             try {
                   bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());

            } catch (Exception e) {
                  e.printStackTrace();
            }
            return bitmap;
         }

         protected void onPostExecute(Bitmap image) {

             if(image != null){
             b2.setImageBitmap(image);


             pDialog.dismiss();

             }
         }
     }
    private class LoadIma extends AsyncTask<String, String, Bitmap> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading Image ....");
            pDialog.show();

        }
         protected Bitmap doInBackground(String... args) {
             try {
                   bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());

            } catch (Exception e) {
                  e.printStackTrace();
            }
            return bitmap;
         }

         protected void onPostExecute(Bitmap image) {

             if(image != null){
             b3.setImageBitmap(image);


             pDialog.dismiss();

             }
         }
     }
}

1 个答案:

答案 0 :(得分:1)

您可以使用Picasso清理代码。

你只需要这一行:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);