0

I've googled a lot finding a lot of solutions for problems similar to mine but not equal.

I have to show a jpg file (as an Ad banner not full screen) that is stored on a server (internet). I've tried using WebView, but, when the screen width is bigger than the image width, the image appears smaller than the screen.

Is it possible to use ImageView instead of WebView? If 'Yes' how can I scale the downloaded image to fit different screen resolutions?

Thanks in advance.

Here the solution find there https://stackoverflow.com/a/9288544/2252143:

MainActivity.java:

public class MainActivity extends Activity {

    ImageView imgProva = null;
    FunzioniUtili Funzioni = new FunzioniUtili();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgProva = (ImageView)findViewById(R.id.imgProva);

        // show The Image
        new DownloadImageTask((ImageView) findViewById(R.id.imgProva)).execute("http://www.softlive.net/advs/banner_adolfo.jpg");
    }

    public void onClick(View v) {
        startActivity(new Intent(this, MainActivity.class));
        finish();

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
        int DisplayWidth = 0;
        DisplayWidth  = Funzioni.ScreenWidth (MainActivity.this);

        double CardResizeFactor=1.0;
        //This is the function that I use to resize... / CardResizer is redundant, I know!
        Funzioni.scaleImage(bmImage,(int)(DisplayWidth / CardResizeFactor),0,0);
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

My resize function:

/**
 * public void scaleImage(ImageView view, int boundBoxInDp)
 * 
 * Resize the given ImageView.
 *
 * @param view The ImageView.
 * @param boundBoxInDp   Scaling factor in Dp.
 * @param layoutType 0 = RelativeLayout 1 = LinearLayout 2 = TableLayout 3 = TableRow 4 = FrameLayout.
 * @param colNumber Column number if the selected layout is TableRow. If other, put 0
 */
public void scaleImage(ImageView view, int boundBoxInDp, int layoutType, int colNumber)
{
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();

    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    if(layoutType == 0)
    {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;

        view.setLayoutParams(params);
    }
    else if(layoutType == 1)
    {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }
    else if(layoutType == 2)
    {
        TableLayout.LayoutParams params = (TableLayout.LayoutParams) view.getLayoutParams();

        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }
    else if(layoutType == 3)
    {
        TableRow.LayoutParams params = (TableRow.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        params.column = colNumber;
    }
    else if(layoutType == 4)
    {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }
}

Here my Activity_Main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/imgProva"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
Community
  • 1
  • 1
Massimo Costanzo
  • 382
  • 3
  • 18
  • `imageView.setScaleType()`. Did you even try reading the documentation? – Simon Oct 26 '13 at 19:40
  • @Simon Yes, but only God knows everything inside android documentation if never used before...first of all I would like to know the better way to display an image from web (ImageView or WebView)... then...Using setScaleType, to fit the width maintaining the aspect ratio, is it better to use FIT_END or FIT_START? – Massimo Costanzo Oct 26 '13 at 20:18
  • Finally I've found the solution and added at the end of my question. Thanks. – Massimo Costanzo Oct 27 '13 at 11:15

3 Answers3

0

you can set the width of the image to 100% with css

see http://www.w3schools.com/cssref/pr_dim_width.asp

Kirill Kulakov
  • 10,035
  • 9
  • 50
  • 67
0

I am using this to display an png image. It will stretch the image to fill the screen vertically keeping its aspect ratio. It seems to only work on API 11+ tho. It's in a RelativeLayout.

<ImageView
    android:id="@+id/main_imageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/img_alchemy2" />
Rick Falck
  • 1,778
  • 3
  • 15
  • 19
0

Ok, I've found the solution and I've edited the question adding the code. Thanks.

Massimo Costanzo
  • 382
  • 3
  • 18