请看下面的代码。myView在framelayout的无法设置,大小也不行。这就是那个常用的爆炸模拟的animation,
我将
MyView myView = new MyView(Blast.this,x,y);
frame.addView(myView);
这两行代码放倒点击监听中去了。就无法讲myView的位置放到FrameLayout的点击处。请问这是哪里出了问题。谢谢。
放倒onCreate那里就没有问题。 public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final FrameLayout frame = new FrameLayout(this);
setContentView(frame);
frame.setBackgroundResource(R.drawable.back);
bomb = MediaPlayer.create(this , R.raw.bomb);
//myView.setBackgroundResource(R.anim.blast);
frame.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View source, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN )
{
float x = event.getX();
float y = event.getY();
MyView myView = new MyView(Blast.this,x,y);
frame.addView(myView);
myView.setLocation((int)y, (int)x);
ViewGroup.LayoutParams params = myView.getLayoutParams(); 
anim = (AnimationDrawable) myView.getBackground();
anim.stop();
anim.start();
bomb.start();
}
return false;
}
}); class MyView extends ImageView
{
public MyView(Context context,float x,float y)
{
super(context);
setBackgroundResource(R.anim.blast);
}
// 定义一个方法,该方法用于控制MyView的显示位置
public void setLocation(int top, int left)
{
this.setFrame(left, top, left + 40, top + 40);
}
        }

解决方案 »

  1.   

     FrameLayout --> RelativeLayout -->MyView   试试
      

  2.   

    跟这个没关系。而且FrameLayout下面有这个RelativeLayout吗
      

  3.   

    在onTouch中每次都创建一个新的MyView??
      

  4.   

    那你什么时候删除呢?难道每次都添加一个view??这个样要添加多少啊
      

  5.   

    还有setFrame的参数中明确说了
    /**
         * Assign a size and position to this view.
         *
         * This is called from layout.
         *
         * @param left Left position, relative to parent
         * @param top Top position, relative to parent
         * @param right Right position, relative to parent
         * @param bottom Bottom position, relative to parent
         * @return true if the new size and position are different than the
         *         previous ones
         * {@hide}
         */是相对于parent的位置,所以你不能每次都用x,y,要求差值
      

  6.   

    每点击一次就添加一个view显示爆炸。
    你说xy要差值,我点击的地方不一样,自然就是差值了。
    但是为什么不行呢?
      

  7.   

    要设置成在parent中的相对位置,
    /**
      * Assign a size and position to this view.
      *
      * This is called from layout.
      *
      * @param left Left position, relative to parent
      * @param top Top position, relative to parent
      * @param right Right position, relative to parent
      * @param bottom Bottom position, relative to parent
      * @return true if the new size and position are different than the
      * previous ones
      * {@hide}
      */
      

  8.   

    sorry,我还是不清楚如何改。
    能否告诉下我如何更改代码?谢谢
      

  9.   

    LZ,请问你解决了吗,我现在也是这样
    view把整个framelayout填满了
      

  10.   

    试着用handle把事件转到另一个时刻,并尝试从新setContentView没具体试过,只能试着给点思路。希望能有所帮助。