ImageView 中的 Android 中心位图

标签 android android-layout bitmap

我有一个显示位图的 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);
}

bitmap

最佳答案

使用这个:

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

关于ImageView 中的 Android 中心位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25010409/

相关文章:

在 OS X 上安装 Java 7 JDK 后,JavaVirtualMachines 仍然显示 "1.6.0.jdk"

java - 尝试在我的 Android 应用程序中 setDisplayHomeAsUpEnabled(true) ,它给了我一个 NullPointerException

android - 从右到左增加 Android 按钮的宽度

android - 将位图图像从一个 Activity 传递到另一个 Activity

Android TalkBack 仅循环显示浏览器中的屏幕内容

android - 如何在 Android 中设置多个闹钟?

android - Android Lollipop CardView 上的波纹效果

android - 如何在android中用文本显示圆弧

android - 避免位图被回收android

java - 如何将字节的json数组转换为位图