我有一个显示位图的 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|center_vertical"
android:layout_gravity="center|center_vertical"
关于ImageView 中的 Android 中心位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25010409/