- 反射倒影
public View createReflectedImages(Bitmap originalImage,int roationAngle) {
final int reflectionGap = 4;
int index = 0;
//Integer id = (Integer) map.get("image");
//Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), id); // 获取原始图片
int width = originalImage.getWidth();
int height = originalImage.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1); // 图片矩阵变换(从低部向顶部的倒影)
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false); // 截取原图下半部分
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); // 创建倒影图片(高度为原图3/2)
Canvas canvas = new Canvas(bitmapWithReflection); // 绘制倒影图(原图 + 间距 + 倒影)
canvas.drawBitmap(originalImage, 0, 0, null); // 绘制原图
Paint paint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, paint); // 绘制原图与倒影的间距
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); // 绘制倒影图
paint = new Paint();
LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader); // 线性渐变效果
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 倒影遮罩效果
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); // 绘制倒影的阴影效果
return imageTrans(bitmapWithReflection, roationAngle);
//return imageView;
}
2.drawable 转bitmap
public static Bitmap convertDrawable2BitmapByCanvas(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
200,
200,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
// canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
3.图形沿着Y轴旋转
private View imageTrans(Bitmap tmpBit,int roationAngle)
{
// 开始处理图像
// 1.获取处理矩阵
// 记录一下初始状态。save()和restore()可以将图像过渡得柔和一些。
// Each save should be balanced with a call to restore().
camera.save();
Matrix matrix = new Matrix();
// rotate
camera.rotateY(roationAngle);
// translate
camera.translate(0, 0, 100.0f);
camera.getMatrix(matrix);
// 恢复到之前的初始状态。
camera.restore();
// 设置图像处理的中心点
//matrix.preTranslate(tmpBit.getWidth() >> 1, tmpBit.getHeight() >> 1);
matrix.preTranslate(-(tmpBit.getWidth() / 2), -(tmpBit.getHeight() / 2));
matrix.postTranslate((tmpBit.getWidth() / 2), (tmpBit.getHeight() / 2));
//matrix.preSkew(0.5f, 0.5f);
// matrix.postSkew(skewX, skewY);
// 直接setSkew(),则前面处理的rotate()、translate()等等都将无效。
// matrix.setSkew(skewX, skewY);
// 2.通过矩阵生成新图像(或直接作用于Canvas)
// Log.d("ANDROID_LAB", "width=" + tmpBit.getWidth() + " height=" + tmpBit.getHeight());
Bitmap newBit = null;
try {
// 经过矩阵转换后的图像宽高有可能不大于0,此时会抛出IllegalArgumentException
newBit = Bitmap.createBitmap(tmpBit, 0, 0, tmpBit.getWidth(), tmpBit.getHeight(), matrix, true);
} catch (IllegalArgumentException iae) {
iae.printStackTrace();
}
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(newBit); // 设置倒影图片
imageView.setLayoutParams(new CultureGallery.LayoutParams(200,500));
imageView.setScaleType(ScaleType.MATRIX);
return imageView;
}