2016-07-02 12 views
0

enter image description here Ich lade Bild von Server in AsyncTask-Methode. Das Bild wird während des Herunterladens im Vollbildmodus angezeigt, aber nach Abschluss des Downloads wird das Bild in der Mitte des Bildschirms klein. HierBild Größe nach dem Download in Android

ist der Code:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.show_pics); 

    DownloadTask downloadTask = new DownloadTask(); 

    downloadTask.execute("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg"); 
} 

private Bitmap downloadUrl(String strUrl) throws IOException { 
    Bitmap bitmap = null; 
    InputStream iStream = null; 
    try { 
     URL url = new URL(strUrl); 
     /** Creating an http connection to communcate with url */ 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

     /** Connecting to url */ 
     urlConnection.connect(); 

     /** Reading data from url */ 
     iStream = urlConnection.getInputStream(); 

     /** Creating a bitmap from the stream returned from the url */ 
     bitmap = BitmapFactory.decodeStream(iStream); 

    } catch (Exception e) { 
     //Log.d("Exception while downloading url", e.toString()); 
    } finally { 
     iStream.close(); 
    } 
    return bitmap; 
} 

private class DownloadTask extends AsyncTask<String, Integer, Bitmap> { 
    Bitmap bitmap = null; 

    @Override 
    protected Bitmap doInBackground(String... url) { 
     try { 
      bitmap = downloadUrl(url[0]); 
     } catch (Exception e) { 
      //Log.d("Background Task",e.toString()); 
     } 
     return bitmap; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     /** Getting a reference to ImageView to display the 
     * downloaded image 
     */ 
     ImageView iView = (ImageView) findViewById(R.id.TahlilImageView); 

     /** Displaying the downloaded image */ 
     iView.setImageBitmap(result); 

     /** Showing a message, on completion of download process */ 
     Toast.makeText(getBaseContext(), "Image downloaded successfully", Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

Geben Sie Ihren XML-Code ein – sJy

+0

Wenn das Bild heruntergeladen wird, haben Sie noch kein Bild, also wie ist es möglich, es im Vollbildmodus anzuzeigen. Sind Sie sicher, dass Sie nicht das gleiche Bild als Hintergrund für die Bildansicht statisch in XML oder Java-Code gesetzt haben. – Tony

+0

Ja das war mein Fehler. Ich hatte es eingestellt. Ich entfernte es und während des Downloads wird nichts angezeigt und wenn der Download abgeschlossen ist, wird das Bild in der Mitte angezeigt und es ist nicht im Vollbildmodus @Tony – moji

Antwort

2

Bitte werfen Sie einen Blick auf die folgende URL in Bezug auf die ImageView.ScaleType

Die Scaletype gibt Ihnen Optionen für die Skalierung der Grenzen eines Bildes an die Grenzen des Image . Sie können entweder den XML Throught durch das folgende Attribut erklärt:

android:scaleType="fitXY" 

Oder man kann es dynamisch im Code festgelegt:

iView.setScaleType(ImageView.ScaleType.FIT_XY); 

Wenn Sie das Bild zu füllen der ganze Raum wollen von ImageView müssen Sie FIT_XY auswählen. Im Allgemeinen haben Sie viele Möglichkeiten, und Sie können mit ihnen auskommen. Und als Ergebnis, dasjenige auszuwählen, das Ihrem Bedarf am besten entspricht.

Verwandte Themen