ImageView没有按我的意愿移动

时间:2014-02-19 05:15:29

标签: android imageview ontouchlistener

我正在实施onTouchListener演示。我希望动态生成imageView并在整个frameLayout中移动它。我完成imageView创建并实施onTouchListenerimageView不是按照我的意愿移动..收集是我的代码..

package your.packagename;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class MainActivity extends Activity {
    Button b1;
    Button b2;
    FrameLayout f;
    ImageView imageview;
    int windowwidth;
    int windowheight;
    private android.widget.FrameLayout.LayoutParams layoutParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        f = (FrameLayout) findViewById(R.id.framelayout);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        windowwidth = size.x;
        windowheight = size.y;
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageview = new ImageView(getBaseContext());
                imageview.setId(1);
                registerForContextMenu(imageview);
                imageview.setBackgroundResource(R.drawable.ic_launcher);
                FrameLayout.LayoutParams ivparam = new FrameLayout.LayoutParams(100, 100);
                imageview.setLayoutParams(ivparam);
                f.addView(imageview);
                imageview.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        layoutParams = (FrameLayout.LayoutParams) imageview.getLayoutParams();
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                break;
                            case MotionEvent.ACTION_MOVE:
                                int x_cord = (int) event.getRawX();
                                int y_cord = (int) event.getRawY();
                                if (x_cord > windowwidth) {
                                    x_cord = windowwidth;
                                }
                                if (y_cord > windowheight) {
                                    y_cord = windowheight;
                                }
                                layoutParams.leftMargin = x_cord - 25;
                                layoutParams.topMargin = y_cord - 25;
                                imageview.setLayoutParams(layoutParams);
                                break;
                            default:
                                break;
                        }
                        return true;
                    }
                });
            }
        });
    }
}

请告诉我,我错了,或者我应该以其他方式使用。

0 个答案:

没有答案