表題について質問があります。
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}
悪化しました。
画像は変な方向に縮小されるわ、アニメーションは終了後に元に戻ってしまって途中で止まらないわで困っています。
どうすればいいのか誰か教えてください
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/04 06:20
2017/08/04 06:56
2017/08/04 07:44