android,添加多个ImageViews onTouch事件

时间:2013-07-06 11:34:50

标签: android ontouchevent dynamically-generated

我刚开始为Android编程。我经常搜索我的问题,但建议并没有帮助我。我想在触摸坐标屏幕上显示相同的图像。这就是我所做的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View main_view = (View)findViewById(R.id.main_view);
    main_view.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView image = new ImageView(getApplicationContext());
            //ImageView image = (ImageView)findViewById(R.id.broken);
            image.setImageResource(R.drawable.broken);
            image.setX(event.getX() + image.getWidth() / 2);
            image.setY(event.getY() - image.getHeight() / 2);
            LinearLayout top_layout = (LinearLayout) findViewById(R.id.top_layout);
            LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            image.setLayoutParams(p);
            top_layout.addView(image);

            return true;
        }
    });
}

一切似乎都适合我,但是当触摸屏幕时,没有任何反应。我犯过的明显错误在哪里?提前谢谢。

2 个答案:

答案 0 :(得分:1)

你不能在LinearLayout中这样做。 我们改用FrameLayout

<强> activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/framelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<强> MainActivity.java

public class MainActivity extends Activity {

    private FrameLayout mLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLayout = (FrameLayout) findViewById(R.id.framelayout);
        mLayout.setOnTouchListener(mListener);

    }

    private OnTouchListener mListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                return true;

            case MotionEvent.ACTION_UP:
                // decode the resource to get width and height
                Options opts = new Options();
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, opts);

                int imageWidth = opts.outWidth;
                int imageHeight = opts.outHeight;

                // set the imageview's top and left margins
                FrameLayout.LayoutParams lp = new LayoutParams(imageWidth, imageHeight);
                lp.leftMargin = (int) (event.getX() - (imageWidth / 2));
                lp.topMargin = (int) (event.getY() - (imageHeight / 2));

                ImageView image = new ImageView(MainActivity.this);
                image.setImageResource(R.drawable.ic_launcher);
                mLayout.addView(image, lp);

                return false;

            }
            return false;
        }
    };

}

答案 1 :(得分:0)

setX和setY不设置图像位置。它基本上将图像滚动到位。该位置由其父视图控制。由于其父级是线性布局,因此它将始终位于布局中位于其上方的东西的下方或右侧。如果你想把它放在一个确切的位置,你需要将它放在支持它的父级中,例如不推荐使用的AbsoluteLayout。