Android实现图片的倒影效果

public static Bitmap createReflectedImage(Bitmap originalImage) {
  final int reflectionGap = 4; 
  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); 
  Canvas canvas = new Canvas(bitmapWithReflection); 
  canvas.drawBitmap(originalImage, 0, 0, null); 
  Paint defaultPaint = new Paint(); 
  canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); 
  canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 
  Paint paint = new Paint(); 
  LinearGradient shader = new LinearGradient(0, 
    originalImage.getHeight(), 0, bitmapWithReflection.getHeight() 
    + reflectionGap, 0×70ffffff, 0×00ffffff, 
    TileMode.MIRROR); 
  paint.setShader(shader); 
  paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
  canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() 
    + reflectionGap, paint); 
  return bitmapWithReflection; 
}





解释一下:

Matrix matrix = new Matrix();
matrix.preScale(1, -1);

实现图片的反转,见Android利用Matrix简单处理图片。

Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false);

创建反转后的图片Bitmap对象,图片高是原图的一半。

Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);

创建标准的Bitmap对象,宽和原图一致,高是原图的1.5倍。

Canvas canvas = new Canvas(bitmapWithReflection);

canvas.drawBitmap(originalImage, 0, 0, null);

创建画布对象,将原图画于画布,起点是原点位置。

Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

将反转后的图片画到画布中。

LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0×70ffffff, 0×00ffffff,
TileMode.MIRROR);

创建线性渐变LinearGradient 对象。

canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);

画布画出反转图片大小区域,然后把渐变效果加到其中,就出现了图片的倒影效果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现水面倒影效果,可以使用three.js的水面倒影插件`three.js-water`。这个插件可以创建一个水面对象,并且将其反射到场景中,从而实现水面倒影效果。 以下是实现水面倒影效果的基本步骤: 1. 导入`three.js`和`three.js-water`插件。 ```javascript import * as THREE from 'three'; import Water from 'three-water'; ``` 2. 创建场景、渲染器和相机。 ```javascript const scene = new THREE.Scene(); const renderer = new THREE.WebGLRenderer(); const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000); camera.position.set(0, 100, 200); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); ``` 3. 创建水面对象。 ```javascript const waterGeometry = new THREE.PlaneBufferGeometry(10000, 10000); const water = new Water(waterGeometry, { color: 'blue', scale: 5, flowDirection: new THREE.Vector2(1, 1), textureWidth: 512, textureHeight: 512, reflectivity: 0.3 }); water.rotation.x = -Math.PI / 2; scene.add(water); ``` 4. 创建地面对象,并将其添加到场景中。 ```javascript const groundGeometry = new THREE.PlaneBufferGeometry(10000, 10000); const groundMaterial = new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false }); const ground = new THREE.Mesh(groundGeometry, groundMaterial); ground.rotation.x = -Math.PI / 2; scene.add(ground); ``` 5. 创建一个立方体,并将其添加到场景中。 ```javascript const cubeGeometry = new THREE.BoxBufferGeometry(50, 50, 50); const cubeMaterial = new THREE.MeshPhongMaterial({ color: 'red' }); const cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.position.set(0, 25, 0); scene.add(cube); ``` 6. 创建一个点光源,并将其添加到场景中。 ```javascript const light = new THREE.PointLight('white', 1, 1000); light.position.set(0, 200, 0); scene.add(light); ``` 7. 使用`three.js-water`插件提供的`update`方法更新水面对象。 ```javascript function animate() { requestAnimationFrame(animate); water.material.uniforms.time.value += 1.0 / 60.0; water.render(); renderer.render(scene, camera); } animate(); ``` 通过以上步骤,就可以在three.js中实现水面倒影效果了。完整的代码示例可以参考`three.js-water`插件的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值