Android位图将小图像调整为imageview

时间:2014-01-31 05:13:00

标签: android bitmap android-imageview

我需要在我的imageview中显示一个位图。我从服务器获取位图,我想正确地将图像放入我的imageview中。我怎样才能做到这一点??我试过了,但没有正确显示。

代码:

public class ImageTask extends AsyncTask<String, Integer, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap event_Image = null;
        String urldisplay = urls[0];

        try {
            InputStream in = new java.net.URL(urldisplay.toString().trim())
                    .openStream();
            event_Image = BitmapFactory.decodeStream(in);

        } catch (Exception e) {

            Log.e("Error", "Error while getting image  " + e.toString());
            // e.printStackTrace();
        }
        return event_Image;
    }

    public Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
        int width = image.getWidth();
        int height = image.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // create a matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
                matrix, false);
        return resizedBitmap;
    }


new ImageTask() {
                @Override
                protected void onPostExecute(Bitmap result) {
                    if (result != null) {

                        // Rescaling event details image view
                        Bitmap resized = this.getResizedBitmap(result,
                                eventDetails_Image.getWidth(),
                                eventDetails_Image.getHeight());

                        eventDetails_Image.setImageBitmap(resized);
                    }
                }
            }.execute(mEventDetailsUtil1.getEvent_image_url());

2 个答案:

答案 0 :(得分:0)

您可以使用imageview的刻度类型属性来执行此操作。检查this

答案 1 :(得分:0)

//您可以在libs文件夹中使用android查询添加aquery库

public void getImage()
        {
            AQuery aq = new AQuery("Your Class Name");
            String url = "Your Server Url"
            aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() 
            {
            public void callback(String url,JSONObject  jsonobject, AjaxStatus  status) 
                    {       
                      try 
                       {
                                    JSONArray jsonarray = jsonobject.getJSONArray("images");
                                    JSONObject robject = null;
                                       image_array.clear();
                                        for(int i=0;i<jsonarray.length();i++)
                                        {
                                                robject=jsonarray.getJSONObject(i);
                                                image_array.add("Your Image Path"));
                                        }

                       }       
                       catch (JSONException e1)
                       {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                       }
                    }
              });

        }//get Image method close

    public void insertImage()
    {

            for(int j=0; j<image_array.size();j++)
            {       

                      aq.ajax(image_array.get(j),Bitmap.class,new AjaxCallback<Bitmap>()
                       {
                                   public void callback(String url, Bitmap object, AjaxStatus status) 
                                   {
                                         super.callback(url, object, status);
                                         try 
                                         {
                                             imageview.setImageBitamp(Bitmap);

                                        }
                                        catch (Exception e)
                                        {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                    }
                        });
              } 
         }`enter code here`
     }