2 xiaoyaovsxin xiaoyaovsxin 于 2013.02.26 17:58 提问

android桌面图片变模糊

自己做了个桌面,获取应用图标跟原来的桌面相比变模糊了是怎么回事?大小一样大。
布局是:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:gravity="center"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingTop="15dip">

  <ImageView
    android:id="@+id/appIcon"
    android:layout_gravity="center"
    android:layout_width="40dip"
    android:layout_height="40dip"/>

  <TextView
    android:id="@+id/appName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:textColor="#FFFF"
    android:maxLines="1"
    android:fadingEdge="horizontal"
    android:gravity="center"
    android:paddingTop="10dip"
    android:paddingBottom="10dip"/>

</LinearLayout>

获取app的代码:

final PackageManager packageManager = getPackageManager();
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        //get all apps
        final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);

        GridView appPage = (GridView) findViewById(R.id.all_app_grid);
        appPage.setAdapter(new AppAdapter(this, apps)); 

在监听器中获取app图标的代码是:

Bitmap bmp = Utilities.createIconBitmap(appInfo.loadIcon(pm), mContext);        
appItem.mAppIcon.setImageDrawable(appInfo.loadIcon(pm));

static Bitmap createIconBitmap(Drawable icon, Context context) {
synchronized (sCanvas) { // we share the statics :-(
if (sIconWidth == -1) {
initStatics(context);
}

        int width = sIconWidth;
        int height = sIconHeight;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();

        if (sourceWidth > 0 && sourceHeight > 0) {
            // There are intrinsic sizes.
            if (width < sourceWidth || height < sourceHeight) {
                // It's too big, scale it down.
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                }
            } else if (sourceWidth < width && sourceHeight < height) {
                // Don't scale up the icon
                width = sourceWidth;
                height = sourceHeight;
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = sIconTextureWidth;
        int textureHeight = sIconTextureHeight;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
                Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth-width) / 2;
        final int top = (textureHeight-height) / 2;

        if (false) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length) sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left+width, top+height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left+width, top+height);
        icon.draw(canvas);
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}

4个回答

xiaoyaovsxin
xiaoyaovsxin   2013.02.28 13:34
已采纳

找到原因了,原因是不能自动调整屏幕分辨率,将sdk版本写成4,就会自适应屏幕分辨率了。

jkptthui
jkptthui   2013.02.28 09:21

Utilities.createIconBitmap()这个方法中的这段代码是对图片进行处理的:

if (sourceWidth > 0 && sourceHeight > 0) {
            // There are intrinsic sizes.
            if (width < sourceWidth || height < sourceHeight) {
                // It's too big, scale it down.
                final float ratio = (float) sourceWidth / sourceHeight;
                if (sourceWidth > sourceHeight) {
                    height = (int) (width / ratio);
                } else if (sourceHeight > sourceWidth) {
                    width = (int) (height * ratio);
                }
            } else if (sourceWidth < width && sourceHeight < height) {
                // Don't scale up the icon
                width = sourceWidth;
                height = sourceHeight;
            }
        }

你尝试将这段屏蔽或者直接改为如下的代码,运行再看看效果:

int width = sourceWidth;
int height = sourceHeight; 

意思是设置图片不作任何处理,长宽分别等于图片自身的大小,试试看。

suannai0314
suannai0314 帮你改了下代码的格式,详细的可以看这个http://ask.csdn.net/pages/markdown而且自己可以对自己的回答进行编辑哦,这样就不用回答好几次了
大约 5 年之前 回复
jkptthui
jkptthui   2013.02.27 09:31

你仅仅发一个布局文件上来,能看出什么来呢?

xiaoyaovsxin
xiaoyaovsxin 相关代码已附上,你知道怎么使图标调清晰么?
大约 5 年之前 回复
jkptthui
jkptthui   2013.02.27 11:08

Utilities.createIconBitmap()这个方法贴上来,看看有没有对图片做拉伸处理

xiaoyaovsxin
xiaoyaovsxin 用appItem.mAppIcon.setImageDrawable(appInfo.loadIcon(pm));这个方法获取的图标一样是模糊的。
大约 5 年之前 回复
xiaoyaovsxin
xiaoyaovsxin 已附上,请看!
大约 5 年之前 回复
xiaoyaovsxin
xiaoyaovsxin Bitmap bmp = Utilities.createIconBitmap(appInfo.loadIcon(pm), mContext); 没有做拉伸处理。
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!