BmpUtil

	public static Bitmap getBmpByResid(Resources resources, int resid)
	{
//		return BitmapFactory.decodeResource(getResources(), resid);
		BitmapDrawable drawable = (BitmapDrawable)resources.getDrawable(resid);
		return drawable.getBitmap();
	}
	
	// 根据width获取bmp
	public static Bitmap getBmpByWidth(Resources resources, int resid, int dstWidth)
	{
		Bitmap bmp = getBmpByResid(resources, resid);
		double dstHeight = getHeightByWidth(bmp, dstWidth);
		bmp = Bitmap.createScaledBitmap(bmp, (int)dstWidth, (int)dstHeight, true);
		return bmp;
	}
	
	public static int getHeightByWidth(Bitmap bmp, double width)
	{
		double height = width / bmp.getWidth() * bmp.getHeight();
		return (int)height;
	}
	
	// 根据width和height获取bmp
	public static Bitmap getBmpBySize(Resources resources, int resid, int dstWidth, int dstHeight)
	{
		Bitmap bmp = getBmpByResid(resources, resid);
		bmp = Bitmap.createScaledBitmap(bmp, (int)dstWidth, (int)dstHeight, true);
		return bmp;
	}
	
	// 根据缩放因子获取等比缩放以后的bmp
	public static Bitmap getBmpByScale(Resources resources, int resid, double scale)
	{
		Bitmap bmp = getBmpByResid(resources, resid);
		double dstWidth = bmp.getWidth() * scale;
		double dstHeight = bmp.getHeight() * scale;
		bmp = Bitmap.createScaledBitmap(bmp, (int)dstWidth, (int)dstHeight, true);
		return bmp;
	}

 

你可能感兴趣的:(util)