如果图像放置在屏幕的左侧,或者正好在中心(宽度或高度),或者在屏幕的右侧,您可以计算它leftX rightX 等等...
所以你要做的是计算这些位置。例如,如果图像位于屏幕中央:
leftx = (ScreenWidth - ImageWidth) / 2
- 添加
imagewidth, 现在你有rightX( rightX = leftX + imageWidth)
- 对高度做同样的事情;您也可以为此使用@dennisdrew 的答案
尝试将 API 更新为等的最新getWidth版本getHeight。
我的代码:
public void calculateImageDimensions() {
if(ScreenWidth < 480) {
HeightDartBoard = ScreenWidth;
WidthDartBoard = ScreenWidth;
RadiusBoard = ScreenWidth / 2;
if(WidthDartBoard == 320) {
RadMulti = 0.75; // multiplier for different scaled images
} else {
RadMulti = 0.5; // means half the image width and height
}
LeftXBoard = 0;
} else {
HeightDartBoard = 480; // I already know the width and height
WidthDartBoard = 480;
RadiusBoard = 240;
LeftXBoard = (ScreenWidth - WidthDartBoard) / 2; // this is what interests you
}
RightXBoard = LeftXBoard + WidthDartBoard; // now add the width of image to left X
TopYBoard = intActionBar; // actionbar height is start of my image topY
BottomYBoard = TopYBoard + HeightDartBoard; // and again add height of board
}
现在给你:
LeftX = (ScreenWidth - (WidthDartBoard * RadMulti)) / 2;
现在你有一个计算图像width和height它的位置的例子......
在该onTouchEvent方法中,您现在可以对其进行测试:
if(((eventX > leftX) && (eventX < rightX)) && ((eventY > topY) && (eventY < bottomY))) {
// code here
}
如果这是true您单击图像上的某个位置。
如果你想制作新的x和y相对于图像本身的:
newX = eventX - leftX
newY = eventY - topY
现在newX和newY是实际图像的X和。Y