0

タイトルに書いた通り、ImageViewでタッチイベントを検出しようとしています。ImageTouchHandler クラスの onTouch イベントを使用してそれを行うことができます。問題は、ImageView に含まれる画像のサイズが画面に合わせて変更された場合、実際の画像ではなく ImageView 座標でタッチ位置を取得することです。

たとえば、画像が 480*648 で imageView が 1000*450 の場合、画像内のタッチ位置を検出する方法がわかりません。

this :_imageView.getDrawable().getBounds().leftで実際の画像の左位置を取得しようとしましたが、常に 0 です。

私もこれを試しました:

float leftImage = (_imageView.getWidth()-_imageView.getDrawable().getIntrinsicWidth())/2;

しかし、それは良くありません。

何か考えはありますか?

前もってありがとう、バレンティン

4

1 に答える 1

0

画像が画面の左側、正確に中央 (幅または高さ)、または画面の右側に配置されている場合、その画像leftX rightX などを計算できます...

あなたがすることは、それらの位置を計算することです。たとえば、画像が画面の中央にある場合:

  • leftx = (ScreenWidth - ImageWidth) / 2
  • を追加すると、 ( )imagewidthができますrightXrightX = 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

newXnewYは実際のイメージですXY

于 2013-01-05T01:42:23.317 に答える