こんにちは。
タイトルの件について質問があります。
今Androidでアニメーションの動作を勉強しています。
画像が貼ってあるImageViewをタッチした時にアニメーションで縮小→離した時にアニメーションで元の大きさまで戻したい動きを作りたいのですが上手くいきません。
縮小の部分は以下の処理で上手くいったのですが、指を離すと直ぐに元の大きさに戻ってしまいます。
ヒントでもいいのでよろしくお願いします。
java
1import android.os.Bundle; 2import android.support.v7.app.AppCompatActivity; 3import android.util.Log; 4import android.view.MotionEvent; 5import android.view.View; 6import android.view.View.OnTouchListener; 7import android.view.animation.ScaleAnimation; 8import android.widget.ImageView; 9 10public class MainActivity extends AppCompatActivity { 11 12 private static final String TAG = "ScaleAnimation"; 13 public ImageView imageView = null; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 imageView = (ImageView) findViewById(R.id.imageView); 21 imageView.setOnTouchListener(new OnTouchListener() { 22 @Override 23 public boolean onTouch(View v, MotionEvent event) { 24 25 if(event.getAction() == MotionEvent.ACTION_DOWN){ 26 Log.d(TAG, "down!"); 27 ScaleAnimation(imageView, 0.5f, 0.5f); 28 }else if(event.getAction() == MotionEvent.ACTION_UP){ 29 Log.d(TAG, "up!"); 30 ScaleAnimation(imageView, 1.0f, 1.0f); 31 } 32 return true; // true にしとかないと UP が呼ばれない 33 } 34 }); 35 } 36 37 private void ScaleAnimation(ImageView v, float scaleX, float scaleY){ 38 /* 39 Animation.ABSOLUTE:240 のようなpixel絶対値 40 Animation.RELATIVE_TO_SELF:自分のサイズの割合、0.5fが画像の中心 41 Animation.RELATIVE_TO_PARENT:parentサイズの割合、0.5fが画面中心 42 */ 43 ScaleAnimation anime = null; 44 anime = new ScaleAnimation( 45 1.0f, scaleX, // Xスケールサイズ(開始時→終了時) 46 1.0f, scaleY, // Yスケールサイズ(開始時→終了時) 47 ScaleAnimation.RELATIVE_TO_SELF, 0.5f, // X座標のタイプ、X座標(0.5fがview中央値) 48 ScaleAnimation.RELATIVE_TO_SELF, 0.5f); // Y座標のタイプ、Y座標(0.5fがview中央値) 49 50 // 0.3秒かけてアニメーションする(ms) 51 anime.setDuration(300); 52 53 // アニメーション終了時の表示状態を維持する 54 anime.setFillEnabled(true); 55 anime.setFillAfter(true); 56 57 // アニメーションを開始 58 v.startAnimation(anime); 59 } 60}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/12 04:42 編集