我有一个显示视图的活动,在视图中有一个画布是画画,然后我有一个ontouch函数,它应该允许画布被拖动/缩放。但是,在调试时它似乎正常工作,除了画布没有移动。我认为我需要调用invalidate,这是对的吗?如果是这样我应该在哪里打电话?
感谢您提供任何帮助。
Touch.java
package org.example.touch;
import android.app.Activity;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
public class Touch extends Activity implements OnTouchListener {
private static final String TAG = "Touch";
// These matrices will be used to move and zoom image
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView view = new GridView(this);
setContentView(view);
// ImageView view = (ImageView) findViewById(R.id.imageView);
view.setOnTouchListener(this);
// ...
// Work around a Cupcake bug
matrix.setTranslate(1f, 1f);
view.setImageMatrix(matrix);
}
@Override
public boolean onTouch(View v, MotionEvent rawEvent) {
WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent);
// ...
GridView view = (GridView) v;
// Dump touch event to log
dumpEvent(event);
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x,
event.getY() - start.y);
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
/** Show an event in the LogCat view, for debugging */
private void dumpEvent(WrapMotionEvent event) {
// ...
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
Log.d(TAG, sb.toString());
}
/** Determine the space between the first two fingers */
private float spacing(WrapMotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, WrapMotionEvent event) {
// ...
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
和视图,GridView.java
package org.example.touch;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.Display;
import android.graphics.Path;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.widget.ImageView;
public class GridView extends ImageView {
private int canvasWidth;
private int canvasHeight;
// private Config config;
// private Bitmap bitmap;
// private Canvas canvas;
public GridView(Context context) {
super(context);
// config = Bitmap.Config.ARGB_8888;
// bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, config);
// canvas = new Canvas(bitmap);
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvasWidth = canvas.getWidth();
canvasHeight = canvas.getHeight();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
canvas.drawRect(0, 0, (float)canvasWidth, (float)canvasHeight, paint);
int Grid_size = canvasWidth / 32;
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
for (int i = 1; i < 100; i++) {
canvas.drawLine((Grid_size*i), 10, (Grid_size*i), 10+(Grid_size*10), paint);
}
paint.setColor(Color.RED);
for (int i = 1; i < 12; i++) {
canvas.drawLine(10, (Grid_size*i), (Grid_size*99), (Grid_size*i), paint);
}
final int FS= 500; //Sampling Frequency
final int WIN=FS*20;
//create a test sine wave
short[]wave = new short[WIN];
int frequency = 1;
int amplitude = 40;
for (int n=0; n<WIN; n++)
wave[n] = (short) (60.0+amplitude*Math.sin(6.282*frequency*n/FS));
//To display the waveform
Path path2 = new Path();
path2.moveTo(10, 60);
for (float x2=1; x2<WIN; x2++)
path2.lineTo(10+(3*x2/50), wave[(int) x2]);
Paint paint3 = new Paint();
paint3.setColor(Color.BLACK);
paint3.setStyle(Style.STROKE);
canvas.drawPath(path2, paint3);
}
//public void invalidate ();
public void setImageMatrix(Matrix matrix) {
super.setImageMatrix(matrix);
}}
答案 0 :(得分:3)
当您想要重绘屏幕时调用invalidate。如果你想要它不断更新你可以在onDraw舞台的末尾调用它,它只会反复绘制它。
如果您只需要在缩放后重绘屏幕,那么在缩放功能完成后再执行此操作