gpt4 book ai didi

android - 使用 ImageView 显示 AlertDialog 而无需任何填充

转载 作者:IT老高 更新时间:2023-10-28 23:11:28 26 4
gpt4 key购买 nike

编辑 - 解决方案:

我最终找到了解决此问题的方法。因为手动更改 ImageView 的高度会删除额外的填充,所以我最终找到了原始图像的尺寸,并在可以计算 ImageView 尺寸后将它们应用于 ImageView。这是最终结果:

enter image description here

这是最终的工作代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog dialog = builder.create();
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
dialog.setView(dialogLayout);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog.show();

dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface d) {
ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.whygoprodialogimage);
float imageWidthInPX = (float)image.getWidth();

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
image.setLayoutParams(layoutParams);


}
});

还有 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/goProDialogImage"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:src="@drawable/whygoprodialogimage"/>

</LinearLayout>

原始问题:

我的目标是有一个 AlertDialog ,它有一个 ImageView 占据整个对话框,保留它的尺寸,加上两个按钮。通过标准方法实现,如下所示:

enter image description here

我正在尝试消除它上方和下方的填充。下面是它的设置代码:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/goProDialogImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/whygoprodialogimage"/>

</LinearLayout>

代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
dialog.setView(dialogLayout);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog.show();

看完this StackOverflow answer ,我尝试实现该解决方案,因为他们的问题看起来很相似,但即使它更接近我现在想要的结果,结果看起来像这样:

enter image description here

所以它消除了填充,但图像看起来被压扁了。这是此潜在修复实现的代码:

// Get screen size
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;

// Get target image size
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.whygoprodialogimage);
int bitmapHeight = bitmap.getHeight();
int bitmapWidth = bitmap.getWidth();

// Scale the image down to fit perfectly into the screen
// The value (250 in this case) must be adjusted for phone/tables displays
while(bitmapHeight > (screenHeight - 250) || bitmapWidth > (screenWidth - 250)) {
bitmapHeight = bitmapHeight / 2;
bitmapWidth = bitmapWidth / 2;
}

// Create resized bitmap image
BitmapDrawable resizedDialogImage = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, false));

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
}).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
dialog.setView(dialogLayout);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

// Without this line there is a very small border around the image (1px)
dialog.getWindow().setBackgroundDrawable(null);

dialog.show();
ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
image.setBackground(resizedDialogImage);

是什么导致图像现在看起来被压扁了?你可以看出它去掉了额外的填充,但图像尺寸发生了变化。

最佳答案

我最终找到了解决此问题的方法。因为手动更改 ImageView 的高度会删除额外的填充,所以我最终找到了原始图像的尺寸,并在可以计算 ImageView 尺寸后将它们应用于 ImageView。这是最终结果:

enter image description here

这是最终的工作代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog dialog = builder.create();
LayoutInflater inflater = getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
dialog.setView(dialogLayout);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog.show();

dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface d) {
ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.whygoprodialogimage);
float imageWidthInPX = (float)image.getWidth();

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
image.setLayoutParams(layoutParams);


}
});

还有 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/goProDialogImage"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:src="@drawable/whygoprodialogimage"/>

</LinearLayout>

关于android - 使用 ImageView 显示 AlertDialog 而无需任何填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27954561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com