• 締切済み

Androidで、ImageViewを移動させたい

Androidアプリを開発しているのですが、ImageViewを移動させるのがうまく出来ません。 アニメーションではなくて、所定の位置に単に移動させたいです。 最近の機種では、imgView.setTranslationX(x);で、できたのですがAndroid2.2などではサポートされておらず…。 レイアウトXMLのandroid:layout_marginRightなどが簡単に設定できると理想です。 なにか方法はあるのでしょうか?

  • Java
  • 回答数1
  • ありがとう数0

みんなの回答

noname#177743
noname#177743
回答No.1

いくつからsetTranslationXがサポートになったかはっきり覚えてませんが……、それ以前なら、layoutで再レイアウトするのが基本じゃないでしょうか。 <Viewインスタンス>.layout( 横, 縦, 幅, 高さ );

関連するQ&A

  • androidアプリ開発 -間違い探しアプリ-

    androidの間違い探しアプリを作ろうと思っています。 イメージ的には背景用のImageViewに画像を表示して、 間違っている箇所にカラのImageViewを載せ、 ImageViewがクリックされたら正解というような仕組みにしようと思っています。 そこで、下記コードを書いています。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" > <ImageView android:id="@+id/mainImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/stage1" ></ImageView> <ImageView android:id="@+id/onImage" android:layout_width="40dip" android:layout_height="40dip" android:layout_marginLeft="240dp" android:layout_marginTop="180dp" android:onClick="seikai" /> </RelativeLayout> が、解像度や画面サイズによってカラのImageViewの場所が変わってしまい、うまくいきません。 resフォルダ内に 「layout-land-hdpi」 「layout-land-ldpi」 「layout-land-mdpi」 「layout-land-xhdpi」 フォルダを作り、解像度ごとに位置の微調整は行なっていますが、 画面サイズが異なるとやはりずれてしまいます。 解像度、画面サイズにかかわらず位置を固定する方法はないのでしょうか。 ない場合はやはり画面サイズと解像度すべての条件のxmlを作成する必要がありますか。 また、そもそも 「イメージ的には背景用のImageViewに画像を表示して、間違っている箇所にカラのImageViewを載せる」やり方が間違っているのでしょうか。

  • Android開発についての初心者質問です。

    Android開発についての質問です。初心者でAndroidアプリを作っています。 初心者でAndroidアプリを作っています。 XMLからの実装をしたいのですが、 やり方がよく分かりません。 どなたかご教授頂けないでしょうか? よろしくお願いいたします。 やりたいことは、imageButtonをクリックしたら imageViewの画像部分が変わる。 imageButton1をクリック → imageView1が表示 imageButton2をクリック → imageView2が表示 デフォルトでは、imageView1が表示している状態 buttonのリスナーを登録するのが関の山で、ボタンクリック時の imageView表示切替処理等ちんぷんかんぷんです。 XMLはこういった感じで書いてます。 よろしくお願いいたします。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/frameLayout1" android:layout_height="wrap_content" android:foregroundGravity="center" android:layout_width="match_parent" > <ImageView android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_gravity="center" android:layout_width="match_parent" > </ImageView> </FrameLayout> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent" > <ImageButton android:id="@+id/imageButton1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content" > </ImageButton> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" > </ImageButton> <ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" > </ImageButton> </LinearLayout> </LinearLayout>

  • androidアプリのアニメーション作成方法

    初めまして、現在趣味でAndroidアプリの開発を行っています。 簡単なアニメーションを作成しようと思っているのですが、どうにもうまくいかないので やり方を教えていただければと思います。 【実現したいこと】 1.画面内をタップするたびに表示されている画像が少しずつ下に移動する 2.規定回数画面内をタップすると画像が画面外に落下していく 上記のうち1.がうまく動作しません。 現在以下のようなXMLファイルを作成して1.を実現しようとしています。 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="30" android:fillBefore="true" android:fillAfter="true" android:duration="500" /> 上記のようなXMLファイルでアニメーションを実装すると画面タップのたびに 移動させたい画像の初期の表示位置から30ピクセル落下してまた元の位置に戻る。 という動きを繰り返す処理になってしまいます。 調べたところAndroidのアニメーション処理は実際の画像の座標をずらしているわけではなく 擬似的に移動させているだけだということがわかりました。 私がやりたいことを実装するにはAndroidのアニメーション処理では不可能なのでしょうか? 不可能ならばどのような方法で実現可能なのでしょうか? わかる方がいましたらご教授願います.

  • Android javaでの画像の表示

    Android javaでの画像の表示 javaとxmlでの使い分け xmlでレイアウトをしつつ画面移行のプログラムを組んでいます。 やりたいことは、 前アクティビティからputExtraで値を受け取り、 その値によって画像の切り替えを行いたいのですが、 切り替えを行うためには変数を使う必要があるので、 xmlでの画像表示ではできません。 ですが、変数で画像を切り替えるようにすると、 R.layout.mainが使えません。 ようは、setcontentview(R.layout.main)でレイアウトをしつつ(こっちではImageView等、画像表示はしない)、 java内で画像を表示したい。 と、いうことです。 どなたか、やり方、もしくは参考になりそうな サイトを教えてください。 お願いします

  • Androidの画面レイアウトがくずれます

    同じサイズの画像を並べても、シミュレータで表示するとばらばらのサイズになってしまいます。 もう1ヶ月以上解決できなくて困っています。 写真はXMLベースで4つの画像を表示した結果です。 シミュレータでやってもほぼ同じ結果です。 元々の画像では、茶色と緑が同じ幅で、ピンクと青が同じ幅です。4つの画像とも高さは一緒です。 サイズの変更等はしてません。 GraphicalLayoutoは5.1inWVGA、シミュレータはWVGA854、Android2.3.3です ソースです。 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/left"/> <LinearLayout android:id="@+id/center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/left" android:orientation="vertical"> <ImageView android:id="@+id/top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/top"/> <ImageView android:id="@+id/bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bottom"/> </LinearLayout> <ImageView android:id="@+id/right" android:layout_height="wrap_content" android:src="@drawable/right" android:layout_toRightOf="@+id/center"/> </RelativeLayout>

  • Androidのレイアウトについて

    こんばんは ちょっとアンドロイドのレイアウトでちょっと戸惑って質問来ました。 添付画像のように画像の横のテキストを2行(本来は右より)で配置したいのですが、うまく設定出来ません。 TableLayoutをどういじってもダメ 泣 AbsoluteLayoutを見つけましたが、上部のテキストが長い時に使えない事がわかりどうしようもありません。 どのようにしたら、画像の横にテキスト行をニ行置けるでしょうか? <TableLayout android:layout_height="wrap_content" android:id="@+id/tableLayout1" android:layout_width="fill_parent"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:id="@+id/imageView1" android:src="@drawable/icon" android:layout_height="wrap_content"></ImageView> <LinearLayout android:id="@+id/linearLayout5" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="TextView" android:layout_width="wrap_content" android:id="@+id/textView5" android:layout_height="wrap_content"></TextView> </LinearLayout> <TextView android:text="TextView" android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </TableRow> </TableLayout> 取り敢えず今の問題の所が上です。 【知りたい事】 画像の横にニ行以上テキストを配置したい。 【やった事】 いろいろやり過ぎて書ききれません 泣 【開発環境】 Eclipse 何卒お知恵を貸してください。お願します。

    • ベストアンサー
    • XML
  • Androidの常駐アプリについて

    個人が開発できるAndroidの常駐アプリについて聞きたいのですが、 例えば、GPS情報を取得し所定の位置についたら画面上に「到着しました」と表示する常駐アプリを作り、それを常駐させた場合、 ゲームアプリを起動している時でも、アプリに割り込み「到着しました」と表示させることは可能なのでしょうか?

  • Androidプログラムについて

    多数の画像を、パラパラ漫画のように切り替えていく方法について相談があり、投稿させていただきました。 以前、xmlファイルで画像を読み込む方法が簡単だということで、hirotn様にご回答いただき、 動作させることができました。 ありがとうございました。 携帯の実機にインストールし、エラーも出ず動作することも確認できたのですが、 画像を切り替えるタイミングがあまりにも遅くなっており困っています。 画像が多い場合は、メモリ不足等起こっているのでしょうか。 プログラムで解決できる方法があれば教えていただきたく、よろしくお願いします。 (ちなみに、xmlファイルで読み出すファイルは100枚程度(80MB程度)です。 もっと数を増やしたり、画質をよくしたいとも思っております。) ※サンプルは下記で見つけました。 http://monoist.atmarkit.co.jp/mn/articles/1205/21/news003.html (以下、プログラム) package jp.test.animation.frame; import android.app.Activity; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class FrameAnimationTestActivity extends Activity { // ボタン Button mBtnAnimation; Button m5BtnAnimFromXML; // ビュー ImageView mImageAnimation; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBtnAnimation = (Button) findViewById( R.id.button_animation ); m5BtnAnimFromXML = (Button) findViewById( R.id.button_anim_from_xml ); mImageAnimation = (ImageView) findViewById( R.id.image_animation ); mBtnAnimation.setOnClickListener(mClickListener); m5BtnAnimFromXML.setOnClickListener(mClickListener); } View.OnClickListener mClickListener = new View.OnClickListener() { public void onClick(View v) { // アニメーション中なら、停止 Drawable d = mImageAnimation.getBackground(); if( d != null ){ try{ if( ((AnimationDrawable) d).isRunning() ){ ((AnimationDrawable) d).stop(); return; } } catch( RuntimeException e ){ e.printStackTrace(); } } //if( v == mBtnAnimation ){ //frameAnimationTest( //FrameAnimationTestActivity.this, mImageAnimation ); //} if( v == m5BtnAnimFromXML ){ frameAnimationFromXMLTest( mImageAnimation ); } } }; // フレームアニメーションを XML から読み込む void frameAnimationFromXMLTest( View v ){ // リソースからアニメーションを読み込み、ビューに設定 v.setBackgroundResource( R.drawable.droid_jump ); // ビューからアニメーションを取り出し AnimationDrawable anim = (AnimationDrawable)v.getBackground(); // アニメーション開始 anim.start(); } }

  • Androidプログラミング アプリが停止する

    eclipseでボタンを押すと画像が表示されるアプリをつくろうとしていましたがボタンをを押すとポップアップで「~を停止します。」と表示されアプリが落ちてしまいます。 Ver4.0を使用しています。 package my.application.imageviewer; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; public class ImageViewer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void showImage(View view) { ImageView imageView1 = (ImageView)findViewById(R.id.imageView1); imageView1.setImageResource(R.drawable.sample); } } プログラムにミスがあるのでしょうか? 宜しくお願い致します。

  • Androidアプリの解像度別レイアウト

    Androidアプリの解像度別レイアウトの質問です。 今現在、Android向けアプリの作成をしています。勉強レベルですが、Android向けのアプリをいくつか作った経験があります。そのときは自分の持っている端末にレイアウトを合わせて設計していました。 リリース向けの汎用性を持たせたアプリを作る場合、レイアウトはどのように行うのが一般的なのでしょうか。自分は、 (1)それぞれの解像度(hdpi,xhdpiなど)に合わせたレイアウトファイル.xmlを作り、setContentView()でレイアウトする。 (2)解像度別に配置するべき位置を計算してビューを配置する。(この場合pixel単位での配置になるでしょうか。) 上記2つの方法を考えました。 動的にビューを動かしたい場合などはdp単位で動かせるのかも疑問点です。 汎用性が高く、実装しやすいレイアウトのポイントなどありましたらご教授お願いします。

専門家に質問してみよう