2013-10-26 49 views
0

我GOOGLE了很多尋找類似於我的問題,但不相同的問題的很多解決方案。ImageView或WebView ...這是

我必須顯示存儲在服務器(互聯網)上的jpg文件(作爲廣告橫幅非全屏)。 我試過使用WebView,但是當屏幕寬度大於圖像寬度時,圖像顯示比屏幕小。

是否有可能使用ImageView而不是WebView? 如果'是'我如何縮放下載的圖像以適應不同的屏幕分辨率?

在此先感謝。

這裏的解決方案發現有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; 
} 

} 

我調整大小功能:

/** 
* 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); 
    } 
} 

這裏我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> 
+0

'imageView.setScaleType()'。你甚至嘗試閱讀文檔? – Simon

+0

@Simon是的,但只有上帝知道android文檔中的所有內容,如果以前從未使用過...首先,我想知道從web(ImageView或WebView)顯示圖像的更好方法...然後...使用setScaleType,以適應保持寬高比的寬度,使用FIT_END還是FIT_START更好? –

+0

最後我找到了解決方案,並在我的問題結尾添加。謝謝。 –

回答

0

好的,我找到了解決方案,我編輯了添加代碼的問題。 謝謝。

0

可以與CSS的圖像的寬度設定爲100%

見我使用此,以顯示PNG圖像 http://www.w3schools.com/cssref/pr_dim_width.asp

+0

好吧,我認爲我必須傳遞給CSS屏幕寬度。這是真的嗎?但是我怎樣才能在我的代碼中使用CSS? –

+0

在行css或樣式標籤 –

+1

最後我找到了解決方案,並添加在我的問題結束。謝謝。 –

0

。它將拉伸圖像以垂直填充屏幕,保持其縱橫比。它似乎只適用於API 11 + tho。它在一個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" /> 
+0

這是一個正常的ImageView在XML文件...沒有別的。它也適用於較舊的API。我需要保持縱橫比只填充寬度而不填充父項。 –

+0

最後,我找到了解決方案,並在我的問題結尾添加。謝謝。 –