A
Andr_green
Neues Mitglied
- 2
Hallo,
ich habe ein Problem mit einer View.
Die View zeigt ein über die Bildschirmgrösse hinausgehendes Image an.
Dieses Image kann per Drag verschoben oder gezoomt werden.
Das Drag und das Zoomen funktionieren auch bis auf ein paar markel und die hoffe ich kann ich mit eurer Hilfe herausnehmen.
Die Markel bestehen im folgenden:
Hier ein paar Code auszüge:
Der Code ist aus verschiedenen Quellen zusammen gefügt.
Wäre echt toll wenn einem von euch auffallen würde, wo das Problem liegt.
Ich glaub ich seh den Wald vor Bäumen nicht mehr .
DANKE.
LG
Andr
ich habe ein Problem mit einer View.
Die View zeigt ein über die Bildschirmgrösse hinausgehendes Image an.
Dieses Image kann per Drag verschoben oder gezoomt werden.
Das Drag und das Zoomen funktionieren auch bis auf ein paar markel und die hoffe ich kann ich mit eurer Hilfe herausnehmen.
Die Markel bestehen im folgenden:
- nach dem das Image gezoomt wurde, wird beim drag der untere oder rechte Rand des Images ignoeriert. Statt dessen wird die Bewegung in die andere Richtung (sprich nach links ober oben) fortgesetzt.
- Ebenfalls wird nach dem Image gezoomt wurde, springt das Bild zu einer anderen Bildposition wenn der Finger zum drag angesetzt wird.
Hier ein paar Code auszüge:
Code:
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int newScrollRectY = 0;
int newScrollRectX = 0;
canvas.save();
canvas.scale(mScaleFactor, mScaleFactor, mLastTouchX, mLastTouchY);
// in case the bitmap height is greater then the display
if (bm.getHeight() > displayHeight) {
newScrollRectY = scrollRectY - (int) scrollByY;
// Don't scroll off the top or bottom edges of the bitmap.
if (newScrollRectY <= 0) {
newScrollRectY = 0;
} else if (newScrollRectY > (bm.getHeight() - displayHeight)) {
newScrollRectY = (int) ((bm.getHeight()) - displayHeight);
}
} else {
scrollRectY = 0;
}
// in case the width is greater then the display
if (bm.getWidth() > displayWidth) {
newScrollRectX = scrollRectX - (int) scrollByX;
// Don't scroll off the left or right edges of the bitmap.
if (newScrollRectX <= 0)
newScrollRectX = 0;
else if (newScrollRectX > (bm.getWidth() - displayWidth))
newScrollRectX = (int) ((bm.getWidth() - displayWidth));
} else {
scrollRectX = 0;
}
scrollRect.set(newScrollRectX, newScrollRectY, newScrollRectX
+ displayWidth, newScrollRectY + displayHeight);
Paint paint = new Paint();
canvas.drawBitmap(bm, scrollRect, displayRect, paint);
canvas.restore();
scrollRectX = newScrollRectX;
scrollRectY = newScrollRectY;
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
mode = ZOOM;
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(min_zoom,
Math.min(mScaleFactor, max_zoom));
invalidate();
return true;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
// Fit to screen.
float scale;
float scaleX = (float) width / (float) bm.getWidth();
float scaleY = (float) height / (float) bm.getHeight();
scale = Math.min(scaleX, scaleY);
// saveScale = 1f;
invalidate();
// Center the image
scrollByY = (float) height - (scale * (float) bm.getHeight());
scrollByX = (float) width - (scale * (float) bm.getWidth());
scrollByY /= (float) 2;
scrollByX /= (float) 2;
origWidth = width - 2 * scrollByX;
origHeight = height - 2 * scrollByY;
right = width * mScaleFactor - width
- (2 * scrollByX * mScaleFactor);
bottom = height * mScaleFactor - height
- (2 * scrollByY * mScaleFactor);
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
x = ev.getX();
y = ev.getY();
mLastTouchX = x; // reset initial values to latest
mLastTouchY = y;
mode = DRAG;
// Save the ID of this pointer
mActivePointerId = ev.getPointerId(0);
break;
case MotionEvent.ACTION_MOVE:
// Find the index of the active pointer and fetch its position
int pointerIndex = ev.findPointerIndex(mActivePointerId);
x = ev.getX(pointerIndex);
y = ev.getY(pointerIndex);
if (mode == DRAG) {
scrollByX = x - mLastTouchX; // move update x increment
scrollByY = y - mLastTouchY; // move update y increment
}
mLastTouchX = x; // reset initial values to latest
mLastTouchY = y;
invalidate();
break;
case MotionEvent.ACTION_UP:
mode = NONE;
mActivePointerId = INVALID_POINTER_ID;
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
case MotionEvent.ACTION_CANCEL:
mActivePointerId = INVALID_POINTER_ID;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode = ZOOM;
break;
}
return true;
}
Wäre echt toll wenn einem von euch auffallen würde, wo das Problem liegt.
Ich glaub ich seh den Wald vor Bäumen nicht mehr .
DANKE.
LG
Andr