【问题标题】:Android center Bitmap in ImageViewImageView中的Android中心位图
【发布时间】:2014-07-29 07:41:52
【问题描述】:

我有一个显示位图的 ImageView。当我将位图设置为 ImageView 时,它没有居中。我还尝试将 scaleType 设置为 center、centerCrop、centerInside。

这是我在布局中的 ImageView:

    android:id="@+id/xyz"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="3dp"
    android:layout_weight="1.03"
    android:background="#FFFFFFFF" 
    android:adjustViewBounds="true"
    android:scaleType="fitXY"

编辑: 这是调整位图大小的代码:

private void scaleImage() {
    Bitmap bitmap = getHostPage().getBackgroundDrawing();

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int bounding = dpToPx(250);
    float xScale = ((float) bounding) / width;
    float yScale = ((float) bounding) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(xScale, yScale);

    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    width = scaledBitmap.getWidth(); 
    height = scaledBitmap.getHeight(); 
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);

    getHostPage().setBackgroundDrawing(result.getBitmap());
    
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) drawView.getLayoutParams(); 
    params.width = width;
    params.height = height;
    drawView.setLayoutParams(params);

}

private int dpToPx(int dp) {
    float density = hostPage.getHostActivity().getApplicationContext().getResources().getDisplayMetrics().density;
    return Math.round((float)dp * density);
}

【问题讨论】:

  • android:gravity="center_horizo​​ntal" 或 android:layout_gravity="center_horizo​​ntal"
  • 你也可以使用 FrameLayout

标签: android android-layout bitmap


【解决方案1】:

使用这个:

android:gravity="center|center_vertical"
android:layout_gravity="center|center_vertical"

【讨论】:

  • 不适用于此图像视图。我已经在相对视图中尝试过,但位图设置左侧没有进入中心
【解决方案2】:

将此添加到您的图像视图 xml 代码中:

android:layout_centerHorizontal="true"

我想这可能对你有帮助。

谢谢。

【讨论】:

    【解决方案3】:
        android:id="@+id/xyz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="3dp"
        android:layout_weight="1.03"
        android:background="#FFFFFFFF" 
        android:gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      相关资源
      最近更新 更多