life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
/**
     * Returns a bitmap suitable for the all apps view.  The bitmap will be a power
     * of two sized ARGB_8888 bitmap that can be used as a gl texture.
     
*/
    
private Bitmap createIconBitmap(Drawable icon) {
        
int width = mIconWidth;
        
int height = mIconHeight;

        
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(mDisplayMetrics);
            }
        }
        
int sourceWidth = icon.getIntrinsicWidth();
        
int sourceHeight = icon.getIntrinsicHeight();
调节大的图片,使它按比例适合我们的画布
        
if (sourceWidth > 0 && sourceWidth > 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) {
//                // It's small, use the size they gave us.
//                width = sourceWidth;
//                height = sourceHeight;
//            }
        }

        
// no intrinsic size --> use default size
        int textureWidth = mIconTextureWidth;
        
int textureHeight = mIconTextureHeight;

        
final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
                Bitmap.Config.ARGB_8888);
        
final Canvas canvas = mCanvas;
        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[mColorIndex]);
            
if (++mColorIndex >= sColors.length) mColorIndex = 0;
            Paint debugPaint 
= new Paint();
            debugPaint.setColor(
0xffcccc00);
            canvas.drawRect(left, top, left
+width, top+height, debugPaint);
        }

        mOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left
+width, top+height);
        icon.draw(canvas);
        icon.setBounds(mOldBounds);
        
return bitmap;
    }
posted on 2012-03-21 16:54 life02 阅读(1067) 评论(1)  编辑 收藏 引用 所属分类: Android开发

评论

# re: IconUtilities类的createIconBitmap方法分析 2012-04-23 20:40 bs
分析在哪里?  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理