2011-07-19 98 views
2

我想在使用動態addView方法使它們在我的應用程序中旋轉後,在ImageView上設置OnClickListener。但是當我在屏幕上按下ImageView時,onClick方法無法正常工作。如果我按下ImageView的原始位置的屏幕,它將起作用。我想在旋轉動畫之後在最終位置高級製作其他ImageView,但我不知道如何使ImageView偏斜佈局。我在等你的幫助.....如何在旋轉動畫後在ImageView上設置OnClickListener

謝謝。

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Matrix; 
import android.graphics.RectF; 
import android.graphics.drawable.BitmapDrawable; 

import android.os.Bundle; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.LinearInterpolator; 
import android.view.animation.RotateAnimation; 
import android.widget.FrameLayout; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.ImageView.ScaleType; 
import android.widget.Toast; 

public class MovingBillActivity extends Activity { 
    private String dollars[] = {"dollar1", "dollar5", "dollar10", "dollar20", "dollar50", "dollar100"}; 

    private ImageView wallet; 

    FrameLayout mFrameLayout; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     wallet = (ImageView) findViewById(R.id.wallet); 

     final OnClickListener handler = new OnClickListener() { 
      public void onClick(View v) { 
       switch (v.getId()) { 
        case 100: Log.i("kcy", "100"); break; 
        case 50: Log.i("kcy", "50"); break; 
       } 
      } 
     }; 


     wallet.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       wallet.setClickable(false); 
       // Create a FrameLayout in which to add the ImageView 
       mFrameLayout = (FrameLayout) findViewById(R.id.ll01); 

       RotateAnimation anim; 

       for(int j=0; j<dollars.length; j++) { 
        // Instantiate an ImageView and define its properties 
        final ImageView i = new ImageView(MovingBillActivity.this); 
        switch (j) { 
         case 5 : i.setImageResource(R.drawable.dollar100); break; 
         case 4 : i.setImageResource(R.drawable.dollar50); break; 
         case 3 : i.setImageResource(R.drawable.dollar20); break; 
         case 2 : i.setImageResource(R.drawable.dollar10); break; 
         case 1 : i.setImageResource(R.drawable.dollar5); break; 
         case 0 : i.setImageResource(R.drawable.dollar1); break; 
        } 
        i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions 
        i.setLayoutParams(new FrameLayout.LayoutParams(170, 67, Gravity.CENTER)); 
        i.setOnClickListener(handler); 

        anim = new RotateAnimation(0f, 30f - j * 20f, 170, 34); 
        anim.setInterpolator(new LinearInterpolator()); 
        anim.setRepeatCount(Animation.ABSOLUTE); 
        anim.setDuration(500); 
        anim.setFillEnabled(true); 
        anim.setFillAfter(true); 

        // Add the ImageView to the layout and set the layout as the content view 
        mFrameLayout.addView(i); 
        i.startAnimation(anim); 
       } 
      } 
     }); 
    } 
} 
+0

」正常工作「是什麼意思?你可以在任何地方爲ImageView設置ID,嘗試添加'default:Log.i(「kcy」,「unknown」); break;'切換大小寫。 – Audrius

回答

-1

Android將再次運行onDestroy()和onCreate()來執行方向更改。爲了避免這種情況,請嘗試在您的活動中實現onConfigurationChanged(),並且不要忘記在您的AndroidManifest.xml文件中允許「android:config =」orientation「

相關問題