方位角を基に画像を表示させたい

SurfaceViewとか

画像とかを連続して描画するのには、SurfaceViewというものを使うといいらしい。他にもTextureViewとかCustomViewとかあるらしいんだけどよく分からん。なんかAndroidのバージョンごとに推奨された変遷があるらしい。みんな使ってそうで、ネット上でも解説記事がある程度見つかるSurfaceViewを使ってみたい。

無理でした

途中まで頑張ったんだけど、やり方がよくわからなくなってしまった。SurfaceView.javaを新しく作ってそこに処理を書いて、MainActivity.javaでsetContentViewする感じにしたんだけど。センサーの値の取得をMainに書いてて、その値をSurfaceの方に渡すやり方がわからない。MainActivityの中でSurfaceViewを扱うやり方も分からない。ので別のやり方を探す。

参考

http://quesera2.hatenablog.jp/entry/2016/08/30/233743

https://blog.keiji.io/2015/12/mincomi-adventcalendar-20.html

https://techbooster.org/android/application/1195/

https://techbooster.org/android/application/1274/

ImageViewを使う

適当に作った画像を用意。

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:srcCompat="@drawable/needle" />

ImageViewを設置。

imageView = findViewById(R.id.imageView);
bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.needle);
imagewidth = bitmap1.getWidth();
imageheight = bitmap1.getHeight();

onCreateでimageViewの取得とdrawableから画像を取得。

azimuth = (float) Math.toDegrees(attitude[0]);
matrix.setRotate(-azimuth, imagewidth / 2, imageheight / 2);
Bitmap bitmap2 = Bitmap.createBitmap(bitmap1, 0, 0, imagewidth, imageheight, matrix, true);

imageView.setImageBitmap(bitmap2);

onSensorChangedで方位角からMatrixを使ってsetRotateで新たにBitmap作る。でImageViewにセット。これ計算合ってるかな。

回った!!なんかカクカクwあと見て分かる通り、回転するごとに画像が拡大縮小してしまう。すっごい方角がずれる。いっぱい動かしたりすると、アプリが固まる。うーん、なんか思ったような感じになってくれないなぁ。めんどくさくなってきたなぁ。

参考

https://akira-watson.com/android/matrix.html

カテゴリー: したい タグ: , , , パーマリンク

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です