Why does this AI class doesn't draw to the canvas?
package com.dragon.game;
import android.graphics.*;
import com.dragon.game.Game.*;
import java.util.*;
public class AI {
public static Bitmap a;
int x, y, dirX, dirY;
int width, height;
long start, stop;
boolean fg = true;
SurView ov;
Random r = new Random();
public AI(SurView surView, Bitmap ai){
a = ai;
ov = surView;
width = a.getWidth();
height = a.getHeight();
x = Remote.wt / 4;
y = Remote.wt / 4;
}
public void update(){
x++;
}
public void onDraw(Canvas canvas){
if(fg){
x = Remote.wt / 4;
y = Remote.wt / 4;
fg = false;
}
update();
Rect src = new Rect(0, 0, width, height);
Rect dst = new Rect(x, y, x + Remote.pw, y + Remote.pw);
canvas.drawBitmap(a, src, dst, null);
}
}
The reason is that you have initialized Bitmap a inside Public AI method and then you are using it inside onDraw method , you cannot do this , either you have to initialize it globally or you have to give a reference to it
Related
I am using MPAndroidChart and need to implement a BarChart to display user scores with user profile pictures.
Can anyone help me to customize the BarChart ?
Thanks
I solved my problem by using a custom BarChartRenderer.
Below is the source code of custom BarRenderer :
import android.graphics.Bitmap;
import android.graphics.Canvas;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.renderer.BarChartRenderer;
import com.github.mikephil.charting.utils.ViewPortHandler;
/**
* Created by hemantchhonkar on 21-02-2018.
*/
public class BarChartImageRenderer extends BarChartRenderer {
private final Bitmap[] imageToRender;
public BarChartImageRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler, Bitmap[] imageToRender) {
super(chart, animator, viewPortHandler);
this.imageToRender=imageToRender;
}
#Override
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
super.drawDataSet(c, dataSet, index);
drawBarImages(c, dataSet, index);
}
protected void drawBarImages(Canvas c, IBarDataSet dataSet, int index) {
BarBuffer buffer = mBarBuffers[index];
float left; //avoid allocation inside loop
float right;
float top;
float bottom;
for (int j = 0; j < buffer.buffer.length * mAnimator.getPhaseX(); j += 4) {
left = buffer.buffer[j];
right = buffer.buffer[j + 2];
top = buffer.buffer[j + 1];
bottom = buffer.buffer[j + 3];
float x = (left + right) / 2f;
if (!mViewPortHandler.isInBoundsRight(x))
break;
if (!mViewPortHandler.isInBoundsY(top)
|| !mViewPortHandler.isInBoundsLeft(x))
continue;
BarEntry entry = dataSet.getEntryForIndex(j / 4);
float val = entry.getY();
final Bitmap scaledBarImage = scaleBarImage(buffer,imageToRender[j / 4]);
int starWidth = scaledBarImage.getWidth();
int starOffset = starWidth / 2;
drawImage(c, scaledBarImage, x - starOffset, top);
}
}
private Bitmap scaleBarImage(BarBuffer buffer, Bitmap image) {
float firstLeft = buffer.buffer[0];
float firstRight = buffer.buffer[2];
int firstWidth = (int) Math.ceil(firstRight - firstLeft);
return Bitmap.createScaledBitmap(image, firstWidth, firstWidth, false);
}
protected void drawImage(Canvas c, Bitmap image, float x, float y) {
if (image != null) {
c.drawBitmap(image, x, y, null);
}
}
}
Then set the renderer into the chart in your activity/fragment:
//Create and array of images(this array size should match the size of dataset)
Bitmap[] starBitmap = new Bitmap[]{ BitmapFactory.decodeResource(getResources(), R.drawable.my_dp),
BitmapFactory.decodeResource(getResources(), R.drawable.profile_pic),
BitmapFactory.decodeResource(getResources(), R.drawable.my_dp),
BitmapFactory.decodeResource(getResources(), R.drawable.profile_pic),
BitmapFactory.decodeResource(getResources(), R.drawable.my_dp),
BitmapFactory.decodeResource(getResources(), R.drawable.profile_pic),
BitmapFactory.decodeResource(getResources(), R.drawable.my_dp),
BitmapFactory.decodeResource(getResources(), R.drawable.profile_pic),
BitmapFactory.decodeResource(getResources(), R.drawable.my_dp),
BitmapFactory.decodeResource(getResources(), R.drawable.my_dp)};
//Create the object of the chart renderer and set into the chart
barChart2.setRenderer(new BarChartImageRenderer(barChart2, barChart2.getAnimator(), barChart2.getViewPortHandler(), starBitmap));
Screenshot of the rendered chart
I've run into a wall with this basic drawRect not showing anything and I cannot figure out why.
onDraw
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.drawCircle(circle1x, circle1y, circleRadius, circlePaint);
canvas.drawCircle(circle2x, circle2y, circleRadius, circlePaint);
canvas.drawRect(rect, rectPaint);
}
setupCropping
Is running before the onDraw and is called from the View constructor to set up all the var's
private void setupCropping() {
final float scale = getContext().getResources().getDisplayMetrics().density;
circleRadius = (int) (circleRadiusDp * scale + 0.5f);
DisplayMetrics metrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
displayX = metrics.widthPixels;
displayY = metrics.heightPixels;
cropAreaY = displayY / 3;
cropAreaX = displayX;
//Setting up the circles for adjusting
circle1x = displayX / 2;
circle1y = displayY / 2 - (cropAreaY / 2);
circle2x = displayX / 2;
circle2y = displayY / 2 + (cropAreaY / 2);
canvasPaint = new Paint();
canvasPaint.setColor(0xffffff00);
circlePaint = new Paint();
circlePaint.setColor(0xffffff00);
circlePaint.setAntiAlias(true);
rectPaint = new Paint();
rectPaint.setColor(0xffffff00);
rect = new Rect();
rect.set(0, circle1y, 0, displayY - cropAreaY - circle1y);
}
The drawCircle works perfectly and draws as I'd expect it, I've checked the numbers being given to drawRect and they are set as they should be so I really don't know what could be going wrong here.
Full View class
package com.samplersnapshoot.domiq.samplersnapshoot;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.CountDownLatch;
/**
* Created by domix on 14.8.2015..
*/
public class CroppingView extends View {
public final String TAG = "DEBUG";
private Canvas cropCanvas;
private Bitmap canvasBitmap;
private int displayX;
private int displayY;
private int circle1x = 0;
private int circle2x = 0;
private int circle1y = 0;
private int circle2y = 0;
private int circleRadiusDp = 20;
private int circleRadius = 100;
private int cropAreaX = 0;
private int cropAreaY = 0;
private Rect rect;
private Paint canvasPaint;
private Paint circlePaint;
private Paint rectPaint;
public CroppingView(Context context, AttributeSet attrs){
super(context, attrs);
setupCropping();
}
private void setupCropping() {
final float scale = getContext().getResources().getDisplayMetrics().density;
circleRadius = (int) (circleRadiusDp * scale + 0.5f);
DisplayMetrics metrics = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(metrics);
displayX = metrics.widthPixels;
displayY = metrics.heightPixels;
cropAreaY = displayY / 3;
cropAreaX = displayX;
//Setting up the circles for adjusting
circle1x = displayX / 2;
circle1y = displayY / 2 - (cropAreaY / 2);
circle2x = displayX / 2;
circle2y = displayY / 2 + (cropAreaY / 2);
canvasPaint = new Paint();
canvasPaint.setColor(0xffffff00);
circlePaint = new Paint();
circlePaint.setColor(0xffffff00);
circlePaint.setAntiAlias(true);
rectPaint = new Paint();
rectPaint.setARGB(50, 135, 225, 255);
}
/*#Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
displayX = widthMeasureSpec;
displayY = heightMeasureSpec;
invalidate();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}*/
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//Getting bitmap
getPath myPath = new getPath();
final File myFile = myPath.getLastModifiedFile();
final CountDownLatch latch = new CountDownLatch(1);
Thread getCanvasBitmap = new Thread() {
public void run() {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
int i = 0;
while (canvasBitmap == null && ++i < 500) {
System.gc();
Log.d(TAG, "Trying again: " + i);
canvasBitmap = BitmapFactory.decodeFile(myFile.getAbsolutePath(), opt);
}
latch.countDown();
}
};
getCanvasBitmap.start();
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
//Turning into mutable bitmap
myFile.getParentFile().mkdirs();
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(myFile, "rw");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int bWidth = canvasBitmap.getWidth();
int bHeight = canvasBitmap.getHeight();
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer map = null;
try {
map = channel.map(FileChannel.MapMode.READ_WRITE, 0, bWidth*bHeight*4);
} catch (IOException e) {
e.printStackTrace();
}
canvasBitmap.copyPixelsToBuffer(map);
canvasBitmap.recycle();
this.canvasBitmap = Bitmap.createBitmap(bWidth, bHeight, Bitmap.Config.ARGB_8888);
map.position(0);
this.canvasBitmap.copyPixelsFromBuffer(map);
try {
channel.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
Log.d(TAG, "Display vars:" + displayX + " " + displayY);
canvas.drawCircle(circle1x, circle1y, circleRadius, circlePaint);
canvas.drawCircle(circle2x, circle2y, circleRadius, circlePaint);
rect = new Rect();
rect.set(5, circle1y, displayX, displayY - cropAreaY - circle1y);
canvas.drawRect(rect, rectPaint);
}
}
The class is far from well coded, I have yet to integrate a lot of functionality and clean it up.
rect.set(0, circle1y, 0, displayY - cropAreaY - circle1y);
Both your left and right coordinates are 0. Go figure.
It's
rect.set(int left, int top, int right, int bottom);
EDIT:
Okay I managed to pinpoint the issue. The following line is in your View class
rect.set(5, circle1y, displayX, displayY - cropAreaY - circle1y);
Using the same calculations in your code, for a device with a 480x800 display, I'm getting the following coordinate values.
rect.set(5, 267, 480, 267);
Again, you have overlapping sides of your Rect; Both your top and bottom sides are on the same Y coordinate. This will produce a rectangle of 262 pixels wide and ZERO HEIGHT.
All you need to do is to update your coordinates calculation and supply the proper coordinates. Otherwise, your Rect should draw just fine.
I have been on this for days without finding a good solution (at least to me).
Basically what I need to do is draw an animated dotted curve, that would represent a missile trajectory for a basic game. This trajectory would accept starting, middle and final x,y. I read a lot of Q/A about animating curves or even straight lines just to begin, but they look really complex compared to what I have to do.
I have a button with an onclick listener which triggers the missile launch, but until now it just draws static lines without any animation.
Do you remember this? That's what i would like to achieve:
http://www.youtube.com/watch?v=UDc3ZEKl-Wc
Banana trajectory is not painted but, you got the idea.
Here again, starting from your example i tried to draw simultaneous curves using arrays and for loops, but what i got is that sometimes, not always, app crashes with a nullpointer exception, don't know why. What am i doing wrong? Here's my code:
public class DrawDottedCurve extends View {
Path[] path = new Path[8];
Path[] drawingPath = new Path[8];
PathMeasure[] measure = new PathMeasure[8];
Path[] segmentPath = new Path[8];
float[] length = new float[8];
float[] start = new float[8];
float[] percent = new float[8];
Paint paint = new Paint();
float x1;
float y1;
float x3;
float y3;
long k = 0;
Canvas canvas;
Random r = new Random();
public DrawDottedCurve(Context context, int a, int b, int c, int d) {
super(context);
x1 = a;
y1 = b;
x3 = c;
y3 = d;
paint.setAlpha(255);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[] { 2, 4 }, 50));
for (int i = 0; i < 8; i++) {
k = r.nextInt(100 - 30) + 30;
path[i] = new Path();
path[i].moveTo(x1 + k, y1 + k); //
path[i].quadTo((x3 + k - x1 + k) / 2, (y3 + k - y1 + k) / 2,
x3 + k, y3 + k); // Calculate Bezier Curves
}
final long DRAW_TIME = 10000;
CountDownTimer timer = new CountDownTimer(DRAW_TIME, 100) {
#Override
public void onTick(long millisUntilFinished) {
Log.d("Timer", "Inizio");
for (int i = 0; i < 8; i++) {
start[i] = 0;
measure[i] = new PathMeasure();
measure[i].setPath(path[i], false);
percent[i] = ((float) (DRAW_TIME - millisUntilFinished))
/ (float) DRAW_TIME;
segmentPath[i] = new Path();
drawingPath[i] = new Path();
length[i] = measure[i].getLength() * percent[i];
measure[i].getSegment(start[i], length[i], segmentPath[i],
true);
start[i] = length[i];
drawingPath[i].addPath(segmentPath[i]);
}
invalidate();
;
}
#Override
public void onFinish() {
for (int i = 0; i < 8; i++) {
measure[i].getSegment(start[i], measure[i].getLength(),
segmentPath[i], true);
drawingPath[i].addPath(segmentPath[i]);
}
invalidate();
Log.d("Timer", "Fine");
}
};
timer.start();
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < 8; i++) {
canvas.drawPath(drawingPath[i], paint);
invalidate();
}
}
}
Assuming that you have a Canvas to draw on and a path you want to trace this code should do the work for animating it.
private void init() {
missilePath = new Path();
missileDrawPath = new Path();
mPaint = new Paint();
mPaint.setStrokeWidth(3);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Style.STROKE);
mPaint.setAntiAlias(true);
PathEffect dashEffect = new DashPathEffect(new float[] {2, 4}, 0);
mPaint.setPathEffect(dashEffect);
final long DRAW_TIME = 5000;
timer = new CountDownTimer(DRAW_TIME, 100) {
PathMeasure measure = new PathMeasure();
Path segmentPath = new Path();
float start = 0;
#Override
public void onTick(long millisUntilFinished) {
measure.setPath(missilePath, false);
float percent = ((float) (DRAW_TIME - millisUntilFinished)) / (float) DRAW_TIME;
float length = measure.getLength() * percent;
measure.getSegment(start, length, segmentPath, true);
start = length;
missileDrawPath.addPath(segmentPath);
invalidate();
}
#Override
public void onFinish() {
measure.getSegment(start, measure.getLength(), segmentPath, true);
missileDrawPath.addPath(segmentPath);
invalidate();
}
};
timer.start();
}
#Override
public void onDraw(Canvas c) {
super.onDraw(c);
c.drawPath(missileDrawPath, mPaint);
}
Basically a timer gets started and depending on the elapsed time a segment is retrieved from missilePath and added to the path that should be currently drawn.
Thank you SceLus that worked really well, here's the full code for people eventually stuck on same problem.
Class constructor takes Bezier Curve starting and final x's and y's as input and simply calculate mid control point, then draws everything according to SceLus code.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.CountDownTimer;
import android.view.View;
public class DrawDottedCurve extends View {
Path path = new Path();
Path drawingPath = new Path();
Paint paint = new Paint();
float x1;
float y1;
float x3;
float y3;
public DrawDottedCurve(Context context, int a, int b, int c, int d) {
super(context);
x1 = a;
y1 = b;
x3 = c;
y3 = d;
paint.setAlpha(255);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[] { 2, 4 }, 50));
path.moveTo(x1, y1); //
path.quadTo((x3 - x1) / 2, (y3 - y1) / 2, x3, y3); // Calculate Bezier Curve
final long DRAW_TIME = 10000;
CountDownTimer timer = new CountDownTimer(DRAW_TIME, 100) {
PathMeasure measure = new PathMeasure();
Path segmentPath = new Path();
float start = 0;
#Override
public void onTick(long millisUntilFinished) {
measure.setPath(path, false);
float percent = ((float) (DRAW_TIME - millisUntilFinished))
/ (float) DRAW_TIME;
float length = measure.getLength() * percent;
measure.getSegment(start, length, segmentPath, true);
start = length;
drawingPath.addPath(segmentPath);
invalidate();
}
#Override
public void onFinish() {
measure.getSegment(start, measure.getLength(), segmentPath,
true);
drawingPath.addPath(segmentPath);
invalidate();
}
};
timer.start();
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(drawingPath, paint);
}
}
i created a sprite class and in this example it runs a spritesheet of 44 frames, i want to set the frame rate to 10fps so the all animation will be 4 seconds more or less.
this is my code, it works but with very slow rate and i cant understand why:
package sprite2.sprite2;
import sprite2.sprite2.Sprite2Activity.Panel;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Sprite {
int x,y;
int xSpeed, ySpeed;
int height, width;
Bitmap b;
Panel ov;
int currentFrame = 0;
int direction = 0;
int framePeriod;
long frameTicker;
int frameNr;
public Sprite(Panel panel, Bitmap _scratch) {
b = _scratch;
ov = panel;
height = 480;
width = b.getWidth()/44;
x = y = 0;
ySpeed = 0;
framePeriod = 1000 / 10;
frameTicker = 0;
frameNr = 44;
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
update(System.currentTimeMillis());
int srcX = currentFrame * width;
Rect src = new Rect (srcX, 0 , srcX+width, height);
Rect dest = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawBitmap(b, src, dest, null);
}
public void update(long videotime) {
if (videotime > frameTicker + framePeriod) {
frameTicker = videotime;
// increment the frame
currentFrame++;
if (currentFrame >= frameNr) {
currentFrame = 0;
}
}
}
}
I've some sample code that comes with Android that distorts a bitmap image(Bitmapmesh.java). I'm wanting a circle placed on my image that gives a fisheye effect. I'm new to android and especially graphics, is it possible to create this effect in the bitmapmesh sample?
I'm not sure where to start with this so any pointers would be appreciated. Can anyone give me a high level view of what's involved, eg i'd like to place a circle on the image firstly. i've placed buttons over images before that seem to float, this was done by using a relative layout then adding child buttons. what i'm tring to do now is different and will probably involve calling some onDraw method? i also have an algorithm that does the distortion, i'm just not sure how to apply this to the image.
Below is the bitmapmesh code. Can anyone talk me through where to start, even if it's just placing the circle on the image first, then i can tackle implementing the effect.
thanks mat
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import android.content.Context;
import android.graphics.;
import android.os.Bundle;
import android.os.Environment;
import android.view.;
import android.util.FloatMath;
public class BitMapFishEye extends GraphicsActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private static final int WIDTH = 20;
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 File tempFile;
private byte[] imageArray;
private static void setXY(float[] array, int index, float x, float y) {
array[index*2 + 0] = x;
array[index*2 + 1] = y;
}
public SampleView(Context context) {
super(context);
setFocusable(true);
/* mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.beach);*/
tempFile = new File(Environment.getExternalStorageDirectory().
getAbsolutePath() + "/"+"image.jpg");
imageArray = new byte[(int)tempFile.length()];
try{
InputStream is = new FileInputStream(tempFile);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
int i = 0;
while (dis.available() > 0) {
imageArray[i] = dis.readByte();
i++;
}
dis.close();
} catch (Exception e) {
e.printStackTrace();
}
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 5;
mBitmap = BitmapFactory.decodeByteArray(imageArray, 0, imageArray.length, bfo);
float w = mBitmap.getWidth();
float h = mBitmap.getHeight();
// construct our mesh
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;
}
}
mMatrix.setTranslate(10, 10);
mMatrix.invert(mInverse);
}
#Override protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFCCCCCC);
canvas.concat(mMatrix);
canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
null, 0, null);
}
private void warp(float cx, float cy) {
final float K = 10000;
float[] src = mOrig;
float[] dst = mVerts;
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 = FloatMath.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) {
dst[i+0] = cx;
dst[i+1] = cy;
} else {
dst[i+0] = x + dx * pull;
dst[i+1] = y + dy * pull;
}
}
}
private int mLastWarpX = -9999; // don't match a touch coordinate
private int mLastWarpY;
#Override public boolean onTouchEvent(MotionEvent event) {
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();
}
return true;
}
}
}