2016-11-08 4 views
0

私はスケールを描画し、タッチイベント(そのスケールの水平スクロール)を扱うカスタムビューを持っています。一部の描画:複数のカスタムビューの同期スクロール

@Override 
public void onDraw(Canvas canvas){ 
    startingPoint = mainPoint; 
    counter = 0; 
    int i = 0; 

    while (startingPoint <= scaleWidth) { 
     if(i % 4 == 0) { 
      size = scaleHeight/4; 

      if (counter < 24) { 
       counter = counter + 1; 
      } else { 
       counter = 1; 
      } 

      String c = Integer.toString(counter); 
      canvas.drawText(c, startingPoint, endPoint - (size + 20), textPaint); 

     } else { 
      if(i % 2 == 0) { 
       size = scaleHeight/8; 
      } else { 
       size = scaleHeight/16; 
      } 
     } 

     if(startingPoint >= closest) { 
      //метки шкалы 
      canvas.drawLine(startingPoint, endPoint - size, startingPoint, endPoint, rulerPaint); 

     } 

     startingPoint = startingPoint + pxmm; 

     i = i + 1; 


    } 
} 

そしてたTouchEvent:activity_main.xmlで

public boolean onTouchEvent(MotionEvent event) { 
    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      float x = event.getX(); 
      prevx = x; 
      System.out.println(x); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      float z = event.getX(); 
      float dist = z - prevx; 
      mainPoint = prevsp + dist; 
      closest = mainPoint - ((int)(mainPoint/pxmm)) * pxmm; 
      break; 
     case MotionEvent.ACTION_UP: 
      float y = event.getX(); 
      prevsp = mainPoint; 
      break; 
    } 

    invalidate(); 

    return true; 
} 

を私は、このビューの2つのコピー&ペースト:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:gravity="center|top"> 

<net.manualuser.calibr.TimeScale 
    android:id="@+id/my_scale" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" /> 

<net.manualuser.calibr.TimeScale 
    android:id="@+id/my_scale1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" /> 

を私はので、これら二つのビューを同期させるために何をすべきどちらか一方のスケールをスクロールするともう一方のスケールが同時に動きます。

TimeScale defScale = (TimeScale)findViewById(R.id.my_scale); 
TimeScale defScale1 = (TimeScale)findViewById(R.id.my_scale1); 
defScale.setOnTouchListener(this); 
defScale1.setOnTouchListener(this); 

そして(MainActivity中)onTouch方法でそれらを同期させるためのいくつかのコードを記述します。私は正しいんだ場合、私はMainActivityの私のカスタムビューのためOnTouchListenerを設定する必要があります。しかし、今私はそれをどうやって考えているのですか?

+0

あなたは '他のビューの' onTouchEvent(MotionEventイベント)にイベントを渡すだけで試すことができます。またonTouchListenerのonTouchでfalseを返す必要があります –

+0

@VladMatvienko thxたくさん。私はMotionEventにもっと注意を払う必要がありました。私は "別のビューにイベントを渡す"を検索したときにいくつかの答えがあります。 –

答えて

0

は、それは私がMainActivityでonTouchの私の意見の両方にイベントを渡す必要がある作業を取得するには、次のonTouchListenerで

public boolean onTouch(View v, MotionEvent event){ 
    defScale.onTouchEvent(event); 
    defScale1.onTouchEvent(event); 
    return false; 
} 
関連する問題