2016-11-01 28 views
0

我正試圖根據相對佈局創建自定義佈局「SquareBox」,以將ImageView放在中間。 而這個自定義佈局將總是在正方形大小(取決於ImageView的高度和寬度)。父中的中心不適用於自定義相對佈局

protected void onMeasure(int width, int height) { 
    super.onMeasure(width, height); 
    int measuredWidth = getMeasuredWidth(); 
    int measuredHeight = getMeasuredHeight(); 
    int size; 
    if (measuredWidth > measuredHeight) { 
     size = measuredWidth; 
    } else { 
     size = measuredHeight; 
    } 
    setMeasuredDimension(size, size); 
} 

這裏是我的佈局代碼:

<com.example.tools.SquareBox 
     android:id="@+id/square_box" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:background="@android:color/darker_gray"> 

    <ImageView 
     android:id="@+id/canvas_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:layout_centerInParent="true" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     tools:src="@drawable/demo_image" /> 
</com.example.tools.SquareBox> 

它工作正常,但圖像的視圖是不是在這個自定義視圖的中心。 我試過以下內容: - > android:layout_centerInParent =「true」用於ImageView。

- > android:gravity =「center」爲「SquareBox」。

- >以編程方式將CENTER_IN_PARENT添加到SquareBox的onMeasure方法中的ImageView中。

但沒有任何工作。 我在這裏失蹤了什麼?

回答

0

我仿效另一種方式是方框行爲。我認爲這是一個解決方法,它可能不是最好的解決方案,但它的工作原理。我想知道更好更清潔的解決方案。

佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/square_box_holder" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_margin="0dp" 
     android:layout_weight="3"> 

     <View 
      android:id="@+id/square_box" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_centerInParent="true" 
      android:background="@android:color/darker_gray" /> 


     <ImageView 
      android:id="@+id/canvas_image" 
      style="@style/CanvasImageStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:layout_margin="0dp" 
      android:adjustViewBounds="true" 
      tools:src="@drawable/demo_image2" /> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

    </LinearLayout> 
</LinearLayout> 

代碼以調整視圖:

squareImageView.setImageBitmap(getResultBitmap()); 
      squareBoxHolder.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
       @Override 
       public boolean onPreDraw() { 
        squareBoxHolder.getViewTreeObserver().removeOnPreDrawListener(this); 
        Log.i(TAG, "onPreDraw: squareBoxHolder"); 
        squareBoxHolderSize = new Point(squareBoxHolder.getMeasuredWidth(), squareBoxHolder.getMeasuredHeight()); 
        return true; 
       } 
      }); 
      squareImageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
       @Override 
       public boolean onPreDraw() { 
        squareImageView.getViewTreeObserver().removeOnPreDrawListener(this); 
        Log.i(TAG, "onPreDraw: squareImageView"); 
        int imageWidth = squareImageView.getMeasuredWidth(); 
        int imageHeight = squareImageView.getMeasuredHeight(); 
        int size; 
        ViewGroup.LayoutParams layoutParams = squareImageView.getLayoutParams(); 
        if (imageWidth > imageHeight) { 
         if (imageWidth > squareBoxHolderSize.y) { 
          size = squareBoxHolderSize.y; 
          layoutParams.width = size; 
          squareImageView.setLayoutParams(layoutParams); 
         } else { 
          size = imageWidth; 
         } 
        } else { 
         if (imageHeight > squareBoxHolderSize.x) { 
          size = squareBoxHolderSize.x; 
          layoutParams.height = size; 
          squareImageView.setLayoutParams(layoutParams); 
         } else { 
          size = imageHeight; 
         } 
        } 

        layoutParams = squareBox.getLayoutParams(); 
        layoutParams.height = size; 
        layoutParams.width = size; 
        squareBox.setLayoutParams(layoutParams); 
        return true; 
       } 
      }); 
0

試試這個: - android:layout_gravity="center_horizontal"

+0

我已經嘗試過這個。但不工作。 –

0

更改您的佈局,這(改變layout_widthmatch_parent):

<com.example.tools.SquareBox 
     android:id="@+id/square_box" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:background="@android:color/darker_gray"> 

    <ImageView 
     android:id="@+id/canvas_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:layout_centerInParent="true" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     tools:src="@drawable/demo_image" /> 
</com.example.tools.SquareBox> 

,並更改您的自定義佈局,以這樣的:

@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
    int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
    int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
    int heightSize = MeasureSpec.getSize(heightMeasureSpec); 

    int size; 
    if(widthMode == MeasureSpec.EXACTLY && widthSize > 0){ 
     size = widthSize; 
    } 
    else if(heightMode == MeasureSpec.EXACTLY && heightSize > 0){ 
     size = heightSize; 
    } 
    else{ 
     size = widthSize < heightSize ? widthSize : heightSize; 
    } 

    int finalMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); 
    super.onMeasure(finalMeasureSpec, finalMeasureSpec); 
    } 
+0

我試過這個。它確實將圖像置於自定義視圖中。但是在調整自定義視圖的大小後,ImageView依然保持居中,這是因爲之前的大小會導致裁剪。 –