nineoldandroids库使视图不稳定

时间:2013-02-28 09:16:30

标签: java android viewhelper

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向左翻译。

1 个答案:

答案 0 :(得分:0)

当您旋转90时,听起来也改变了视图的方向,因此Y轴成为用户看到的X轴。你能提供实际旋转和翻译的动画师代码吗?