Java 某些生成的图标看起来比正常图标大

Java 某些生成的图标看起来比正常图标大,java,android,icons,package-managers,Java,Android,Icons,Package Managers,我目前正在列出我设备上安装的所有应用程序,但由于某些原因,一些图标看起来比通常的图标大。 如何定义图标的最大大小 下面是我用来获取图标的代码 ImageView appIcon = holder.Icon; Drawable icon = null; try { icon = getPackageManager().getApplicationIcon(apps.get(position).name.toStrin

我目前正在列出我设备上安装的所有应用程序,但由于某些原因,一些图标看起来比通常的图标大。 如何定义图标的最大大小

下面是我用来获取图标的代码

ImageView appIcon = holder.Icon;
            Drawable icon = null;
            try {

                icon = getPackageManager().getApplicationIcon(apps.get(position).name.toString());
            } catch (PackageManager.NameNotFoundException e) {
              Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
            }

            appIcon.setImageDrawable(icon);
或者,我假设您使用的是listview。设置图像视图的宽度和高度

  <ImageView
            android:id="@+id/stats_icon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="4dip"
            android:layout_marginTop="2dip"
            android:scaleType="fitCenter" />

私有静态位图drawableToBitmap(DrawableDrawable){
位图=空;
if(BitmapDrawable的可绘制实例){
BitmapDrawable BitmapDrawable=(BitmapDrawable)drawable;
if(bitmapDrawable.getBitmap()!=null){
返回bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth()
  <ImageView
            android:id="@+id/stats_icon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="4dip"
            android:layout_marginTop="2dip"
            android:scaleType="fitCenter" />
private static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }