首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从动态AlertDialog调整BitMap图标的大小

从动态AlertDialog调整BitMap图标的大小
EN

Stack Overflow用户
提问于 2015-09-09 07:40:04
回答 1查看 1.4K关注 0票数 0

从安卓19 (KITKAT)开始,我不再能够在AlertDialog中用代码动态地调整图标的大小。它已经在Android 8- 16中工作了。现在,它只显示原来宽度和高度的图标--而不是调整大小的图标。

请注意,这些图像是从我的服务器上动态下载的,相同的对话框将根据用户的选择显示不同的图像。

这是过去为我工作的东西。对于如何更新安卓19+,有什么建议吗?

注意:我尝试过Bitmap.createScaledBitmap解决方案(请参阅注释行)。它没有效果。

代码语言:javascript
运行
复制
Uri downloadedIcon = Uri.parse( "file://" + filePath );
BitMap bm = Media.getBitmap( getContentResolver(), downloadedIcon );

DisplayMetrics metrics = new DisplayMetrics();        
getWindowManager().getDefaultDisplay().getMetrics(metrics);  
int width = metrics.widthPixels;

if (width >= 2000) {
    bm.setDensity(840);
    //bm = Bitmap.createScaledBitmap(bm,920,920,true);
} else if (width >= 1440) {
    bm.setDensity(640);
    //bm = Bitmap.createScaledBitmap(bm,700,700,true);
} else if(width >= 1024)
    // etc
}

BitmapDrawable icon = new BitmapDrawable(bm);

AlertDialog theAlert = new AlertDialog.Builder(MyActivity.this)
.setTitle("My Title")
.setMessage("My Description")
.setIcon(-1).setIcon(icon)
theAlert.show();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-12 02:12:23

好的,我通过创建我自己的对话框布局来解决这个问题。

问题是,在Android 19+中,默认(系统)对话框布局将强制缩小图标的src。因此,要使它更大--你需要忘记图标,停止使用它。相反,在您自己的自定义布局中定义一个新的ImageView。

custom_dialog.xml:

代码语言:javascript
运行
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/dialog_pic"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitStart"
            android:adjustViewBounds="true" />

    </RelativeLayout>   

    <TextView 
        android:id="@+id/desc"
        android:text="DESCRIPTION GOES HERE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:typeface="serif"
        android:textSize="17sp" />  

</LinearLayout>

在充气ImageView之后,我用动态BitMap覆盖它,如下所示:

代码语言:javascript
运行
复制
Uri downloadedIcon = Uri.parse( "file://" + filePath );
BitMap bm = Media.getBitmap( getContentResolver(), downloadedIcon );

View dialogView = getLayoutInflater().inflate(R.layout.my_custom_dialog, null);
ImageView pic = (ImageView) dialogView.findViewById(R.id.dialog_pic);
pic.setImageBitmap( bm );
TextView desc = (TextView) dialogView.findViewById(R.id.desc);
desc.setText("My Description Text");

AlertDialog theAlert = new AlertDialog.Builder(ScriptMenuActivity.this)
    .setView(dialogView)
    .setTitle("My Title Text")
    .setNegativeButton("Back", null)
    .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // do stuff
        }}).create();
    theAlert.show();

通过ImageView的“adjustViewBounds”和“scaleType”属性,按比例调整对话框图片的大小以适应对话框窗口。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32473652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档