Android学习Matrix对称变换<5>
网上很多的倒影特效实际上就是一个对称变换,在改变透明度即可。
Matrix对称变换包括很多种,有关于Y轴对称,关于X轴对称,关于y= -x对称等等。
1 关于Y轴对称
- // 获取资源文件的引用res
- Resources res = getResources();
- // 获取图形资源文件
- Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.v);
- // 设置canvas画布背景为白色
- canvas.drawColor(Color.BLACK);
- // 在画布上绘制缩放之前的位图,以做对比
- //屏幕上的位置坐标是0,0
- canvas.drawBitmap(bmp, 0, 0, null);
- // 定义矩阵对象
- Matrix matrix = new Matrix();
- float[] values ={-1f,0.0f,0.0f,0.0f,1f,0.0f,0.0f,0.0f,1.0f};
- matrix.setValues(values);
- System.out.println("matrix:" + matrix.toString());
- Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
- matrix, true);
- canvas.drawBitmap(dstbmp, bmp.getWidth(), 0, null);
屏幕上包括两幅图片,一张原图,一张对称图,来形成对比效应。
2 关于X轴对称
- // 获取资源文件的引用res
- Resources res = getResources();
- // 获取图形资源文件
- Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.v);
- // 设置canvas画布背景为白色
- canvas.drawColor(Color.BLACK);
- // 在画布上绘制缩放之前的位图,以做对比
- //屏幕上的位置坐标是0,0
- canvas.drawBitmap(bmp, 0, 0, null);
- // 定义矩阵对象
- Matrix matrix = new Matrix();
- float[] values ={1f,0.0f,0.0f,0.0f,-1f,0.0f,0.0f,0.0f,1.0f};
- matrix.setValues(values);
- System.out.println("matrix:" + matrix.toString());
- Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
- matrix, true);
- canvas.drawBitmap(dstbmp, 0, bmp.getHeight(), null);
3 关于原点对称
- // 获取资源文件的引用res
- Resources res = getResources();
- // 获取图形资源文件
- Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.v);
- // 设置canvas画布背景为白色
- canvas.drawColor(Color.BLACK);
- // 在画布上绘制缩放之前的位图,以做对比
- //屏幕上的位置坐标是0,0
- canvas.drawBitmap(bmp, 0, 0, null);
- // 定义矩阵对象
- Matrix matrix = new Matrix();
- float[] values ={-1f,0.0f,0.0f,0.0f,-1f,0.0f,0.0f,0.0f,1.0f};
- matrix.setValues(values);
- System.out.println("matrix:" + matrix.toString());
- Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
- matrix, true);
- canvas.drawBitmap(dstbmp, bmp.getWidth(), bmp.getHeight(), null);
4 关于y = x 对称
- // 获取资源文件的引用res
- Resources res = getResources();
- // 获取图形资源文件
- Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.v);
- // 设置canvas画布背景为白色
- canvas.drawColor(Color.BLACK);
- // 在画布上绘制缩放之前的位图,以做对比
- //屏幕上的位置坐标是0,0
- canvas.drawBitmap(bmp, 0, 0, null);
- // 定义矩阵对象
- Matrix matrix = new Matrix();
- float[] values ={0f,1f,0.0f,1f,0f,0.0f,0.0f,0.0f,1.0f};
- matrix.setValues(values);
- System.out.println("matrix:" + matrix.toString());
- Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
- matrix, true);
- canvas.drawBitmap(dstbmp, bmp.getWidth(), bmp.getHeight(), null);
5 关于y = -x对称
- // 获取资源文件的引用res
- Resources res = getResources();
- // 获取图形资源文件
- Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.v);
- // 设置canvas画布背景为白色
- canvas.drawColor(Color.BLACK);
- // 在画布上绘制缩放之前的位图,以做对比
- //屏幕上的位置坐标是0,0
- canvas.drawBitmap(bmp, bmp.getHeight(), 0, null);
- // 定义矩阵对象
- Matrix matrix = new Matrix();
- float[] values ={0f,-1f,0.0f,-1f,0f,0.0f,0.0f,0.0f,1.0f};
- matrix.setValues(values);
- System.out.println("matrix:" + matrix.toString());
- Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(),
- matrix, true);
- canvas.drawBitmap(dstbmp, 0, bmp.getHeight(), null);