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

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

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

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

1回答

4494閲覧

layoutを飛び出してもImageViewをリサイズされないようにしたいです。

kyon2

総合スコア15

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2018/05/23 08:25

前提・実現したいこと

androidアプリを作ろと思っているのすが

下記の様なXMLレイアウトを作りました。
addImagesLayoutの中にimageViewを、デフォルトで設置して
ドラッグで移動できるようにしています。
また、追加ボタンで、addImagesLayout 内に画像を更に追加できるようになっています。

解決したいのは、addImagesLayoutから飛び出して設置した画像がリサイズされないようにしたいです。
(はみ出したままで設置したい)
上(TOP)と左(LEFT)は、はみ出したまま設置できるのですが、
下(BOTTOM)と右(RIGHT)に、はみ出して設置した場合、addImagesLayout内に収まるように
自動的にリサイズされてしまいます。

リサイズさせずに、上(TOP)と左(LEFT)の様に、はみ出したままにしておきたいです。

xmlのパラメタ設定で解決できるのでしょうか?

Javaのコードで制御しなければならないのでしょうか?

Javaのソースも載せておきます。

よろしくお願いします。

ソースコード

XML

1 2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 > 8 9 <RelativeLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:clipToPadding="true" 13 android:clipChildren="true" 14 > 15 16 <android.support.constraint.ConstraintLayout 17 android:id="@+id/linearLayoutMain" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent" 20 android:layout_above="@+id/control_layout" 21 android:gravity="center_vertical|center_horizontal|center" 22 android:minHeight="108dp" 23 android:visibility="visible" 24 android:clipToPadding="true" 25 android:clipChildren="true" 26 > 27 28 <RelativeLayout 29 android:id="@+id/mainRelative" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_marginBottom="8dp" 33 android:layout_marginEnd="8dp" 34 android:layout_marginStart="8dp" 35 android:layout_marginTop="8dp" 36 android:clipChildren="false" 37 android:clipToPadding="false" 38 app:layout_constraintBottom_toBottomOf="parent" 39 app:layout_constraintEnd_toEndOf="parent" 40 app:layout_constraintStart_toStartOf="parent" 41 app:layout_constraintTop_toTopOf="parent"> 42 43 <RelativeLayout 44 android:id="@+id/addImagesLayout" 45 android:layout_width="300dp" 46 android:layout_height="400dp" 47 android:clipChildren="false" 48 android:clipToPadding="false" 49 android:visibility="visible"> 50 51 <ImageView 52 android:id="@+id/defaultImageView" 53 android:layout_width="100dp" 54 android:layout_height="100dp" 55 android:src="@android:drawable/sym_def_app_icon" 56 android:tag="11111" /> 57 58 59 </RelativeLayout> 60 61 </RelativeLayout> 62 63 64 </android.support.constraint.ConstraintLayout> 65 66 <LinearLayout 67 android:id="@+id/control_layout" 68 android:layout_width="match_parent" 69 android:layout_height="wrap_content" 70 android:layout_alignParentBottom="true" 71 android:layout_alignParentLeft="true" 72 android:gravity="center" 73 android:visibility="visible"> 74 75 <Button 76 android:id="@+id/addImage" 77 android:layout_width="wrap_content" 78 android:layout_height="match_parent" 79 android:layout_marginBottom="5dp" 80 android:text="設置・追加" /> 81 </LinearLayout> 82 83 </RelativeLayout> 84 85</FrameLayout> 86 87

Java

1import android.app.Activity; 2import android.os.Bundle; 3import android.view.MotionEvent; 4import android.view.View; 5import android.view.ViewGroup; 6import android.widget.ImageView; 7import android.widget.RelativeLayout; 8 9public class MainActivity extends Activity { 10 11 private RelativeLayout relativeLayout; 12 private Activity mActivity; 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 19 mActivity = this; 20 relativeLayout = findViewById(R.id.addImagesLayout); 21 22 // ドラッグによる、ImageViewの移動処理 23 final View.OnTouchListener moving = new View.OnTouchListener() { 24 25 private float downX; 26 private float downY; 27 28 private int downLeftMargin; 29 private int downTopMargin; 30 31 @Override 32 public boolean onTouch(View v, MotionEvent event) { 33 34 final ViewGroup.MarginLayoutParams param = 35 (ViewGroup.MarginLayoutParams)v.getLayoutParams(); 36 37 if( event.getAction() == MotionEvent.ACTION_DOWN ){ 38 39 downX = event.getRawX(); 40 downY = event.getRawY(); 41 42 downLeftMargin = param.leftMargin; 43 downTopMargin = param.topMargin; 44 45 return true; 46 } 47 else if( event.getAction() == MotionEvent.ACTION_MOVE){ 48 49 param.leftMargin = downLeftMargin + (int)(event.getRawX() - downX); 50 param.topMargin = downTopMargin + (int)(event.getRawY() - downY); 51 52 v.layout( 53 param.leftMargin, param.topMargin,param.leftMargin + v.getWidth(),param.topMargin + v.getHeight() 54 ); 55 56 57 return true; 58 } 59 60 return false; 61 } 62 }; 63 64 findViewById(R.id.defaultImageView).setOnTouchListener(moving); 65 66 // 設置・追加ボタン 67 // 68 findViewById(R.id.addImage).setOnClickListener(new View.OnClickListener() { 69 @Override 70 public void onClick(View v) { 71 72 73 ImageView iv = new ImageView(mActivity); 74 iv.setImageResource(android.R.drawable.sym_def_app_icon); 75 relativeLayout.addView(iv); 76 77 iv.setOnTouchListener(moving); 78 79 80 } 81 }); 82 83 84 } 85 86 87}

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

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

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

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

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

guest

回答1

0

ベストアンサー

marginの変更を行うと再レイアウト処理が行われてしまいますから、代わりにtranslationを使ってはいかがでしょうか?Viewをレイアウトされた位置から相対的に動かすことができます。

final View.OnTouchListener moving = new View.OnTouchListener() { private float downX; private float downY; @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { downX = event.getRawX(); downY = event.getRawY(); return true; } else if (event.getAction() == MotionEvent.ACTION_MOVE) { v.setTranslationX(event.getRawX() - downX); v.setTranslationY(event.getRawY() - downY); return true; } return false; } };

投稿2018/05/23 11:52

kakajika

総合スコア3131

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

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

kyon2

2018/05/24 03:33

ありがとうございます! marginは、再レイアウト処理が行われてしまうのですね。 非常に助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問