ImageView onTouch无法正确移动

时间:2016-12-19 12:48:05

标签: java android

我从一个地方移动图像时遇到问题,我用手指在屏幕上触摸它。这意味着当我触摸它时,让我们说在一个按钮的中心并想要移动它,它让我离开按钮的顶角,从那时起,我可以移动它,向上,向左,向右和所以...我想有可能从用户将手指放在按钮上的任何地方移动按钮

代码:

public class Joystick extends RelativeLayout implements View.OnTouchListener {

    private Context context;
    private ImageView backgroundImageView;
    private ImageView buttonImageView;
    float xx = 0;
    float yy = 0;
    private static final String TRANSLATIONX = "setTranslationX";



    public Joystick(Context context) {
        super(context);
        this.context = context;
        initSlider();
        if(buttonImageView != null) {
            buttonImageView.setOnTouchListener(this);
        }
        this.setClipChildren(false);

    }

    public ImageView setView ( String imageName) {

        ImageView image = new ImageView(context);

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                image.setLayoutParams(layoutParams);

                // load image
                try {
                    // get input stream
                    InputStream ims = getContext().getAssets().open(imageName);
                    // load image as Drawable
                    Drawable d = Drawable.createFromStream(ims, null);
                    // set image to ImageView
                    image.setImageDrawable(d);
                } catch (IOException ex) {



                }


        return image;

    }

    public void initSlider() {

        backgroundImageView = setView("background.png");
        buttonImageView = setView("jostick.png");

        addView(backgroundImageView);
        addView(buttonImageView);

    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {


        float x = motionEvent.getRawX(); 
        float y = motionEvent.getRawY(); 

        float cx = buttonImageView.getWidth() / 2.f;
        float cy = buttonImageView.getHeight() / 2.f;

        float w = backgroundImageView.getWidth();
        float h = backgroundImageView.getHeight();

        double r = cx / 2.; 
        double dx = x - cx;
        double dy = y - cy;
        double hypot = Math.hypot(dx, dy); 
        double cos = dx / hypot; // cos
        double sin = dy / hypot; // sin

        double rcos = r * cos;
        double rsin = r * sin;
        double rdx = Math.abs(dx) < Math.abs(rcos) ? dx : rcos; // if,else
        double rdy = Math.abs(dy) < Math.abs(rsin)  ? dy : rsin;

        Log.d("VALUES", "RAW X:" + motionEvent.getRawX() + ", X:" + motionEvent.getX() + ", CX:" + cx + ", CY:" + cy + ", dx:" + dx + ", dy:" + dy + ", Hypo:" + hypot + ", cos:" + cos + ", sin" + sin);


        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                break;

            case MotionEvent.ACTION_UP:


                view.setX(xx); 
                view.setY(yy); 
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                break;

            case MotionEvent.ACTION_POINTER_UP:
                break;

            case MotionEvent.ACTION_MOVE:

                float transX = (float) (cx + rdx - w / 2.);
                float transY = (float) (cy + rdy - h / 2.);

                buttonImageView.setTranslationX(transX);
                buttonImageView.setTranslationY(transY);

                Log.d(TRANSLATIONX,"X:" + transX + ", Y:" + transY);

                break;

        }
        return true;

    }
}

某处是一个小虫子。我希望得到任何帮助。感谢

1 个答案:

答案 0 :(得分:0)

如果有人有兴趣或正在寻找一些答案如何让Joystick Button移动到框内,我找到了一个解决方案:

private Point calculate (float x, float y) {

    float cx = buttonImageView.getWidth() / 2.f;
    float cy = buttonImageView.getHeight() / 2.f;

    double r = cx / 2.; // vrednost radius
    double dx = x;
    double dy = y;
    double hypot = Math.hypot(dx, dy); // izracun hipotenuze
    double cos = dx / hypot; // cos
    double sin = dy / hypot; // sin

    double rcos = r * cos;
    double rsin = r * sin;

    double rdx = Math.abs(dx) < Math.abs(rcos) ? dx : rcos; // if,else
    double rdy = Math.abs(dy) < Math.abs(rsin)  ? dy : rsin;

    return new Point((int)rdx, (int)rdy);

}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {


    final float x = motionEvent.getRawX(); // x točko
    final float y = motionEvent.getRawY(); // y točka

    //Log.d("VALUES", "RAW X:" + motionEvent.getRawX() + ", RAW Y:" + motionEvent.getRawY() +  ", X:" + motionEvent.getX() + ", CX:" + cx + ", CY:" + cy + ", dx:" + dx + ", dy:" + dy + ", Hypo:" + hypot + ", cos:" + cos + ", sin" + sin);


    switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:

            xDelta = view.getX() - x;
            yDelta = view.getY() - y;

            break;

        case MotionEvent.ACTION_UP:

            doBounceAnimation(buttonImageView);
            doVibration();

            view.setX(xx);
            view.setY(yy);
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            break;

        case MotionEvent.ACTION_POINTER_UP:
            break;

        case MotionEvent.ACTION_MOVE:

            final float transX = (float) x;
            final float transY = (float) y;

            thread = new Thread() {
                @Override
                public void run() {

                    Point newPoint = calculate(transX+ xDelta,transY + yDelta);
                    buttonImageView.setX(newPoint.x);
                    buttonImageView.setY(newPoint.y);
                }
            };
            thread.start();




            Log.d(TRANSLATIONX,"X:" + transX + ", Y:" + transY);

            break;

    }
    return true;

}

享受编码!