绝对坐标位图android

发布于 2025-01-08 19:24:13 字数 4049 浏览 6 评论 0原文

我有一个尺寸大于屏幕的位图(即 1000x1000)。多亏了画布,我可以拖动这个位图。如果我在点 (500,500) getX 或 getY 不返回 500,否则返回 0 到 320[屏幕大小模拟器]之间的某个值,所以我需要知道相对于点 0 的绝对值。我希望我的视图不将位图平移到视图的 0 个左点和 0 个顶部点以上

我放置了一些代码来澄清问题

public MapaView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    mSurfaceHeight = 640;
    mSurfaceWidth = 480;
    mapaActual = new Mapa();
    mContext = context;
    mBufferedCanvas = new Canvas();

}


    draw(Canvas c){ 

    bitmapAux.eraseColor(Color.WHITE);

   /*draw the rectangles....*/

    c.translate(mPosX, mPosY);
    c.scale(mScaleFactor, mScaleFactor);
    c.drawBitmap(bitmapAux, 0, 0, null);}

我使用下一个方法获得位置

@Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events.
        mScaleDetector.onTouchEvent(ev);


        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            startTime = ev.getEventTime();
            final float x = ev.getX();
            final float y = ev.getY();
            final float xR= ev.getRawX();
            final float yR= ev.getRawY();
            movido=false;
            mLastTouchX = x;
            mLastTouchY = y;
            mLastTouchXRaw = xR;
            mLastTouchYRaw = yR;

            mActivePointerId = ev.getPointerId(0);

            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                movido=true;
                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;
            movido=true;


            break;
        }

        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            endTime = ev.getEventTime();

            if ((endTime - startTime > 500)&&!movido) {

                // we have a 1000ms duration touch
                // propagate your own event
                Log.d("LC", "time touched greater than 1000ms " + endTime + "  "
                        + startTime);

                startTime = 0;
                endTime = 0;
                movido=false;
                Box pepeBox=new Box();
                pepeBox=buscarElemento();
                //Intent intent=new Intent(mContext,OpcionesConceptoList.class);
                //mContext.startActivity(intent);
                /*
                 * return true; notify that you handled this event (do not
                 * propagate)
                 */
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_POINTER_UP: {
            final int 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;
        }

        }



        return true;
    }

有什么建议吗?

预先感谢,请原谅我的英语。

I have a bitmap with a size greater than screen (i.e, 1000x1000). I can drag this bitmap thanks to canvas. If i am in the point (500,500) getX or getY don´t return 500 else some value between 0 and 320[screen size emulator], so I need to know the absolute value with respect to point 0. I want my View not to translate the bitmap more than the 0 Left and 0 Top points of the View

I put some code to clarify the question

public MapaView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
    mSurfaceHeight = 640;
    mSurfaceWidth = 480;
    mapaActual = new Mapa();
    mContext = context;
    mBufferedCanvas = new Canvas();

}


    draw(Canvas c){ 

    bitmapAux.eraseColor(Color.WHITE);

   /*draw the rectangles....*/

    c.translate(mPosX, mPosY);
    c.scale(mScaleFactor, mScaleFactor);
    c.drawBitmap(bitmapAux, 0, 0, null);}

I get the position with next method

@Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events.
        mScaleDetector.onTouchEvent(ev);


        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            startTime = ev.getEventTime();
            final float x = ev.getX();
            final float y = ev.getY();
            final float xR= ev.getRawX();
            final float yR= ev.getRawY();
            movido=false;
            mLastTouchX = x;
            mLastTouchY = y;
            mLastTouchXRaw = xR;
            mLastTouchYRaw = yR;

            mActivePointerId = ev.getPointerId(0);

            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                movido=true;
                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;
            movido=true;


            break;
        }

        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            endTime = ev.getEventTime();

            if ((endTime - startTime > 500)&&!movido) {

                // we have a 1000ms duration touch
                // propagate your own event
                Log.d("LC", "time touched greater than 1000ms " + endTime + "  "
                        + startTime);

                startTime = 0;
                endTime = 0;
                movido=false;
                Box pepeBox=new Box();
                pepeBox=buscarElemento();
                //Intent intent=new Intent(mContext,OpcionesConceptoList.class);
                //mContext.startActivity(intent);
                /*
                 * return true; notify that you handled this event (do not
                 * propagate)
                 */
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_POINTER_UP: {
            final int 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;
        }

        }



        return true;
    }

Any advice?

Thanks in advance and excuse my english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海少女心 2025-01-15 19:24:13

getRawX() 和 getRawY() :返回相对于手机屏幕的坐标(X,Y)

getX() 和 getY() :返回相对于 View 的坐标(X,Y)。

实际上绝对坐标是什么?

getRawX() and getRawY() : return the coordinate(X,Y) relative to the screen of the phone

getX() and getY() : return the coordiantes(X,Y) relative to the View.

Actually absolute co-ordinates of what?

怪我入戏太深 2025-01-15 19:24:13

如果我理解你的问题

//
Bitmap bitmap;
//
//
double bitmapRatio  = ((double)bitmap.getWidth())/bitmap.getHeight()

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
double screenHeight = metrics.heightPixels;
double screenWidth = metrics.widthPixels;

double screenRatio= screenWidth /screenHeight;

if(bitmapRatio  > screenRatio){

}else{

}

If I understood your question,

//
Bitmap bitmap;
//
//
double bitmapRatio  = ((double)bitmap.getWidth())/bitmap.getHeight()

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
double screenHeight = metrics.heightPixels;
double screenWidth = metrics.widthPixels;

double screenRatio= screenWidth /screenHeight;

if(bitmapRatio  > screenRatio){

}else{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文