public Bitmap DrawableToBitmap(Drawable drawableAPIcon)
{
BitmapDrawable bd = (BitmapDrawable)drawableAPIcon;
Bitmap bitmapAPIcon = bd.getBitmap();
return bitmapAPIcon;
}
{
BitmapDrawable bd = (BitmapDrawable)drawableAPIcon;
Bitmap bitmapAPIcon = bd.getBitmap();
return bitmapAPIcon;
}
原始的資源圖是大小不一的, 但Matrix,【數】矩陣,可以用於圖像處理的縮放、平移、旋轉等操作。
以下將所傳入的Bitmap調整為寬高:100x100
public Bitmap fixSize(Bitmap bitmapAPIcon)
{
int iconWidth = 0;
int iconHeight = 0;
int newIconWidth = 100;
int newIconHeight = 100;
iconWidth = bitmapAPIcon.getWidth(); //先取得原Bitmap的寬
iconHeight = bitmapAPIcon.getHeight(); //先取得原Bitmap的高
float scaleIconWidth = ((float) newIconWidth) / iconWidth; //算出要調整的寬比例
float scaleIconHeight = ((float) newIconHeight) / iconHeight; //算出要調整的高比例
Matrix iconMatrix = new Matrix();
iconMatrix.postScale(scaleIconWidth, scaleIconHeight); //經過比例調整
Bitmap finalAPicon = Bitmap.createBitmap(bitmapAPIcon, 0, 0,iconWidth, iconHeight, iconMatrix, true); //建立新的Bitmap
return finalAPicon;
}
調整為相同大小的圖後看起來整齊多了~
%%%%%%%%%%%%%%%%%延伸學習%%%%%%%%%%%%%%%%%%%%%%%%%
沒有留言:
張貼留言