当用户使用drawBitmapMesh
拖动屏幕时,我正在实施图像变形。这是在进行一些更改后来自android apidemos的代码。
private static final int WIDTH = 20;
private static final int HEIGHT = 20;
private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);
private static float[] mVerts = new float[COUNT*2];
private float[] mOrig = new float[COUNT*2];
private static Matrix mMatrix = new Matrix();
private static Matrix mInverse = new Matrix();
private static float[] dst; //Global
//This is where I construct my mesh
float w = mBitmap.getWidth();
float h = mBitmap.getHeight();
int index = 0;
for (int y = 0; y <= HEIGHT; y++) {
float fy = h * y / HEIGHT;
for (int x = 0; x <= WIDTH; x++) {
float fx = w * x / WIDTH;
setXY(mVerts, index, fx, fy);
setXY(mOrig, index, fx, fy);
index += 1;
dst = mVerts;//Assign dst here just once
vertsCopy = mVerts.clone();
}
}
mMatrix.setTranslate(10, 10);
mMatrix.invert(mInverse);
private static void setXY(float[] array, int index, float x, float y) {
array[index * 2 + 0] = x;
array[index * 2 + 1] = y;
}
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
float[] pt = {event.getX(), event.getY()};
mInverse.mapPoints(pt);
int x = (int) pt[0];
int y = (int) pt[1];
if (mLastWarpX != x || mLastWarpY != y) {
mLastWarpX = x;
mLastWarpY = y;
warp(pt[0], pt[1]);
invalidate();
}
break;
}
return true;
}
private void warp(float cx, float cy) {
final float K = 700;
float[] src = dst; //now you are applying wrap effect on the last effected pixels
for (int i = 0; i < COUNT*2; i += 2) {
float x = src[i + 0];
float y = src[i + 1];
float dx = cx - x;
float dy = cy - y;
float dd = dx * dx + dy * dy;
float d = (float) Math.sqrt(dd);
float pull = K / (dd + 0.000001f);
pull /= (d + 0.000001f);
// android.util.Log.d("skia", "index " + i + " dist=" + d +
// " pull=" + pull);
if (pull >= 1) {
//Log.e("Pull", "Pull >=1");
dst[i + 0] = cx;
dst[i + 1] = cy;
} else {
//Log.e("Pull", "Pull <1");
dst[i + 0] = x + dx * pull;
dst[i + 1] = y + dy * pull;
}
}
}
问题是我在扭曲图像时会出现一些尖锐的边缘。我想要的是像app照片变形中那样的平滑失真。
What I want (as you can see smooth effect)
What I get (as you can see sharp edges)
任何评论,建议都非常感谢。提前谢谢。