2013-10-04 50 views
1

我使用ImageView來顯示圖片,我希望圖片以真實尺寸顯示,但是我希望圖片可以完整顯示在圖片中。我設置了其他scaleType,比如'centerInside',那麼圖片不能在rect中顯示,但'center'可以。我想知道如何編碼,可以顯示一個小於自身的矩形圖片。如何使用android:scaleType來顯示圖片

<ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="20.0dip" 
     android:layout_height="20.0dip" 
     android:adjustViewBounds="true" 
     android:contentDescription="@string/boy" 
     android:paddingLeft="20.0dip" 
     android:paddingTop="40.0dip" 
     android:scaleType="center" 
     android:src="@drawable/boy" /> 
+0

你已經回答了你的問題'「但‘中心’可以」' – Houcine

+0

「的執行情況中心'只是顯示圖片的一部分,而不是完整的圖片。 – user2682897

+0

嘗試'android:scaleType =「centerCrop」' – Houcine

回答

0

根據ImageView scale type documentation,centerInside也包括大小的填充。

均勻地縮放圖像(保持圖像的寬高比),使圖像的兩個尺寸(寬度和高度)等於或小於視圖的相應尺寸(減去填充)。

由於您的padding相同/比ImageView的規模越大,則圖像無法顯示(實際上,它的外邊界推)。使用android:scaleType="centerInside"並考慮使用android:layout_margin而不是android:padding

+0

您的建議很有用。 – user2682897

0

兩個options.-

  • 如果drawable有你ImageView的相同的寬高比,設置scaleTypefitXY
  • 否則,我會去centerCrop

以防萬一你不知道,更多關於scaleTypeshere

+0

我知道這個網站,並且讀了它,但是我不知道爲什麼使用其他的scaleType不能在rect中顯示圖片。 – user2682897

0

應該使用android:scaleType="centerInside"

public static final ImageView.ScaleType CENTER_INSIDE

縮放圖像均勻(保持圖像的縱橫比),以使圖像的兩個尺寸(寬度和高度)將等於或小於視圖的相應尺寸(減去填充)。圖像然後居中在視圖中。從XML中,使用以下語法:android:scaleType =「centerInside」。

看到here的細節

0

正如我已瞭解你有問題,使用scaleType與ImageView的。將ImageView放入(例如)FrameLayout中,並檢查我的示例。

第一個例子:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView android:layout_width="200dp" 
       android:layout_height="200dp" 
       android:layout_gravity="center" 
       android:src="@drawable/something" 
       android:scaleType="centerInside"> 
    </ImageView> 
</FrameLayout> 

二(全屏縮放):

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:src="@drawable/something" 
       android:scaleType="centerInside"> 
    </ImageView> 
</FrameLayout> 
0

具有圓角試試圖像@http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

實測值以下@How to make an ImageView with rounded corners?

http://ruibm.com/?p=184

public class ImageHelper { 
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap 
      .getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
    final RectF rectF = new RectF(rect); 
    final float roundPx = pixels; 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 

    return output; 
} 
} 

您還可以查看演示的爲RoundCornerImageView

0

使用scaleType =「fitXY」,而不是中心