3

我有看法尋呼機內的圖像視圖來顯示一些圖像從URL來作爲顯示在下面的屏幕截圖,的Android - 查看傳呼機內部圖像查看問題

enter image description here 然而,圖像應填寫(應占有完整的視圖尋呼機空間)。但是,在我的情況發生了什麼是圖像顯示在中心,即使當我設置佈局參數填補父母。

我的片段,其中i返回佈局圖像視圖:

ImageView image = new ImageView(getActivity()); 

     ImageLoader imgLoader = new ImageLoader(MainActivity.context); 
     imgLoader.DisplayImage(imagePath, new Activity(), image); 

     LinearLayout layout = new LinearLayout(getActivity()); 
     layout.setLayoutParams(new LayoutParams()); 

     layout.setGravity(Gravity.CENTER); 
     layout.addView(image); 

     return layout; 

視圖尋呼機:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="225dp" 
      android:background="@color/white" 
      android:orientation="vertical" > 

      <android.support.v4.view.ViewPager 
       android:id="@+id/pager" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" /> 
     </LinearLayout> 

當圖像仍然從網址載入我會表現出像動畫一些進展,一旦加載我將設置圖像內的圖像

if (bitmap != null) { 
       imageView.setBackgroundResource(0); 
       imageView.setImageBitmap(bitmap); 

      } else { 
       imageView.setBackgroundResource(R.drawable.view_pager_progress); 
       final AnimationDrawable startAnimation = (AnimationDrawable) imageView 
         .getBackground(); 
       imageView.setScaleType(ScaleType.CENTER);// i tried FIT_XY, dint work 
       startAnimation.start(); 
      } 

總而言之,我想要的是,圖像在視圖內的尋呼機應該在視圖尋呼機的水平和垂直方向上填充整個空間。任何想法如何做到這一點?

+0

親愛的,你怎麼解決這個問題?我有同樣的問題,但與內容使用coordinatorlayout ... – Mateus 2016-01-19 11:20:34

回答

5

嘗試設置ScaleType爲您的ImageView到CENTER_CROP

imageView.setScaleType(CENTER_CROP) 

而且,也許,你需要添加一些佈局PARAMS添加ImageView的到的LinearLayout

+0

CENTER_CROP dint的工作,我甚至嘗試過設置imageview的佈局參數,但沒有運氣到目前爲止 – 2013-04-30 11:12:29

+0

很抱歉聽到這個消息。需要在viewpager中使用線性佈局和圖像視圖的父級佈局參數,最簡單的方法是使用xml layout re資源。然後CENTER_CROP可能會有所幫助。 – httpdispatch 2013-04-30 11:27:04

0

嘗試使用:

image.setAdjustViewBounds(true); 
+0

dint工作:( – 2013-04-30 10:57:53

+0

它改變了什麼嗎?如果不是,請確保視圖的父母或鄰近您的imageView的填充或邊緣沒有干擾你可以嘗試手動添加一個imageView,看看它是如何在編輯器中看到的。 – NoToast 2013-04-30 11:02:21

+0

這裏是完整的xml代碼,用於上述活動。你認爲這裏有什麼問題嗎?http:// pastebin .com/7AAMWuvP – 2013-04-30 11:05:00

1

時@suresh cheemalamudi在你的上面的代碼中給出了你的尋呼機所在的線性佈局的高度,而不是使它匹配parent.And xml代碼應該看起來像l IKE在此之下

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/view_pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

我你的Java代碼指定這樣

imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
0

確定。實際工作的解決方案是:

 ImageView imageView = new ImageView(context); 
     imageView.setLayoutParams(new ViewGroup.LayoutParams(mScreenWidth, ViewGroup.LayoutParams.WRAP_CONTENT)); 
     imageView.setAdjustViewBounds(true); 
     imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 

     Drawable drawable = Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/Android/data/" + getPackageName() + 
       "/"+....+".jpg");   
     if(drawable != null)     
      imageView.setImageDrawable(drawable); 

     ((ViewPager) container).addView(imageView, 0); 


<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/view_pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/black" 
    /> 
+0

mScreenWidth的值是多少? – dmh 2015-09-28 15:18:00

+0

設備屏幕的當前寬度(取決於旋轉)。 – Yar 2015-09-28 15:26:02

+0

你在頁面適配器上使用這個嗎? – dmh 2015-09-28 15:30:01