Android在屏幕边界内移动图像

时间:2016-11-11 21:04:34

标签: java android image animation matrix

我试图在基于3个变量(gyroXaxis,gyroYaxis,gyroZaxis)的活动中移动两个图像,每个毫秒更新一次(因此图像将不断移动)。图像必须保持在屏幕的边界内,因此也需要实现矩阵。

我试过跟着 ImageView Drag Limitation In Android 问题,但我的设置有点不同。

全局变量

ImageView imageView1;
ImageView imageView2;

// gyro float variables get updated every millisecond in a different function
float gyroXaxis;
float gyroYaxis;
float gyroZaxis;

private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();

PointF DownPT = new PointF(); 
PointF StartPT = new PointF(); 

OnCreate中

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_version);
    // Create images
    imageView1 = (ImageView) findViewById(R.id.image1);
    Drawable drawableImage1 = getDrawable(R.drawable.image1);
    imageView1.setImageDrawable(drawableChromoX);

    imageView2 = (ImageView) findViewById(R.id.image2);
    Drawable drawableImage2 = getDrawable(R.drawable.image2);
    imageView2.setImageDrawable(drawableChromoY);

    View.OnTouchListener listener = new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (v.getTag() != null && v.getTag().equals("image1")) {
                int eid = event.getAction();
                switch (eid) {
                    case MotionEvent.ACTION_MOVE:
                        PointF mv = new PointF(event.getX() - DownPT.x, event.getY() - DownPT.y);
                        imageView1.setX((int) (StartPT.x + mv.x));
                        imageView1.setY((int) (StartPT.y + mv.y));
                        StartPT = new PointF(imageView1.getX(), imageView1.getY());
                        break;
                    case MotionEvent.ACTION_DOWN:
                        DownPT.x = event.getX();
                        DownPT.y = event.getY();
                        StartPT = new PointF(imageView1.getX(), imageView1.getY());
                        break;
                    case MotionEvent.ACTION_UP:
                        break;

                    default:
                        break;
                }
            }
            else if (v.getTag() != null && v.getTag().equals("image2")) {
                int eid = event.getAction();
                switch (eid)
                {
                    case MotionEvent.ACTION_MOVE :
                        PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
                        imageView2.setX((int)(StartPT.x+mv.x));
                        imageView2.setY((int)(StartPT.y+mv.y));
                        StartPT = new PointF(imageView2.getX(), imageView2.getY() );
                        break;
                    case MotionEvent.ACTION_DOWN :
                        DownPT.x = event.getX();
                        DownPT.y = event.getY();
                        StartPT = new PointF(imageView2.getX(), imageView2.getY() );
                        break;
                    case MotionEvent.ACTION_UP :
                        break;
                    default :
                        break;
                }
            }
            return true;

        }
    };

    imageView1.setOnTouchListener(listener);
    imageView2.setOnTouchListener(listener);
}

1 个答案:

答案 0 :(得分:0)

根据您的示例代码,您可以执行类似的操作,以便将imageView保持在特定范围内:

final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
// Display Rect Boundaries
final Rect parentRect = new Rect(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);

// original down point
final PointF offsetPoint = new PointF();

imageView1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        final int action = motionEvent.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                offsetPoint.x = motionEvent.getX();
                offsetPoint.y = motionEvent.getY();
                break;

            case MotionEvent.ACTION_MOVE:
                float x = motionEvent.getX();
                float y = motionEvent.getY();

                imageView1.offsetLeftAndRight((int) (x - offsetPoint.x));
                imageView1.offsetTopAndBottom((int) (y - offsetPoint.y));

                // check boundaries
                if (imageView1.getRight() > parentRect.right) {
                    imageView1.offsetLeftAndRight(-(imageView1.getRight() - parentRect.right));
                } else if (imageView1.getLeft() < parentRect.left) {
                    imageView1.offsetLeftAndRight((parentRect.left - imageView1.getLeft()));
                }

                if (imageView1.getBottom() > parentRect.bottom) {
                    imageView1.offsetTopAndBottom(-(imageView1.getBottom() - parentRect.bottom));
                } else if (imageView1.getTop() < parentRect.top) {
                    imageView1.offsetTopAndBottom((parentRect.top - imageView1.getTop()));
                }

                break;
        }
        return true;
    }
});