簡體   English   中英

在Android中放大后如何獲取視圖的xy位置

[英]How to get the x y position of a view after zoom-in in android

我是android的初學者,正在放大頁面中尋找視圖的xy位置,但是我嘗試了很多我無法獲得的位置。我在下面的代碼中使用了此代碼,但是在縮放前后我得到了相同的位置。 請任何人張貼代碼,以便在縮放后找到xy位置。

 private int fieldImgXY[] = new int[2];
 v.getLocationOnScreen(fieldImgXY);
 Log.v("Log",fieldImgXY[0]+" "+fieldImgXY[1]);

我的完整代碼:

     zoom = (ZoomView) findViewById(R.id.maincontainerScroller);    

     imageView = new ImageView(this);
     imageView.setImageResource(R.drawable.ic_launcher);
     imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     zoom.addView(imageView);    

     imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            v.getLocationOnScreen(fieldImgXY);
            Log.v("Log",fieldImgXY[0]+" "+fieldImgXY[1]);

        }
    });

為了從圖像中獲取絕對坐標,我使用這種方法:

void calculeAbsoluteCoorde(MotionEvent e){
    float []m = new float[9];
    matrix.getValues(m);
    float transX = m[Matrix.MTRANS_X] * -1;
    float transY = m[Matrix.MTRANS_Y] * -1;
    float scaleX = m[Matrix.MSCALE_X];
    float scaleY = m[Matrix.MSCALE_Y];
    lastTouchX = (int) ((e.getX() + transX) / scaleX);
    lastTouchY = (int) ((e.getY() + transY) / scaleY);
    lastTouchX = Math.abs(lastTouchX);
    lastTouchY = Math.abs(lastTouchY);
}

我認為您使用矩陣進行縮放,希望對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM