我有一个尺寸大于屏幕的位图(即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;
}
有什么建议吗?
提前致谢并原谅我的英语。
答案 0 :(得分:1)
getRawX()和getRawY():返回相对于手机屏幕的坐标(X,Y)
getX()和getY():返回相对于视图的坐标(X,Y)。
实际上是什么绝对的坐标?
答案 1 :(得分:0)
如果我理解你的问题,
//
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{
}