1
private void createnewpattern(final String res) {
    final int width, height;
    BitmapDrawable image = null;
    if (res.equals("")) {
        width = bitmap.getWidth();
        height = bitmap.getHeight();
    } else {
        image = (BitmapDrawable) this.getResources().getDrawable(
                getResources().getIdentifier(res, "drawable",
                        getPackageName()));
        width = image.getBitmap().getWidth();
        height = image.getBitmap().getHeight();
    }

    ImageView pattern = new ImageView(this);
    pattern.setId(startingnumber);
    if (res.equals(""))
        pattern.setImageBitmap(bitmap);
    else
        pattern.setImageDrawable(image);
    pattern.setScaleType(ScaleType.FIT_CENTER);

    pattern.setPadding(width / 4, height / 4, width / 4, height / 4);

    final RelativeLayout.LayoutParams patternparam = new RelativeLayout.LayoutParams(
            width * 3 / 2, height * 3 / 2);

    Random random = new Random();
    int rx = random.nextInt((contentimage.getWidth() * 3 / 4)
            - contentimage.getWidth() / 4 + 1) + 0;
    int ry = random.nextInt((contentimage.getHeight() * 3 / 4)
            - contentimage.getHeight() / 4 + 1) + 0;

    if (res.equals(""))
        patternparam.setMargins(GlobalFunction.leftmargin - width / 4,
                GlobalFunction.topmargin - height / 4, 0, 0);
    else
        patternparam.setMargins(rx, ry, 0, 0);

    pattern.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (isselected.get(view.getId() - 1001)) {

                mScaleDetector.onTouchEvent(event);

                mRotateDetector.onTouchEvent(event);

                mMoveDetector.onTouchEvent(event);

                ImageView imageview = (ImageView) rLayout
                        .findViewById(selectedid);

                ViewHelper.setRotation(imageview, mRotationDegrees);

                ViewHelper.setScaleX(imageview, mScaleFactor);
                ViewHelper.setScaleY(imageview, mScaleFactor);

                ViewHelper.setTranslationX(imageview, mFocusX);
                ViewHelper.setTranslationY(imageview, mFocusY);
            }

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_UP:
                if (!isselected.get(view.getId() - 1001)) {
                    for (int j = 0; j < isselected.size(); j++) {
                        isselected.set(j, false);
                        rLayout.findViewById(1001 + j)
                                .setBackgroundDrawable(null);
                    }

                    selectedid = view.getId();
                    isselected.set(view.getId() - 1001, true);
                    view.setBackgroundResource(R.drawable.border);

                    ImageView imageview = (ImageView) view;
                    imageview.setScaleType(ScaleType.FIT_CENTER);
                    imageview.setPadding(width / 4, height / 4, width / 4,
                            height / 4);

                    mScaleFactor = ViewHelper.getScaleX(imageview);

                    mRotationDegrees = ViewHelper.getRotation(imageview);

                    mFocusX = ViewHelper.getTranslationX(imageview);
                    mFocusY = ViewHelper.getTranslationY(imageview);
                }
                break;
            }

            return true;
        }
    });

    patternframe.addView(pattern, patternparam);

    isselected.add(false);
    startingnumber++;

    action4btn.setBackgroundDrawable(GlobalFunction.backgroundselector(
            this, R.drawable.paint_action_4_default,
            R.drawable.paint_action_4_selected));
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        mScaleFactor = Math.max(0.5f, Math.min(mScaleFactor, 4f));

        return true;
    }
}

private class OnRotationGestureListener implements
        RotationGestureDetector.OnRotationGestureListener {

    @Override
    public boolean OnRotation(RotationGestureDetector rotationDetector) {
        mRotationDegrees -= rotationDetector.getAngle();

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

我有这个监听器,但有一些错误。

图像的方向也会旋转。

当我旋转 90 度时,我向上平移 Y,但图像向左平移 X。

4

1 回答 1

0

Sounds like when you rotated 90 the orientation of the view changed as well, so that the Y axis became the X axis that the user sees. Can you provide the Animator code that actually rotates and translates?

于 2013-06-21T19:17:22.213 回答