Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 拖航及;在屏幕上移动位图_Java_Android_Bitmap_Drag And Drop - Fatal编程技术网

Java 拖航及;在屏幕上移动位图

Java 拖航及;在屏幕上移动位图,java,android,bitmap,drag-and-drop,Java,Android,Bitmap,Drag And Drop,我尝试过实现一种在屏幕上拖放位图的方法。当卡片是imageView并且在XML文件中时,我尝试的方法是有效的,但是我改变了我的方法,我只是在调用draw方法时将位图加载到屏幕上的矩形中 有人能告诉我实现拖放加载到屏幕上的位图的最佳方法吗?提前谢谢 我的代码: Play Screen类:(其中位图加载并绘制到屏幕上) 编辑: 有效的方法是使用以下方法: RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParam

我尝试过实现一种在屏幕上拖放位图的方法。当卡片是imageView并且在XML文件中时,我尝试的方法是有效的,但是我改变了我的方法,我只是在调用draw方法时将位图加载到屏幕上的矩形中

有人能告诉我实现拖放加载到屏幕上的位图的最佳方法吗?提前谢谢

我的代码: Play Screen类:(其中位图加载并绘制到屏幕上)

编辑: 有效的方法是使用以下方法:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(500, 150);
        cardImage.setLayoutParams(layoutParams);
        cardImage.setOnTouchListener(new ChoiceTouchListener());



 //load hazard playing card
    Bitmap hazardCard = loadBitmap(assetManager, "img/hazard_card.jpg");
    ImageView hazardCardimageView = (ImageView) view.findViewById(R.id.hazardCardImageView);
    if (hazardCard != null) {
        hazardCardimageView.setImageBitmap(hazardCard);
    }
     private final class ChoiceTouchListener implements View.OnTouchListener {
            public boolean onTouch(View view, MotionEvent event) {
                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        RelativeLayout.LayoutParams lparams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                        _xDelta = X - lparams.leftMargin;
                        _yDelta = Y - lparams.topMargin;
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                        layoutParams.leftMargin = X - _xDelta;
                        layoutParams.topMargin = Y - _yDelta;
                        layoutParams.rightMargin = 0b11111111111111111111111100000110;
                        layoutParams.bottomMargin = 0b11111111111111111111111100000110;
                        view.setLayoutParams(layoutParams);
                        break;
                }
                rootLayout.invalidate();
                return true;
            }
        }
主要活动类别: 包com.example.llave_000.shootout

import android.app.Activity;
import android.app.FragmentManager;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.example.llave_000.shootout.world.DemoGame;
import com.example.llave_000.shootout.BaseGestureDetector;
import com.example.llave_000.shootout.MoveGestureDetector;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnTouchListener {

    private float mScaleFactor = 1.0f;
    private float mRotationDegrees = 0.f;
    private float mFocusX = 0.f;
    private float mFocusY = 0.f;

    private ImageView cardImage1;
    private ViewGroup rootLayout;
    public View view;

    private MoveGestureDetector mMoveDetector;


    private ScaleGestureDetector mScaleDetector;
/*    private RotationGestureDetector mRotateDetector;
    private MoveGestureDetector mMoveDetector;*/

    /**
     * Game fragment instance
     */
    private Game mGame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //ImageView hazardCardImageView = (ImageView) view.findViewById(R.id.hazardCardImageView);

        cardImage1 = (ImageView)findViewById(R.id.hazardCardImageView);

        // Setup Gesture Detectors
        mMoveDetector = new MoveGestureDetector(getApplicationContext(), new MoveListener());
        //mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());
        /*mRotateDetector = new RotationGestureDetector(getApplicationContext(), new RotateListener());
        mMoveDetector = new MoveGestureDetector(getApplicationContext(), new MoveListener());*/


        // Setup the window as suitable for a game, namely: full screen
        // with no title and a request to keep the screen on. The changes
        // are made before any content is inflated.
        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        // Set the content view to use a simple frame layout
        setContentView(R.layout.activity_main);

        // Add in the main game fragment..
        FragmentManager fm = getFragmentManager();
       // MenuScreen menuScreen = (MenuScreen)fm.findFragmentById(R.id.activity_main_id);
        mGame = (Game)fm.findFragmentById(R.id.activity_main_id);

        if (mGame == null) {
            mGame = new DemoGame(); //not sure if correct
            fm.beginTransaction().add(R.id.activity_main_id, mGame)
                    .commit();
        }

        // Add in the main game fragment
     /*   if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.activity_main_id, new MenuScreen()).commit();
        }*/

    }

    public boolean onTouch(View view, MotionEvent event) {
        mScaleDetector.onTouchEvent(event);
        mMoveDetector.onTouchEvent(event);

       /* mRotateDetector.onTouchEvent(event);
        mMoveDetector.onTouchEvent(event);
*/
        // Mmmmmhhhagic!!!
        //  with: mScaleFactor, mRotationDegrees, mFocusX and mFocusY

        return true; // indicate event was handled
    }
    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor(); // scale change since previous event
            return true;
        }
    }

 /*   private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
        @Override
        public boolean onRotate(RotateGestureDetector detector) {
            mRotationDegrees -= detector.getRotationDegreesDelta();
            return true;
        }
    }
*/
    private class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
        @Override
        public boolean onMove(MoveGestureDetector detector) {
            PointF d = detector.getFocusDelta();
            mFocusX += d.x;
            mFocusY += d.y;

            return true;
        }
    }



    @Override
    public void onBackPressed() {
        // If the fragment does not consume the back event then
        // trigger the default behaviour
        if(!mGame.onBackPressed())
            super.onBackPressed();
    }

   /* public boolean onTouchEvent(MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            //code in here to take me to new screen with match attax background
            //with new screen displaying a card. then work on being able to move that card.

            //playButtonY -= 20;

        }*/
       // playButton.setY(playButtonY);
        //code in here to take me to new screen with match attax background
        //with new screen displaying a card. then work on being able to move that card.

        //return true;
    }

..

看到了吗?我尝试了一些答案,主要是一个使用图层类和视口类的答案,但出现了很多错误。很多错误?@psink,我实现了视口类。导入必要的包后的前两个错误:“无法解析符号图层0”和“无法解析符号MoveGestureDetector”。。与RotateGestureDetector相同。关于我的具体比赛,我很难遵循这个答案。我刚开始使用安卓系统,所以不熟悉这些东西。我了解绘制位图和与之相关的矩形,以便将它们显示在屏幕上,但之后就不那么了解了。我的答案是:
,借助android手势检测器https://github.com/pskink/android-gesture-detectors […]“
那么您是否获得了
android手势检测器
?当然,你不必在你的代码中使用它们,但是阅读我的代码你就会知道它是如何工作的
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(500, 150);
        cardImage.setLayoutParams(layoutParams);
        cardImage.setOnTouchListener(new ChoiceTouchListener());



 //load hazard playing card
    Bitmap hazardCard = loadBitmap(assetManager, "img/hazard_card.jpg");
    ImageView hazardCardimageView = (ImageView) view.findViewById(R.id.hazardCardImageView);
    if (hazardCard != null) {
        hazardCardimageView.setImageBitmap(hazardCard);
    }
     private final class ChoiceTouchListener implements View.OnTouchListener {
            public boolean onTouch(View view, MotionEvent event) {
                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        RelativeLayout.LayoutParams lparams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                        _xDelta = X - lparams.leftMargin;
                        _yDelta = Y - lparams.topMargin;
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                        layoutParams.leftMargin = X - _xDelta;
                        layoutParams.topMargin = Y - _yDelta;
                        layoutParams.rightMargin = 0b11111111111111111111111100000110;
                        layoutParams.bottomMargin = 0b11111111111111111111111100000110;
                        view.setLayoutParams(layoutParams);
                        break;
                }
                rootLayout.invalidate();
                return true;
            }
        }
import android.app.Activity;
import android.app.FragmentManager;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.example.llave_000.shootout.world.DemoGame;
import com.example.llave_000.shootout.BaseGestureDetector;
import com.example.llave_000.shootout.MoveGestureDetector;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnTouchListener {

    private float mScaleFactor = 1.0f;
    private float mRotationDegrees = 0.f;
    private float mFocusX = 0.f;
    private float mFocusY = 0.f;

    private ImageView cardImage1;
    private ViewGroup rootLayout;
    public View view;

    private MoveGestureDetector mMoveDetector;


    private ScaleGestureDetector mScaleDetector;
/*    private RotationGestureDetector mRotateDetector;
    private MoveGestureDetector mMoveDetector;*/

    /**
     * Game fragment instance
     */
    private Game mGame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //ImageView hazardCardImageView = (ImageView) view.findViewById(R.id.hazardCardImageView);

        cardImage1 = (ImageView)findViewById(R.id.hazardCardImageView);

        // Setup Gesture Detectors
        mMoveDetector = new MoveGestureDetector(getApplicationContext(), new MoveListener());
        //mScaleDetector = new ScaleGestureDetector(getApplicationContext(), new ScaleListener());
        /*mRotateDetector = new RotationGestureDetector(getApplicationContext(), new RotateListener());
        mMoveDetector = new MoveGestureDetector(getApplicationContext(), new MoveListener());*/


        // Setup the window as suitable for a game, namely: full screen
        // with no title and a request to keep the screen on. The changes
        // are made before any content is inflated.
        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        // Set the content view to use a simple frame layout
        setContentView(R.layout.activity_main);

        // Add in the main game fragment..
        FragmentManager fm = getFragmentManager();
       // MenuScreen menuScreen = (MenuScreen)fm.findFragmentById(R.id.activity_main_id);
        mGame = (Game)fm.findFragmentById(R.id.activity_main_id);

        if (mGame == null) {
            mGame = new DemoGame(); //not sure if correct
            fm.beginTransaction().add(R.id.activity_main_id, mGame)
                    .commit();
        }

        // Add in the main game fragment
     /*   if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.activity_main_id, new MenuScreen()).commit();
        }*/

    }

    public boolean onTouch(View view, MotionEvent event) {
        mScaleDetector.onTouchEvent(event);
        mMoveDetector.onTouchEvent(event);

       /* mRotateDetector.onTouchEvent(event);
        mMoveDetector.onTouchEvent(event);
*/
        // Mmmmmhhhagic!!!
        //  with: mScaleFactor, mRotationDegrees, mFocusX and mFocusY

        return true; // indicate event was handled
    }
    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor(); // scale change since previous event
            return true;
        }
    }

 /*   private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
        @Override
        public boolean onRotate(RotateGestureDetector detector) {
            mRotationDegrees -= detector.getRotationDegreesDelta();
            return true;
        }
    }
*/
    private class MoveListener extends MoveGestureDetector.SimpleOnMoveGestureListener {
        @Override
        public boolean onMove(MoveGestureDetector detector) {
            PointF d = detector.getFocusDelta();
            mFocusX += d.x;
            mFocusY += d.y;

            return true;
        }
    }



    @Override
    public void onBackPressed() {
        // If the fragment does not consume the back event then
        // trigger the default behaviour
        if(!mGame.onBackPressed())
            super.onBackPressed();
    }

   /* public boolean onTouchEvent(MotionEvent me) {
        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            //code in here to take me to new screen with match attax background
            //with new screen displaying a card. then work on being able to move that card.

            //playButtonY -= 20;

        }*/
       // playButton.setY(playButtonY);
        //code in here to take me to new screen with match attax background
        //with new screen displaying a card. then work on being able to move that card.

        //return true;
    }