画像が画面の左側、正確に中央 (幅または高さ)、または画面の右側に配置されている場合、その画像leftX
rightX
などを計算できます...
あなたがすることは、それらの位置を計算することです。たとえば、画像が画面の中央にある場合:
leftx = (ScreenWidth - ImageWidth) / 2
- を追加すると、 ( )
imagewidth
ができますrightX
rightX = leftX + imageWidth
- 高さについても同じことを行います。これにも@dennisdrewの回答を使用できます
などの APIgetWidth
を最新バージョンに更新してみてください。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