gpt4 book ai didi

scroll - ANDROID - 在图库小部件中滚动、多点触控、平移 ImageView

转载 作者:行者123 更新时间:2023-12-02 05:25:50 25 4
gpt4 key购买 nike

public class PageGallery extends Gallery implements OnScaleGestureListener, OnGestureListener{

private static final String TAG = "Multitouch eventi";
static final int NONE = 0;
static final int DRAG = 1;
int mode = NONE;

private Context c;
private final LayoutInflater mInflater;

private ScaleGestureDetector mScaleDetector;
private GestureDetector mDetector;
private float mScaleFactor = 1.f;

float nuova_distanza_tocchi, vecchia_distanza_tocchi, init_x, init_y;

Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();

PointF mid = new PointF();
PointF start = new PointF();

ImageView pagina;


public PageGallery(Context context, AttributeSet atSet) {
super(context, atSet);
mInflater = LayoutInflater.from(context);
c = context;
mDetector = new GestureDetector(c,this);
mScaleDetector = new ScaleGestureDetector(c, this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
pagina = (ImageView) super.getSelectedView();

if (mDetector.onTouchEvent(event)) { // gesure detector per i movimenti
Log.d("onTouchEvent", "--[ MOVIMENTO ]--");
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
init_x = event.getX();
init_y = event.getY();
midPoint(mid, event);
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
mode = DRAG;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
}
break;
}

pagina = (ImageView) super.getSelectedView();
pagina.setImageMatrix(matrix);

return true;
}
else if(mScaleDetector.onTouchEvent(event)) { // scale detector per lo zoom
Log.d("onTouchEvent", "--[ SCALE ]--");
return true;
}
else
return false;
}


public boolean onScaleBegin(ScaleGestureDetector detector) {
Log.d(TAG, "-- onScaleBegin --");
matrix = pagina.getImageMatrix();
savedMatrix.set(matrix);
start.set(init_x, init_y);
return true;
}

public boolean onScale(ScaleGestureDetector detector) {

mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

if (nuova_distanza_tocchi > 10f) {
matrix.set(savedMatrix);
matrix.postScale(mScaleFactor, mScaleFactor, mid.x, mid.y);
Log.d("ZOOMMING",matrix.toShortString());
}
else {
matrix.set(savedMatrix);
matrix.postTranslate(init_x - start.x, init_y - start.y);
Log.d("PANNING",matrix.toShortString());
}

pagina.setImageMatrix(matrix);

pagina.invalidate();

Log.d("MATRIX", matrix.toString());
return true;
}

public void onScaleEnd(ScaleGestureDetector detector) {
Log.d(TAG, "-- onScaleEnd --");
vecchia_distanza_tocchi = detector.getPreviousSpan();
nuova_distanza_tocchi = detector.getCurrentSpan();
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.d(TAG, "-- onFling --");

float velMax = 2500f;
float velMin = 1000f;
float velX = Math.abs(velocityX);
if (velX > velMax) {
velX = velMax;
} else if (velX < velMin) {
velX = velMin;
}
velX -= 600;
int k = 500000;
int speed = (int) Math.floor(1f / velX * k);
setAnimationDuration(speed);

int kEvent;
if (isScrollingLeft(e1, e2)) {
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
} else {
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);

return true;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.d(TAG, "-- onScroll --");
return super.onScroll(e1, e2, distanceX, distanceY);
}


private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}


private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}

我扩展了一个实现 onScaleGestureListener 的 Gallery,并在主事件中设置了一个 onItemClickListener。

这段代码工作正常:我可以捏缩放图库中的所有 ImageView ,并且可以平移已缩放的 ImageView。但是,当我缩放图像然后又启用时,如何禁用图库滚动呢?对不起,我的英语不好谢谢!

最佳答案

我认为您必须在要处理“触摸”事件的类中调用方法dispatchTouchEvent(),而不是Gallery

关于scroll - ANDROID - 在图库小部件中滚动、多点触控、平移 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5514863/

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