Android上图像处理-倒影,圆角,渐变

Android对图片处理的函数应该是很多的,但由于平时对图像处理用到情况不多,要用到时只有不停的Google.

这次要用到的图像效果比较多,有倒影,图片圆角,还有图像渐变,感谢Google,在经过我不懈的搜索后,找到了一些比较有用的做法.

1.在android中2D中实现对图片的倒影:

http://blog.163.com/joe_zhpf@126/blog/static/81331086201032755840878/

2.Android 中实现倒影效果

http://www.blogjava.net/lihao336/archive/2010/04/13/318163.html

3.使用 2D 方法实现倒影特效

http://www.linuxgraphics.cn/android/reflection_effect_2d.html

4.Android 图片透明度处理代码

http://blog.csdn.net/xyz_lmn/archive/2009/12/10/4978982.aspx

5.android图片处理方法

http://gundumw100.iteye.com/blog/849729

我将上面这些代码进行了整理,以适合自己的使用,现在对这些原理都不太了解,需要慢慢的去熟悉:

附目前效果图:

	public static Bitmap createReflectionImageWithOrigin(Bitmap originalImage) {
 
		// The gap we want between the reflection and the original image
 
		final int reflectionGap = 4;
 
 
 
		// Get you bit map from drawable folder
 
		// Bitmap originalImage = BitmapFactory.decodeResource(getResources(),
 
		// R.drawable.twitter_icon);
 
 
 
		int width = originalImage.getWidth();
 
		int height = originalImage.getHeight();
 
 
 
		// This will not scale but will flip on the Y axis
 
		Matrix matrix = new Matrix();
 
		matrix.preScale(1, -1);
 
 
 
		// Create a Bitmap with the flip matix applied to it.
 
		// We only want the bottom half of the image
 
		Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
 
				height / 2, width, height / 2, matrix, false);
 
 
 
		// Create a new bitmap with same width but taller to fit reflection
 
		Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
 
				(height + height / 2), Config.ARGB_8888);
 
 
 
		// Create a new Canvas with the bitmap that's big enough for
 
		// the image plus gap plus reflection
 
		Canvas canvas = new Canvas(bitmapWithReflection);
 
		// Draw in the original image
 
		canvas.drawBitmap(originalImage, 0, 0, null);
 
		// Draw in the gap
 
		Paint deafaultPaint = new Paint();
 
		canvas
 
				.drawRect(0, height, width, height + reflectionGap,
 
						deafaultPaint);
 
		// Draw in the reflection
 
		canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
 
 
 
		// Create a shader that is a linear gradient that covers the reflection
 
		Paint paint = new Paint();
 
		LinearGradient shader = new LinearGradient(0,
 
				originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
 
						+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
 
		// Set the paint to use this shader (linear gradient)
 
		paint.setShader(shader);
 
		// Set the Transfer mode to be porter duff and destination in
 
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
 
		// Draw a rectangle using the paint with our linear gradient
 
		canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
 
				+ reflectionGap, paint);
 
 
 
		return bitmapWithReflection;
 
	}
 
 
 
	public static Bitmap createReflectionImageWithNoOrigin(Bitmap originalImage) {
 
		// The gap we want between the reflection and the original image
 
		final int reflectionGap = 4;
 
 
 
		// Get you bit map from drawable folder
 
		// Bitmap originalImage = BitmapFactory.decodeResource(getResources(),
 
		// R.drawable.twitter_icon);
 
 
 
		int width = originalImage.getWidth();
 
		int height = originalImage.getHeight();
 
 
 
		// This will not scale but will flip on the Y axis
 
		Matrix matrix = new Matrix();
 
		matrix.preScale(1, -1);
 
 
 
		// Create a Bitmap with the flip matix applied to it.
 
		// We only want the bottom half of the image
 
		int divider = 8;
 
 
 
		Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height
 
				/ divider * (divider - 1), width, height / divider, matrix,
 
				false);
 
 
 
		// Create a new bitmap with same width but taller to fit reflection
 
		Bitmap bitmapWithReflection2 = Bitmap.createBitmap(width, height / 5,
 
				Config.ARGB_8888);
 
 
 
		// Create a new Canvas with the bitmap that's big enough for
 
		// the image plus gap plus reflection
 
		Canvas canvas = new Canvas(reflectionImage);
 
		// Draw in the reflection
 
		canvas.drawBitmap(reflectionImage, 0, 0, null);
 
 
 
		// Create a shader that is a linear gradient that covers the reflection
 
		Paint paint = new Paint();
 
		LinearGradient shader = new LinearGradient(0,
 
				originalImage.getHeight(), 0, reflectionImage.getHeight(),
 
				0x70ffffff, 0x00ffffff, TileMode.CLAMP);
 
		// Set the paint to use this shader (linear gradient)
 
		paint.setShader(shader);
 
		// Set the Transfer mode to be porter duff and destination in
 
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
 
		// Draw a rectangle using the paint with our linear gradient
 
		canvas.drawRect(0, height/divider, width, reflectionImage.getHeight(), paint);
 
 
 
		return reflectionImage;
 
	}
 
 
 
	public static Bitmap setAlpha(Bitmap sourceImg, int number) {
 
		int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];
 
		sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0, sourceImg
 
				.getWidth(), sourceImg.getHeight());//Get the ARGB Value of the image
 
		number = number * 255 / 100;
 
		for (int i = 0; i < argb.length; i++) {
 
			//argb = (number << 24) | (argb & 0x00ffffff);// modify the top two bit value
 
		}
 
		sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg
 
				.getHeight(), Config.ARGB_8888);
 
 
 
		return sourceImg;
 
	}
 
 
 
	/**
 
	 * get reflection bitmap of the original bitmap.
 
	 * 
 
	 * @param srcBitmap
 
	 * @return
 
	 */
 
	public static Bitmap makeReflectionBitmap(Bitmap originalImage) {
 
 
 
		int width = originalImage.getWidth();
 
		int height = originalImage.getHeight();
 
 
 
		// This will not scale but will flip on the Y axis
 
		Matrix matrix = new Matrix();
 
		matrix.preScale(1, -1);
 
 
 
		// Create a Bitmap with the flip matix applied to it.
 
		// We only want the bottom half of the image
 
		int divider = 8;
 
 
 
		Bitmap srcBitmap = Bitmap.createBitmap(originalImage, 0, height
 
				/ divider * (divider - 1), width, height / divider, matrix,
 
				false);
 
 
 
		//srcBitmap=originalImage;
 
		int bmpWidth = srcBitmap.getWidth();
 
		int bmpHeight = srcBitmap.getHeight();
 
		int[] pixels = new int[bmpWidth * bmpHeight * 4];
 
		srcBitmap.getPixels(pixels, 0, bmpWidth, 0, 0, bmpWidth, bmpHeight);
 
 
 
		// get reversed bitmap
 
		Bitmap reverseBitmap = Bitmap.createBitmap(bmpWidth, bmpHeight,
 
				Bitmap.Config.ARGB_8888);
 
		for (int y = 0; y < bmpHeight; y++) {
 
			reverseBitmap.setPixels(pixels, y * bmpWidth, bmpWidth, 0,
 
					bmpHeight - y - 1, bmpWidth, 1);
 
		}
 
 
 
		// get reflection bitmap based on the reversed one
 
		reverseBitmap.getPixels(pixels, 0, bmpWidth, 0, 0, bmpWidth, bmpHeight);
 
		Bitmap reflectionBitmap = Bitmap.createBitmap(bmpWidth, bmpHeight,
 
				Bitmap.Config.ARGB_8888);
 
		int alpha = 0x00000000;
 
		for (int y = 0; y < bmpHeight; y++) {
 
			for (int x = 0; x < bmpWidth; x++) {
 
				int index = y * bmpWidth + x;
 
				int r = (pixels[index] >> 16) & 0xff;
 
				int g = (pixels[index] >> 8) & 0xff;
 
				int b = pixels[index] & 0xff;
 
 
 
				pixels[index] = alpha | (r << 16) | (g << 8) | b;
 
 
 
				reflectionBitmap.setPixel(x, y, pixels[index]);
 
			}
 
			alpha = alpha + 0x01000000;
 
		}
 
		return reflectionBitmap;
 
	}
 
 
 
 
 
	/**
 
	 * Set the Bitmap alpha
 
	 * @param srcBitmap
 
	 * @return
 
	 */
 
	public static Bitmap setAlpha(Bitmap srcBitmap){
 
		int bmpWidth = srcBitmap.getWidth();
 
		int bmpHeight = srcBitmap.getHeight();
 
		int[] pixels = new int[bmpWidth * bmpHeight * 4];
 
		srcBitmap.getPixels(pixels, 0, bmpWidth, 0, 0, bmpWidth, bmpHeight);
 
		// get reflection bitmap based on the reversed one
 
		// Bitmap reverseBitmap = Bitmap.createBitmap(bmpWidth, bmpHeight,
 
		// Bitmap.Config.ARGB_8888);
 
		// srcBitmap.getPixels(pixels, 0, bmpWidth, 0, 0, bmpWidth, bmpHeight);
 
		Bitmap reflectionBitmap = Bitmap.createBitmap(bmpWidth, bmpHeight,
 
				Bitmap.Config.ARGB_8888);
 
		//int alpha = 0x88ffffff/10;
 
		int alpha = 0x01000000*100;
 
		Utils.log("bmpWidth="+bmpWidth+",bmpHeight="+bmpHeight);
 
		for (int y = 0; y < bmpHeight; y++) {
 
			Utils.log("alpha="+alpha);
 
			for (int x = 0; x < bmpWidth; x++) {
 
				int index = y * bmpWidth + x;
 
				int r = (pixels[index] >> 16) & 0xff;
 
				int g = (pixels[index] >> 8) & 0xff;
 
				int b = pixels[index] & 0xff;
 
 
 
				pixels[index] = alpha | (r << 16) | (g << 8) | b;
 
 
 
				reflectionBitmap.setPixel(x, y, pixels[index]);
 
			}
 
 
 
			alpha = alpha - 0x01000000*10;
 
 
 
		}
 
		return reflectionBitmap;
 
	}
 
 
 
 
 
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
 
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
 
				.getHeight(), Config.ARGB_8888);
 
		Canvas canvas = new Canvas(output);
 
 
 
		final int color = 0xff424242;
 
		final Paint paint = new Paint();
 
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
 
		final RectF rectF = new RectF(rect);
 
		final float roundPx = 5;
 
 
 
		paint.setAntiAlias(true);
 
		canvas.drawARGB(0, 0, 0, 0);
 
		paint.setColor(color);
 
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
 
 
 
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
 
		canvas.drawBitmap(bitmap, rect, rect, paint);
 
 
 
		return output;
 
	}

图像等比例伸缩

    public static Bitmap createFixedBitmapWithImageView(ImageView imageView, Bitmap bitmap) {
        float bitmapHeight = bitmap.getHeight();
        float bitmapWidth = bitmap.getWidth();
 
        float imageViewWidth = imageView.getWidth();
        float imageViewHeight = imageView.getHeight();
 
        float imageViewRate = (float) imageViewWidth / (float) imageViewHeight;
        float bitmapRate = (float) bitmapWidth / (float) bitmapHeight;
        float newWidth = 0;
        float newHeight = 0;
        if (bitmapHeight > imageViewHeight && bitmapWidth > imageViewWidth) {
            if (bitmapRate > imageViewRate) {
                newWidth = imageViewWidth;
                newHeight = (imageViewWidth / bitmapWidth) * imageViewHeight;
            } else {
                newHeight = imageViewHeight;
                newWidth = (imageViewHeight / bitmapHeight) * bitmapWidth;
            }
        } else if (bitmapHeight >= imageViewHeight && bitmapWidth <= imageViewWidth) {
            if (bitmapRate > imageViewRate) {
                newWidth = imageViewWidth;
                newHeight = (imageViewWidth / bitmapWidth) * imageViewHeight;
            } else {
                newHeight = imageViewHeight;
                newWidth = imageViewHeight / bitmapHeight * bitmapWidth;
            }
        } else if (bitmapHeight < imageViewHeight && bitmapWidth > imageViewWidth) {
            if (bitmapRate > imageViewRate) {
                newWidth = imageViewWidth;
                newHeight = (imageViewWidth / bitmapWidth) * imageViewHeight;
            } else {
                newHeight = imageViewHeight;
                newWidth = (imageViewHeight / bitmapHeight) * bitmapWidth;
            }
        } else {
            if (bitmapRate > imageViewRate) {
                newWidth = imageViewWidth;
                newHeight = (imageViewWidth) / bitmapWidth * imageViewHeight;
            } else {
                newHeight = imageViewHeight;
                newWidth = (imageViewHeight / bitmapHeight) * bitmapWidth;
            }
        }
 
        Bitmap resizedBitmap = resizeImage(bitmap, (int) newWidth, (int) newHeight);
 
        return resizedBitmap;
    }