Revert "update demo to use boundary patch"
This reverts commit 3f14f59ee6.
This commit is contained in:
@@ -20,7 +20,6 @@ import com.example.android.apis.R;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.*;
|
import android.graphics.*;
|
||||||
import android.graphics.utils.BoundaryPatch;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.*;
|
import android.view.*;
|
||||||
import android.util.FloatMath;
|
import android.util.FloatMath;
|
||||||
@@ -32,83 +31,97 @@ public class BitmapMesh extends GraphicsActivity {
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(new SampleView(this));
|
setContentView(new SampleView(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SampleView extends View {
|
private static class SampleView extends View {
|
||||||
private static final int ROWS = 16;
|
private static final int WIDTH = 20;
|
||||||
private static final int COLS = 16;
|
private static final int HEIGHT = 20;
|
||||||
|
private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);
|
||||||
|
|
||||||
|
private final Bitmap mBitmap;
|
||||||
|
private final float[] mVerts = new float[COUNT*2];
|
||||||
|
private final float[] mOrig = new float[COUNT*2];
|
||||||
|
|
||||||
|
private final Matrix mMatrix = new Matrix();
|
||||||
|
private final Matrix mInverse = new Matrix();
|
||||||
|
|
||||||
private BoundaryPatch mPatch;
|
private static void setXY(float[] array, int index, float x, float y) {
|
||||||
private float[] mCubics;
|
array[index*2 + 0] = x;
|
||||||
private Paint mPaint0;
|
array[index*2 + 1] = y;
|
||||||
private Paint mPaint1;
|
}
|
||||||
private int mCurrIndex = -1;
|
|
||||||
|
|
||||||
public SampleView(Context context) {
|
public SampleView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
setFocusable(true);
|
setFocusable(true);
|
||||||
|
|
||||||
Bitmap bm = BitmapFactory.decodeResource(getResources(),
|
mBitmap = BitmapFactory.decodeResource(getResources(),
|
||||||
R.drawable.beach);
|
R.drawable.beach);
|
||||||
|
|
||||||
mPatch = new BoundaryPatch();
|
float w = mBitmap.getWidth();
|
||||||
mPatch.setTexture(bm);
|
float h = mBitmap.getHeight();
|
||||||
|
// construct our mesh
|
||||||
float unit = 90;
|
int index = 0;
|
||||||
mCubics = new float[] {
|
for (int y = 0; y <= HEIGHT; y++) {
|
||||||
0, 0, 1, 0, 2, 0,
|
float fy = h * y / HEIGHT;
|
||||||
3, 0, 3, 1, 3, 2,
|
for (int x = 0; x <= WIDTH; x++) {
|
||||||
3, 3, 2, 3, 1, 3,
|
float fx = w * x / WIDTH;
|
||||||
0, 3, 0, 2, 0, 1
|
setXY(mVerts, index, fx, fy);
|
||||||
};
|
setXY(mOrig, index, fx, fy);
|
||||||
for (int i = 0; i < 24; i++) {
|
index += 1;
|
||||||
mCubics[i] *= 90;
|
}
|
||||||
mCubics[i] += 20;
|
}
|
||||||
}
|
|
||||||
mPatch.setCubicBoundary(mCubics, 0, ROWS, COLS);
|
mMatrix.setTranslate(10, 10);
|
||||||
|
mMatrix.invert(mInverse);
|
||||||
mPaint0 = new Paint();
|
}
|
||||||
mPaint0.setAntiAlias(true);
|
|
||||||
mPaint0.setStrokeWidth(12);
|
@Override protected void onDraw(Canvas canvas) {
|
||||||
mPaint0.setStrokeCap(Paint.Cap.ROUND);
|
canvas.drawColor(0xFFCCCCCC);
|
||||||
mPaint1 = new Paint(mPaint0);
|
|
||||||
mPaint1.setColor(0xFFFFFFFF);
|
canvas.concat(mMatrix);
|
||||||
mPaint1.setStrokeWidth(10);
|
canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
|
||||||
}
|
null, 0, null);
|
||||||
|
}
|
||||||
@Override protected void onDraw(Canvas canvas) {
|
|
||||||
canvas.drawColor(0xFFCCCCCC);
|
private void warp(float cx, float cy) {
|
||||||
mPatch.draw(canvas);
|
final float K = 10000;
|
||||||
canvas.drawPoints(mCubics, mPaint0);
|
float[] src = mOrig;
|
||||||
canvas.drawPoints(mCubics, mPaint1);
|
float[] dst = mVerts;
|
||||||
}
|
for (int i = 0; i < COUNT*2; i += 2) {
|
||||||
|
float x = src[i+0];
|
||||||
private int findPtIndex(float x, float y) {
|
float y = src[i+1];
|
||||||
final float tolerance = 25;
|
float dx = cx - x;
|
||||||
final float[] pts = mCubics;
|
float dy = cy - y;
|
||||||
for (int i = 0; i < (pts.length >> 1); i++) {
|
float dd = dx*dx + dy*dy;
|
||||||
if (Math.abs(pts[i*2 + 0] - x) <= tolerance &&
|
float d = FloatMath.sqrt(dd);
|
||||||
Math.abs(pts[i*2 + 1] - y) <= tolerance) {
|
float pull = K / (dd + 0.000001f);
|
||||||
return i*2;
|
|
||||||
|
pull /= (d + 0.000001f);
|
||||||
|
// android.util.Log.d("skia", "index " + i + " dist=" + d + " pull=" + pull);
|
||||||
|
|
||||||
|
if (pull >= 1) {
|
||||||
|
dst[i+0] = cx;
|
||||||
|
dst[i+1] = cy;
|
||||||
|
} else {
|
||||||
|
dst[i+0] = x + dx * pull;
|
||||||
|
dst[i+1] = y + dy * pull;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int mLastWarpX = -9999; // don't match a touch coordinate
|
||||||
|
private int mLastWarpY;
|
||||||
|
|
||||||
@Override public boolean onTouchEvent(MotionEvent event) {
|
@Override public boolean onTouchEvent(MotionEvent event) {
|
||||||
float x = event.getX();
|
float[] pt = { event.getX(), event.getY() };
|
||||||
float y = event.getY();
|
mInverse.mapPoints(pt);
|
||||||
switch (event.getAction()) {
|
|
||||||
case MotionEvent.ACTION_DOWN:
|
int x = (int)pt[0];
|
||||||
mCurrIndex = findPtIndex(x, y);
|
int y = (int)pt[1];
|
||||||
break;
|
if (mLastWarpX != x || mLastWarpY != y) {
|
||||||
case MotionEvent.ACTION_MOVE:
|
mLastWarpX = x;
|
||||||
if (mCurrIndex >= 0) {
|
mLastWarpY = y;
|
||||||
mCubics[mCurrIndex + 0] = x;
|
warp(pt[0], pt[1]);
|
||||||
mCubics[mCurrIndex + 1] = y;
|
invalidate();
|
||||||
mPatch.setCubicBoundary(mCubics, 0, ROWS, COLS);
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user