使用canvas.drawBitmapMesh扭曲图像
2015-12-16 17:18
651 查看
drawBitmapMesh 关键参数:
width:宽度上把图分成多少格
height:高度上把图分成多少格
verts:记录位图各个顶点位置,控制图片的扭曲效果
width:宽度上把图分成多少格
height:高度上把图分成多少格
verts:记录位图各个顶点位置,控制图片的扭曲效果
private Bitmap bitmap; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main); linearLayout.addView(new MyView(this, R.drawable.late)); } private class MyView extends View { private final int width = 20; private final int height = 20; private final int count = (width + 1) * (height + 1); private final float[] verts = new float[count * 2]; private final float[] orig = new float[count * 2]; public MyView(Context context, int drawableId) { super(context); setFocusable(true); bitmap = BitmapFactory.decodeResource(getResources(), drawableId); float bitmapWidth = bitmap.getWidth(); float bitmapHeight = bitmap.getHeight(); int index = 0; for (int y = 0; y <= height; y++) { float fy = bitmapHeight * y / height; for (int x = 0; x <= width; x++) { float fx = bitmapWidth * x / width; orig[index * 2 + 0] = verts[index * 2 + 0] = fx; orig[index * 2 + 1] = verts[index * 2 + 1] = fy; index += 1; } } setBackgroundColor(Color.WHITE); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmapMesh(bitmap, width, height, verts, 0, null, 0, null); } @Override public boolean onTouchEvent(MotionEvent event) { float cx = event.getX(); float cy = event.getY(); for (int i = 0; i < count * 2; i += 2) { float dx = cx - orig[i + 0]; float dy = cy - orig[i + 1]; float dd = dx * dx + dy * dy; float d = (float) Math.sqrt(dd); float pull = 80000 / ((float) (dd * d)); if (pull >= 1) { verts[i + 0] = cx; verts[i + 1] = cy; } else { verts[i + 0] = orig[i + 0] + dx * pull; verts[i + 1] = orig[i + 1] + dy * pull; } } invalidate(); return true; } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories