0

我刚刚开始为 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;
        }
    });
}

一切对我来说似乎都是正确的,但是当触摸屏幕时,什么也没有发生。我犯的明显错误在哪里?提前致谢。

4

2 回答 2

1

你不能在LinearLayout. 让我们使用 aFrameLayout代替。

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;
        }
    };

}
于 2013-07-06T13:16:58.667 回答
0

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

于 2013-07-06T11:40:47.020 回答