gpt4 book ai didi

android - 我是否需要使用无效调用,如果需要,在哪里?

转载 作者:搜寻专家 更新时间:2023-11-01 09:12:57 25 4
gpt4 key购买 nike

我有一个显示 View 的 Activity ,在 View 中有一个正在绘制的 Canvas ,然后我有一个允许拖动/缩放 Canvas 的 ontouch 函数。但是,在调试时它似乎可以正常工作,只是 Canvas 没有移动。我认为我需要调用 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);
}
}

和 View ,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);

}}

最佳答案

当你想重绘屏幕时调用invalidate。如果你想让它不断更新,你可以在 onDraw 阶段结束时调用它,它会一遍又一遍地绘制它。

如果你只需要在缩放后重绘屏幕,那么在缩放功能完成后重绘

关于android - 我是否需要使用无效调用,如果需要,在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6926803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com