48

如何使背景圖像適合視圖,但在使用<bitmap />作爲背景可繪製xml時保持其縱橫比? 沒有<bitmap>android:gravity值給出所需的效果。縮放圖像保持背景縱橫比可繪製

+1

你是否已經嘗試過[ScaleType]任何規模的邏輯(http://developer.android.com/reference /android/widget/ImageView.ScaleType.html)? – 2012-03-27 14:03:46

+1

嘗試android:scaleType =「matrix」 – Pavandroid 2012-03-27 14:52:24

+1

ScaleType用於ImageView。位圖不支持它。 – OldSchool4664 2016-12-01 01:28:16

回答

48

只能在xml文件中實現操縱背景屬性是不可能的。有兩個選項:

  1. 你砍/分的位圖編程方式與 Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)並將其設置爲一些View的背景。

  2. 您使用ImageView而不是背景把它作爲第一個佈局的元素,併爲它指定android:scaleType屬性:

    <RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" > 
    
        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
    
         android:src="@drawable/backgrnd" 
         android:scaleType="centerCrop" /> 
    
        ... 
    
        rest layout components here 
    
        ... 
    
    </RelativeLayout> 
    
+6

ScaleType的'centerCrop'選項對此非常有用。 – 2012-03-27 14:52:10

+2

所有可能的'android:scaleType'值都不會提供所需的效果。僅當我沒有指定'scaleType'時,圖像才符合視圖的縱橫比。 – tilex 2012-03-28 07:25:18

+0

謝謝,centerInside適合我。 – pengguang001 2016-08-25 01:10:43

2

使用a.ch描述的方法,除了我用這對我需要什麼工作要好得多這個規模類型爲我工作的偉大

+1

,在這裏您可以看到每種類型的一些示例:http://etcodehome.blogspot.de/2011/05/android-imageview-scaletype-samples.html – 2013-09-27 12:09:46

29

有一個簡單的方法來從繪製這樣做:

your_drawable.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item android:drawable="@color/bg_color"/> 
    <item> 
     <bitmap 
      android:gravity="center|bottom|clip_vertical" 
      android:src="@drawable/your_image" /> 
    </item> 

</layer-list> 

唯一的缺點是,如果沒有足夠的空間,您的圖像將不會完全顯示,但會被剪切,我無法找到一種方式直接從drawable中執行此操作。但是從測試中我發現它工作得很好,而且它不會剪切那麼多圖像。你可以玩更多的重力選項。

另一種方式將是剛剛創建的佈局,在那裏你會使用ImageView並設置scaleTypefitCenter

希望這些信息可以幫助你實現你想要的。

1

嘗試使用InsetDrawable(對我很好)。

只要給你這個可繪製的,並且從四邊中的任何一邊你想要的插入(或填充)。

InsetDrawable insetDrawable = new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom); 

它是專門用於設置大小小於視圖的背景可繪製。

在這裏看到: https://developer.android.com/reference/android/graphics/drawable/InsetDrawable.html

1

老問題,但沒有其他的答案爲我工作。然而,這種XML代碼所做的:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:scaleType="centerCrop" 
     android:src="@drawable/background_image"/> 

</RelativeLayout> 
1

如果您的位圖的寬度大於高,使用android:gravity="fill_vertical"。否則,請使用android:gravity="fill_horitzonal"。這與在ImageView上使用android:scaleType="centerCrop"具有相似的效果。

<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:gravity="fill_vertical" 
    android:src="@drawable/image" /> 

如果您支持多個方向,您可以創建在drawable-port文件夾和一個位圖的XML文件,另一個在drawable-land文件夾中。

1

我想在我的自定義Drawable類中做類似的事情。 這裏是重要的部分:

public class CustomBackgroundDrawable extends Drawable 
{ 
    private Rect mTempRect = new Rect(); 
    private Paint mBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 

    ... 

public void draw(@NonNull Canvas canvas) 
{ 
    Rect bounds = getBounds(); 
    if (mBitmap != null) { 
     if (mScaleType == ScaleType.SCALE_FILL) { 
      //bitmap scales to fill the whole bounds area (bitmap can be cropped) 
      if (bounds.height() > 0 && bounds.height() > 0) { 
       float scale = Math.min(mBitmap.getWidth()/(float)bounds.width(), mBitmap.getHeight()/(float)bounds.height()); 

       float bitmapVisibleWidth = scale * bounds.width(); 
       float bitmapVisibleHeight = scale * bounds.height(); 

       mTempRect.set((int)(mBitmap.getWidth()-bitmapVisibleWidth)/2, 0, (int)(bitmapVisibleWidth+mBitmap.getWidth())/2, (int)bitmapVisibleHeight); 
       canvas.drawBitmap(mBitmap, mTempRect, bounds, mBitmapPaint); 
      } 
     } else if (mScaleType == ScaleType.SCALE_FIT) { 
      //bitmap scales to fit in bounds area 
      if (bounds.height() > 0 && bounds.height() > 0) { 
       float scale = Math.min((float)bounds.width()/mBitmap.getWidth(), (float)bounds.height()/mBitmap.getHeight()); 

       float bitmapScaledWidth = scale * mBitmap.getWidth(); 
       float bitmapScaledHeight = scale * mBitmap.getHeight(); 
       int centerPadding = (int)(bounds.width()-bitmapScaledWidth)/2; 
       mTempRect.set(bounds.left + centerPadding, bounds.top, bounds.right - centerPadding, bounds.top+(int)bitmapScaledHeight); 
       canvas.drawBitmap(mBitmap, null, mTempRect, mBitmapPaint); 
      } 
     } 
    } 
} 

通過這種方法,你可以靈活地應用,你需要

相關問題