質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

2107閲覧

ImageViewのgetX(),getY()ができない。

aruko

総合スコア47

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2021/12/13 00:09

編集2021/12/13 07:05

すみません。
分かりにくいところや、間違えて書いていることろがあったので、質問をもう一度書き換えさせてもらいます。

androidstudioでゲーム作りをしています。
kaki[0]~kaki[23]のImageViewと、saru1,saru2のImageViewがmain_activityに設定してあります。
アニメーションが繰り返されるようにコードを書いたつもりですが、アニメーションされず、ImageViewがとまったままになります。

そこで、どこに原因があるのか探すためTextViewに、

ma_tv1.setText("kaki["+kaki_num+"]="+kaki[kaki_num]);
ma_tv2.setText("kaki"+(kaki_num+1)+".x="+ x+" : kaki"+(kaki_num+1)+".y="+y);
ma_tv3.setText("saru1="+saru1);
ma_tv4.setText("saru1.x"+saru1.getX()+" : saru1.y="+saru1.getY());
ma_tv5.setText("list_kaki24"+list_kaki24);
ma_tv6.setText("");
ma_tv7.setText("kazu="+kazu+" : list_kaki_select="+list_kaki_select.size());
ma_tv8.setText("list_kaki_select="+list_kaki_select);

を表示してみました。

ここから見ると

ma_tv2.setText("kaki"+(kaki_num+1)+".x="+ x+" : kaki"+(kaki_num+1)+".y="+y);

に問題があり、
kaki20.x=0.0 : kaki20.y=0.0
となっています。
他の部分には問題はありません。

x = kaki[kaki_num].getX();
y = kaki[kaki_num].getY();
が取得されていないということです。
実際は0.0ではなく違う場所に設置してあるので、そのx座標とy座標を正しく取得したいです。

ImageViewでは、getX(),getY()ではだめなのでしょうか。
どうしたら取得できるか教えていただきたいです。
よろしくお願いします。

java

1 2import androidx.appcompat.app.AppCompatActivity; 3import androidx.constraintlayout.widget.ConstraintLayout; 4import androidx.lifecycle.ViewModelProvider; 5 6import android.animation.Animator; 7import android.animation.AnimatorInflater; 8import android.animation.AnimatorSet; 9import android.animation.ObjectAnimator; 10import android.content.Intent; 11import android.media.Image; 12import android.os.Bundle; 13import android.os.Handler; 14import android.os.Looper; 15import android.util.DisplayMetrics; 16import android.view.View; 17import android.view.animation.Animation; 18import android.view.animation.AnimationUtils; 19import android.widget.ImageView; 20import android.widget.TextView; 21 22import java.util.ArrayList; 23import java.util.List; 24import java.util.Random; 25 26public class MainActivity extends AppCompatActivity implements Runnable { 27 TextView ma_tv1; 28 TextView ma_tv2; 29 TextView ma_tv3; 30 TextView ma_tv4; 31 TextView ma_tv5; 32 TextView ma_tv6; 33 TextView ma_tv7; 34 TextView ma_tv8; 35 ImageView saru1,saru2; 36 static int w,h;//画面サイズ取得 37 ImageView kaki[]=new ImageView[24];//柿全部24こ 38 int kakiImageId[]=new int[]{R.id.kaki1, R.id.kaki2,R.id.kaki3, R.id.kaki4,R.id.kaki5, R.id.kaki6,R.id.kaki7, R.id.kaki8,R.id.kaki9, R.id.kaki10,R.id.kaki11, R.id.kaki12,R.id.kaki13, R.id.kaki14,R.id.kaki15, R.id.kaki16,R.id.kaki17, R.id.kaki18,R.id.kaki19, R.id.kaki20,R.id.kaki21, R.id.kaki22,R.id.kaki23, R.id.kaki24};;//柿24このImageId 39 boolean flag_animator; 40 List<Integer> list_kaki24 = new ArrayList<>(); 41 List<Integer> list_kaki_select = new ArrayList<>(); 42 int kazu;//サルがとる柿の数 43 int px;//サル画像の左側のマージン 44 int kaki_num;//とる柿ナンバー(配列インデックス) 45 float x;//とる柿のX座標 46 float y;//とる柿のy座標 47 48 @Override 49 protected void onCreate(Bundle savedInstanceState) { 50 super.onCreate(savedInstanceState); 51 setContentView(R.layout.activity_main); 52 53 saru1 = findViewById(R.id.saru1); 54 saru2 = findViewById(R.id.saru2); 55 56 ma_tv1 = findViewById(R.id.ma_tv1); 57 ma_tv2 = findViewById(R.id.ma_tv2); 58 ma_tv3 = findViewById(R.id.ma_tv3); 59 ma_tv4 = findViewById(R.id.ma_tv4); 60 ma_tv5 = findViewById(R.id.ma_tv5); 61 ma_tv6 = findViewById(R.id.ma_tv6); 62 ma_tv7 = findViewById(R.id.ma_tv7); 63 ma_tv8 = findViewById(R.id.ma_tv8); 64 65 for(int i=0;i<24;i++){ 66 kaki[i]=(ImageView)this.findViewById(kakiImageId[i]); 67 list_kaki24.add(i); 68 } 69 kimeru_kaki();//どの柿をいくつとるか決めておく 70 flag_animator = true; 71 px = convertDPtoPX(100);//サル画像の左側のマージンが100 72 kaki_num=list_kaki_select.get(0);//最初の要素を取り出す 73 list_kaki_select.remove(0); 74 x = kaki[kaki_num].getX(); 75 y = kaki[kaki_num].getY(); 76 77 if(flag_animator==true&&list_kaki_select.size()!=0){ 78 kaki_saru_keisan_animation(); 79 } 80 81 ma_tv1.setText("kaki["+kaki_num+"]="+kaki[kaki_num]); 82 ma_tv2.setText("kaki"+(kaki_num+1)+".x="+ x+" : kaki"+(kaki_num+1)+".y="+y); 83 ma_tv3.setText("saru1="+saru1); 84 ma_tv4.setText("saru1.x"+saru1.getX()+" : saru1.y="+saru1.getY()); 85 ma_tv5.setText("list_kaki24"+list_kaki24); 86 ma_tv6.setText(""); 87 ma_tv7.setText("kazu="+kazu+" : list_kaki_select="+list_kaki_select.size()); 88 ma_tv8.setText("list_kaki_select="+list_kaki_select); 89 } 90 91 public int convertDPtoPX(int dp) { 92 DisplayMetrics metrics = this.getResources().getDisplayMetrics(); 93 int px = (int)(dp*metrics.density); 94 return px; 95 } 96 97 public void onWindowFocusChanged(boolean hasFocus) { 98 super.onWindowFocusChanged(hasFocus); 99 View view=findViewById(R.id.ma_app_screen); 100 h= view.getHeight(); 101 w= view.getWidth(); 102 } 103 104 private void kimeru_kaki(){ 105 Random rand = new Random(); 106 kazu = rand.nextInt(4) + 5;//5~9の数が持ち去る柿の数 107 Random rand2 = new Random(); 108 for (int i = 0; i < kazu; i++) {//全柿の数24の中からどの柿を持ち去るか決める 109 int num = rand2.nextInt(list_kaki24.size()); 110 list_kaki_select.add(list_kaki24.get(num)); 111 list_kaki24.remove(num); 112 } 113 } 114 private void kaki_getXY(){ 115 kaki_num=list_kaki_select.get(0);//最初の要素を取り出す 116 x = kaki[kaki_num].getX(); 117 y = kaki[kaki_num].getY(); 118 } 119 private void kaki_saru_keisan_animation() { 120 Random rand=new Random(); 121 int r=rand.nextInt(3000)+2000; 122 float from_x=0; 123 float to_x=0; 124 float from_xx=0; 125 float to_xx=0; 126 int jikan=0; 127 saru1.setY(y - 48);//48は位置調整 128 saru2.setY(y - 48); 129 if(kaki_num<12){ //上の段の柿 130 jikan = (int) (r *(w-x) / (w + px-20));//移動時間の計算//pxはサル画像のマージン分 131 from_x=w+px; 132 to_x=x+px-15; 133 from_xx=x + px - 15; 134 to_xx=w+px; 135 }else{ //下の段の柿 136 jikan = (int)( r * x / (w + px-20));//移動時間の計算 137 from_x=-px; 138 to_x=x + px - 15; 139 from_xx=x + px - 15; 140 to_xx=-px; 141 } 142 ObjectAnimator animator_yoko_idou = ObjectAnimator.ofFloat(saru1, "translationX", from_x,to_x).setDuration(jikan);//15は位置調整 143 ObjectAnimator animator_idou_modoru1 = ObjectAnimator.ofFloat(saru1, "translationX", from_xx, to_xx).setDuration(jikan); 144 ObjectAnimator animator_idou_modoru2 = ObjectAnimator.ofFloat(saru2, "translationX", from_xx, to_xx).setDuration(jikan); 145 ObjectAnimator animator_toumei = ObjectAnimator.ofFloat(kaki[kaki_num], "alpha", 0f, 0f).setDuration(jikan); 146 Random rand2 = new Random(); 147 int start_ofset = rand2.nextInt(4000);//スタートオフセットの時間 148 animator_toumei.setStartDelay(start_ofset + jikan); 149 AnimatorSet animatorset_idou_modoru12 = new AnimatorSet(); 150 animatorset_idou_modoru12.playTogether(animator_idou_modoru1, animator_idou_modoru2); 151 AnimatorSet animatorset_ouhuku = new AnimatorSet(); 152 animatorset_ouhuku.playSequentially(animator_yoko_idou, animatorset_idou_modoru12); 153 animatorset_ouhuku.setStartDelay(start_ofset); 154 animator_toumei.start(); 155 animatorset_ouhuku.start(); 156 flag_animator=false; 157 ma_tv6.setText("kaki"+(kaki_num+1)+".x="+x+" : kaki"+(kaki_num+1)+".y="+y); 158 animatorset_ouhuku.addListener(new Animator.AnimatorListener() { 159 @Override 160 public void onAnimationStart(Animator animation) { } 161 @Override 162 public void onAnimationEnd(Animator animation) { 163 flag_animator=true; 164 list_kaki_select.remove(0); 165 kaki_getXY(); 166 } 167 @Override 168 public void onAnimationCancel(Animator animation) { } 169 @Override 170 public void onAnimationRepeat(Animator animation) { } 171 }); 172 } 173} 174

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2021/12/13 04:09

メソッドがパラメータ・戻り値を使用していないため、データがどこでどう設定・参照・変更されているのかが非常に分かり難く読み難くなっています。これはコードとして悪い傾向と見られます。 どの個所で x,y が 0.0 になっているのが確認出来て、どの個所でgetX,getY をされたいのでしょうか。
jimbe

2021/12/13 08:23 編集

編集ありがとうございます。 ma_tv4 に saru1 の x,y は表示されているのですね。 x = kaki[kaki_num].getX(); y = kaki[kaki_num].getY(); と ma_tv2.setText("kaki"+(kaki_num+1)+".x="+ x+" : kaki"+(kaki_num+1)+".y="+y); の間に if 文と kaki_saru_keisan_animation の実行がありますが、 kaki_saru_keisan_animation は実行されているのでしょうか。 可能でしたらレイアウトもご提示頂けますでしょうか。
aruko

2021/12/13 11:18

はい、実行できているので、 ma_tv4.setText("saru1.x="+saru1.getX()+" : saru1.y="+saru1.getY()); が、 saru1.x=0.0 : saru1.y=48.0 と表示されます。 これは、kaki_saru_keisan_animationの中の、 saru1.setY(y - 48);//48は位置調整   があるから48.0となります。 ですので、最初の投稿でsaru1はxとyが取得できていると書きましたが間違いで、saru1も取得できていないことが分かりました。 結局、ImageViewのx座標y座標がどれも取得できないというシンプルな内容であることに気づきました。 ところが、ここに至るまでの間に色々試しながらアニメーションさせようとしました。 threadを使う中でrunの中でこのアニメーションをスタートさせたところ、アニメーションの連続ができましたが、1つ目のアニメーションだけが今と同じ状態でxyが取得できない状態で始まりましたが、続いて2つ目から最後までは正しくアニメーションできていました。(2つ目からはxyが取得できていた) サルがランダムに柿をとって戻っていくというアニメーションです。 その1つ目を何とかしようと原因を探っていると、1つ目だけがxyが取得できない状態になっていることが分かったので、runをなくして今の状態にしています。 レイアウトを載せます。 よろしくお願いします。
guest

回答1

0

ベストアンサー

View の位置 ( やサイズ等 ) は、最終的に Activity の View ツリーが完全に構築されてからでないと取り出せないものと思います。
これは、画面サイズが onCreate 中では取れなかった (0) のと同じ理由です。
ですので、画面サイズと同じく onWindowFocusChanged() の実行されたタイミング(以降)で取り出す必要があるものと思います。

投稿2021/12/13 11:48

編集2021/12/13 11:49
jimbe

総合スコア12545

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

aruko

2021/12/13 12:01

先週、horizontaiBiasの取得の仕方を教えてもらったように行い、できましたが、そこから計算してxyを出したら、実際の位置とわずかにずれが出たので、xyを取得する方法に変えている状態です。 レイアウトですが、 多分文字数の関係だと思いますが、かなり省略しても載せられないので、こちらに載せます。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ma_app_screen" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="visible"> <ImageView android:id="@+id/saru2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="-100dp" android:layout_marginLeft="-100dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.489" app:srcCompat="@drawable/saru_kaki" /> <ImageView android:id="@+id/saru1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="-100dp" android:layout_marginLeft="-100dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.489" app:srcCompat="@drawable/saru" /> <ImageView android:id="@+id/ma_gamen" android:layout_width="0dp" android:layout_height="0dp" android:scaleType="fitXY" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" app:srcCompat="@drawable/ma_gamen" /> <ImageView android:id="@+id/kaki24" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.958" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.923" app:srcCompat="@drawable/kaki" /> ・・・kaki2~kaki23とTextViewは文字数の関係で省略しました。・・・ <ImageView android:id="@+id/kaki1" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.291" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.341" app:srcCompat="@drawable/kaki" /> <ImageView android:id="@+id/jiisan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="fitXY" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.499" app:srcCompat="@drawable/jiisan_l_mae" /> </androidx.constraintlayout.widget.ConstraintLayout>
aruko

2021/12/13 14:39

いつも解決できるご指導ありがとうございます。 ばっちり連続アニメーションできました。
jimbe

2021/12/13 15:02

>かなり省略しても載せられない すいませんでした、考えてみれば ImageView だけで 24 個あるのですから長大になるのは間違いなかったですね。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問