LoginSignup
0
1

More than 5 years have passed since last update.

SurfaceViewを使用するために2

Posted at

はじめに

SurfaceViewを使用する方法は
EclipseのGraphical Layoutで配置する
SurafaceViewを継承したクラスを実装する
の2通りの方法がありましたが、
タッチイベントの通知を受け取る方法に違いがありました。

画面のタッチイベント処理

SurfaceViewをEclipseのGraphical Layoutで配置する場合、
タッチイベントを受けるためにはOnTouchListenerを
implementsする必要があります。

画面のタッチイベントは
@Override
public boolean onTouch(View v, MotionEvent event)
で受け取ります。

SurafaceViewを継承したクラスを実装する場合は、
ViewクラスのonTouchEventをオーバーライドすることで、
タッチイベントを受け取ることができます。
OnTouchListenerをimplementsする必要はありません。
@Override
public boolean onTouchEvent(MotionEvent event)

なぜクラスを作成しているのか

EclipseのGraphical Layoutで配置する場合、
MainSurfaceViewクラスは作らなくても問題ないと思います。
ではなぜ温度計アプリケーションではMainSurfaceViewクラスを作成したかといいますと
SurfaceView内でタッチイベントを処理したり描画用のThreadを使用するため、
分かりやすくなるようにクラスを作成しました。

MainActivity.java
import android.view.SurfaceView;

public class MainActivity extends Activity implements Runable, OnTouchListener {

    private SurfaceView surfaceView;

    @Override
    public void onCreateView() {

        super.onCreateView();

        this.surfaceView.setOnTouchListener(this);
    }
}

まとめ

ちょっとした違いでしたが同じことをするにも手順が異なり悩まされました。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1