Forum » Programiranje » Android move/skaliranje
Android move/skaliranje
g333kk ::
Pozdravljeni, delam svojo prvo aplikacijo in imam težavo,.. Našel sem nek primer ki omogoča zoom/move/scaling, ki ga najadete na slednjem linku: https://github.com/droidcode/ThreadPaint
-zoom mi je uspelo usposobiti
-move mi enostavno ne prime
-scaling prst je nekje črta drugje
Primer:
_______
| |
| ++++|
| |
__----_
+ so na mestih kjer je bil prst, - pa na mestih kjer se na ekranu pojavi črta ampak le v primeru ko uprabljam zoom.
Najboljš da kar začnem z kodo:
Sicer problem ni tako obsežen kot zgleda,ko pritisnem glumb mi enostavno noče zaznati da se mora zadeva premikat, ce lahko pogledate, kaj bi bilo lahko narobe, če pa kdo hoče sprobat celoten paket mi naj napiše. Lepo lepo prosim za pomoč in če se da do četrtka :) Vsem se že vnaprej zahvaljujem.
DrawHandler.java:
-zoom mi je uspelo usposobiti
-move mi enostavno ne prime
-scaling prst je nekje črta drugje
Primer:
_______
| |
| ++++|
| |
__----_
+ so na mestih kjer je bil prst, - pa na mestih kjer se na ekranu pojavi črta ampak le v primeru ko uprabljam zoom.
Najboljš da kar začnem z kodo:
Sicer problem ni tako obsežen kot zgleda,ko pritisnem glumb mi enostavno noče zaznati da se mora zadeva premikat, ce lahko pogledate, kaj bi bilo lahko narobe, če pa kdo hoče sprobat celoten paket mi naj napiše. Lepo lepo prosim za pomoč in če se da do četrtka :) Vsem se že vnaprej zahvaljujem.
DrawHandler.java:
private float xTouchCoordinate; private float yTouchCoordinate; private float previousX; private float previousY; private float oldDist; private boolean hasMoved; private boolean pinchToZoom; Point point; public void handleTouchEvent(MotionEvent event) { Point point1 = new Point(event.getX(), event.getY()); point=point1; xTouchCoordinate = event.getX(); yTouchCoordinate = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: previousX = xTouchCoordinate; previousY = yTouchCoordinate; hasMoved = false; break; case MotionEvent.ACTION_MOVE: float dx = Math.abs(xTouchCoordinate - previousX); float dy = Math.abs(yTouchCoordinate - previousY); if (dx > moveThreshold || dy > moveThreshold) { hasMoved = true; } break; } // always allow pinch to zoom handlePinchToZoom(event); // if zooming, don't handle tools if (!pinchToZoom) { switch (SELECTED_SHAPE) { case LINE: case FREEHAND: case RECTANGLE: case CIRCLE: case OVAL: case ERASE: case FILL: case SPRAY: handleBrushTool(event); break; case MOVE: handleMoveTool(event); break; } } previousX = xTouchCoordinate; previousY = yTouchCoordinate; } private float spacing(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return FloatMath.sqrt(x * x + y * y); } /** * Scroll the picture. */ private void handleMoveTool(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: int dx = Math.round(xTouchCoordinate - previousX); int dy = Math.round(yTouchCoordinate - previousY); mPanel.scroll(dx, dy); break; } } /** * Zoom into the picture or out of it. */ void handlePinchToZoom(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_POINTER_2_DOWN: oldDist = spacing(event) / mPanel.getZoom(); pinchToZoom = true; break; case MotionEvent.ACTION_MOVE: if (pinchToZoom) { float newDist = spacing(event); if (newDist > 10) { float scale = newDist / oldDist; mPanel.zoom(scale); } } break; default: pinchToZoom = false; } } void handleBrushTool(MotionEvent event) { if (SELECTED_SHAPE == SHAPES.ERASE) { mCurrentPaint = mErasePaint; } else mCurrentPaint = mPaint; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mDownPoint = point; break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_MOVE: setCurrentShape(SELECTED_SHAPE, point); if (mOnDrawListener != null) { if (event.getAction() == MotionEvent.ACTION_MOVE) { mOnDrawListener.onDrawShapeTemp(mCurrentShape, mCurrentPaint); } else { mOnDrawListener.onDrawShape(mCurrentShape, mCurrentPaint); } } break; } if (event.getAction() == MotionEvent.ACTION_UP) { GRAPHICOBJECTS.add(new AuditContainer(mCurrentShape.getPath(), mCurrentPaint)); // undopaint.length; mCurrentShape = null; } }
private void prepareBitmaps() { if (mBitmap == null) { try { displayCanvas = getHolder().lockCanvas(); if (displayCanvas != null) { // Create buffer bitmaps, the original and the preview one. mBitmap = Bitmap.createBitmap(displayCanvas.getWidth(), displayCanvas.getHeight(), Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); tmpBitmap = Bitmap.createBitmap(displayCanvas.getWidth(), displayCanvas.getHeight(), Bitmap.Config.ARGB_8888); tmpCanvas = new Canvas(tmpBitmap); // Make background the same for every bitmap. tmpCanvas.drawColor(mBackgroundColor); mCanvas.drawColor(mBackgroundColor); displayCanvas.drawColor(mBackgroundColor); resetPerspective(); // Initial background drawing. mCanvas.drawBitmap(mBitmap, 0, 0, new Paint()); } } finally { if (displayCanvas != null) getHolder().unlockCanvasAndPost(displayCanvas); } } } public DrawHandler getPainterHandler() { return mDrawHandler; } public boolean onTouchEvent(MotionEvent event) { // Če je ACTION_MOVE pa da se številka poveča iz 1 na 2 prekineš tmpDrawing pa skaliraš sliko. // Če je pa kuj 2, pa direkt začeneš skalirat. // event.getPointerCount() mDrawHandler.handleTouchEvent(event); // mDrawHandler.handleBrushTool(event); return true; } public void drawBitmap(Bitmap btp) { displayCanvas = getHolder().lockCanvas(); displayCanvas.scale(zoom, zoom, surfaceCenter.x, surfaceCenter.y); displayCanvas.translate(scroll.x, scroll.y); // displayCanvas.drawPaint(checkeredPattern); displayCanvas.drawColor(mBackgroundColor); displayCanvas.drawBitmap(btp, 0, 0, null); // displayCanvas.drawPath(pathToDraw, mDrawHandler.getPaint()); getHolder().unlockCanvasAndPost(displayCanvas); } float getZoom() { return zoom; } private void resetPerspective() { zoom = 1f; scroll.set(0, 0); displayCanvas.setMatrix(null); } void scroll(int dx, int dy) { float surfaceZoomedWidth = rectSurface.right / zoom; float surfaceZoomedHeight = rectSurface.bottom / zoom; // Don't scroll if the (zoomed) bitmap is smaller than the surface. if ((surfaceZoomedWidth - rectBitmap.right) > 0 && (surfaceZoomedHeight - rectBitmap.bottom) > 0) { scroll.set(0, 0); } else { scroll.offset(Math.round(dx / zoom), Math.round(dy / zoom)); float pivotX = surfaceCenter.x - (surfaceCenter.x / zoom); float pivotY = surfaceCenter.y - (surfaceCenter.y / zoom); float xMax = (surfaceZoomedWidth - rectBitmap.right) + pivotX; float yMax = (surfaceZoomedHeight - rectBitmap.bottom) + pivotY; if (scroll.x < xMax) { scroll.x = Math.round(xMax); } if (scroll.y < yMax) { scroll.y = Math.round(yMax); } // Make checks for upper left corner after checks for // lower right corner to prevent jumping. if (scroll.x - pivotX > 0) { scroll.x = Math.round(pivotX); } if (scroll.y - pivotY > 0) { scroll.y = Math.round(pivotY); } } //drawBitmap(displayBitmap); } /** * Begin a new path at the specified coordinates on the Bitmap. * * @param x X-Coordinate on the Bitmap. * @param y Y-Coordinate on the Bitmap. */ void startPath(float x, float y) { pathToDraw.rewind(); translate(x, y); pathToDraw.moveTo(translate.x, translate.y); } /** * Continue an unfinished path from the previous to the new coordinates on the Bitmap. * * @param x1 Previous X-Coordinate on the Screen. * @param y1 Previous Y-Coordinate on the Screen. * @param x2 New X-Coordinate on the Screen. * @param y2 New Y-Coordinate on the Screen. */ void updatePath(float x1, float y1, float x2, float y2) { translate((x1 + x2) / 2f, (y1 + y2) / 2f); float cx = translate.x; float cy = translate.y; translate(x2, y2); pathToDraw.quadTo(cx, cy, translate.x, translate.y); } /** * Draw the currently unfinished Path on the Bitmap and rewind it. */ void finishPath() { /* Command command = new Command(bitmapPathPaint, pathToDraw); commandManager.commitCommand(command, bitmapCanvas); pathToDraw.rewind();*/ } private final Point translate = new Point(); // Translate screen coordinates to bitmap coordinates. private void translate(float x, float y) { // Long form: (x - scroll.x * zoom - (center.x - center.x * zoom)) / zoom translate.x = (int) ((x - surfaceCenter.x) / zoom + surfaceCenter.x - scroll.x); translate.y = (int) ((y - surfaceCenter.y) / zoom + surfaceCenter.y - scroll.y); }
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | [Android] pomoč pri postavitvi layoutaOddelek: Programiranje | 2257 (2015) | icurk |
» | [Android] SplošnoOddelek: Programiranje | 2019 (1149) | piki12 |
» | Programiranje problem androidOddelek: Programiranje | 1184 (949) | g333kk |
» | Android zamenjava pogledovOddelek: Programiranje | 1094 (915) | c00L3r |
» | android črtaOddelek: Programiranje | 2419 (1690) | g333kk |