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

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

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

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

Q&A

解決済

1回答

2952閲覧

ImageViewを押された時にアニメーションを行いたい

KokoCoupon

総合スコア15

Android Studio

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

0グッド

0クリップ

投稿2017/08/04 04:58

表題について質問があります。

ImageViewを押した時に画像が小さくなり、指を離すと元の大きさに戻るアニメーションを付けたくて悩んでいます。

手始めに以下の方法で実装しました。

Android Studio 2.3
Windows7 Professional x64

java

1[MainActivity.java] 2public class MainActivity extends AppCompatActivity { 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 } 9 10 public void animeStart(View v){ 11 Animation animation = AnimationUtils.loadAnimation(this, R.anim.touch); 12 v.startAnimation(animation); 13 } 14}

xml

1[res/layout/activity_main.xml] 2 3<?xml version="1.0" encoding="utf-8"?> 4<RelativeLayout 5 xmlns:android="http://schemas.android.com/apk/res/android" 6 xmlns:app="http://schemas.android.com/apk/res-auto" 7 xmlns:tools="http://schemas.android.com/tools" 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 tools:context=".MainActivity"> 11 12 <ImageView 13 android:id="@+id/imageView" 14 android:onClick="animeStart" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 app:srcCompat="@mipmap/ic_launcher"/> 18</RelativeLayout>

xml

1[res/anim/touch.xml] 2 3<?xml version="1.0" encoding="utf-8"?> 4<set xmlns:android="http://schemas.android.com/apk/res/android"> 5 <scale 6 android:fromXScale="0.5" 7 android:toXScale="1" 8 android:fromYScale="0.5" 9 android:toYScale="1" 10 android:pivotX="50%" 11 android:pivotY="50%" 12 android:duration="300" 13 android:fillAfter="true" /> 14</set>

これで実装しましたが、これだとボタンから指が離れた時にアニメーションが開始します。

なので少し修正しました。

xml

1public class MainActivity extends AppCompatActivity { 2 3 final String TAG = "TouchAnime"; 4 ImageView imageView; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 imageView = (ImageView) findViewById(R.id.imageView); 11 imageView.setOnTouchListener(new View.OnTouchListener() { 12 @Override 13 public boolean onTouch(View view, MotionEvent motionEvent) { 14 if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ 15 Log.d(TAG, "down"); 16 // 縮小するアニメーションを作成 17 ScaleAnimation animation_scale = new ScaleAnimation(1, 0.5f, 1, 0.5f, 0.5f, 0.5f); 18 // アニメーション実行時間を指定する(ms) 19 animation_scale.setDuration( 300 ); 20 // アニメーションの起動 21 imageView.startAnimation( animation_scale ); 22 23 // 途中で止めるには....? 24 // animation_scale.cancel(); // 利かない 25 26 27 }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){ 28 // ここで元の画像サイズに戻す処理を行う 29 Log.d(TAG, "up"); 30 31 ScaleAnimation animation_scale = new ScaleAnimation(0.5f,1, 0.5f, 1, 2, 2); 32 // アニメーション実行時間を指定する(ms) 33 animation_scale.setDuration( 300 ); 34 // アニメーションの起動 35 imageView.startAnimation( animation_scale ); 36 37 // 途中で止めるには....? 38 // animation_scale.cancel(); // 利かない 39 40 } 41 return false; 42 } 43 }); 44 } 45 46 public void animeStart(View v){ 47 Log.d(TAG, "animeStart"); 48 // ここはImageViewが押された時の処理 49 } 50}

悪化しました。

画像は変な方向に縮小されるわ、アニメーションは終了後に元に戻ってしまって途中で止まらないわで困っています。
どうすればいいのか誰か教えてください

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

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

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

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

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

guest

回答1

0

ベストアンサー

タッチイベントのログは想定通りに出力されていますか?

ScaleAnimation#setFillAfterを設定していませんね、おそらくこれが終了後に戻る理由ですね。

投稿2017/08/04 05:20

yona

総合スコア18155

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

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

KokoCoupon

2017/08/04 06:20

ありがとうございます。 ご指摘頂いた内容を元に、以下の様に修正したら期待する動作になりました。 もし変更出来る箇所があればご指摘下さい。 imageView = (ImageView) findViewById(R.id.imageView); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ Log.d(TAG, "down"); // 縮小するアニメーションを作成 ScaleAnimation animation_scale = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //修正 // アニメーション実行時間を指定する(ms) animation_scale.setDuration( 300 ); // animation_scale.setFillEnabled(true); animation_scale.setFillAfter(true); // 追加 // アニメーションの起動 imageView.startAnimation( animation_scale ); }else if(motionEvent.getAction() == MotionEvent.ACTION_UP){ Log.d(TAG, "up"); ScaleAnimation animation_scale = new ScaleAnimation(0.5f, 1, 0.5f, 1, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 修正 // アニメーション実行時間を指定する(ms) animation_scale.setDuration( 300 ); // アニメーションの起動 imageView.startAnimation( animation_scale ); } return false; } });
yona

2017/08/04 06:56

アニメーションの処理をメソッドに分割した方がいいですね。
KokoCoupon

2017/08/04 07:44

こんな感じですかね。 imageView = (ImageView) findViewById(R.id.imageView); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { Log.d(TAG, "down"); onTouchAnimation(imageView, 0.5f, 0.5f); } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { Log.d(TAG, "up"); onTouchAnimation(imageView, 1.0f, 1.0f); } return false; } }); } private void onTouchAnimation(View targetView, float scaleX, float scaleY ) { // 縮小するアニメーションを作成 ScaleAnimation animation_scale = new ScaleAnimation(1, scaleX, 1, scaleY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //修正 // アニメーション実行時間を指定する(ms) animation_scale.setDuration(300); animation_scale.setFillEnabled(true); animation_scale.setFillAfter(true); // 追加 // アニメーションの起動 targetView.startAnimation(animation_scale); }
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問