扩展imageview的新类不像普通的imageview类一样旋转?

时间:2011-08-27 20:35:53

标签: android rotation imageview

package t.circle;

import android.app.Activity;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class circle extends Activity implements OnTouchListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // IV = (ImageView) findViewById(R.id.IV);

        fl = (FrameLayout) findViewById(R.id.frame_lo);
        fl.setOnTouchListener(new LayoutListener());

        CV = new CircleView(this);      
        CV.setImageResource(R.drawable.circle);
        CV.setVisibility(View.INVISIBLE);
        CV.setOnTouchListener(this);


        fl.addView(CV);
    }

    @Override
    public boolean onTouch(View v, MotionEvent e) {
        Log.i("X", String.valueOf(e.getX()));
        Log.i("Center", String.valueOf(center.x) + ","
                + String.valueOf(center.y));
        CircleView imagev = (CircleView) v;

        switch (e.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            iPt.set(e.getX(), e.getY());
        case MotionEvent.ACTION_MOVE:
            cPt.set(e.getX(), e.getY());
            angle = (float) Math.toDegrees(getAngle(center, iPt)
                    - getAngle(center, cPt));
            angle += oldAngle;
            mat.setRotate(-angle, center.x, center.y);
            break;
        case MotionEvent.ACTION_UP:
            oldAngle = angle;
            break;
        }
        imagev.setImageMatrix(mat);
        Log.i("Angle", String.valueOf(angle));
        return true;
    }

    class LayoutListener implements OnTouchListener {

        public boolean onTouch(View v, MotionEvent e) {
            center.set(e.getX(), e.getY());
            CV.setVisibility(View.VISIBLE);
            int l, t, r, b;
            l = (int) (e.getX() - 80);
            r = (int) (e.getX() + 80);
            t = (int) (e.getY() - 80);
            b = (int) (e.getY() + 80);
            CV.setLocation(l, t, r, b);

            return false;
        }
    }

    class CircleView extends ImageView {

        public CircleView(Context context) {
            super(context);
        }

        // Set the location
        public void setLocation(int l, int t, int r, int b) {
            this.setFrame(l, t, r, b);
            this.setImageMatrix(mat);
        }
    }

    /**
     * Get the angle of the current point.
     * 
     * @param origin
     * @param point
     * @return angle in radiant
     */
    private float getAngle(PointF origin, PointF point) {
        float angle = (float) Math
                .atan2(point.y - origin.y, point.x - origin.x);
        if (angle < 0) {
            angle += 2 * Math.PI;
        }
        return angle;
    }

    FrameLayout fl;
    CircleView CV;
    Matrix mat = new Matrix();
    PointF center = new PointF();
    PointF iPt = new PointF();
    PointF cPt = new PointF();
    PointF v1 = new PointF();
    PointF v2 = new PointF();
    private float angle, oldAngle;

}

我正在尝试旋转imageview。当我使用imageview类时,它工作得很好,但它不适用于我用我自己的类创建的imageview,它扩展了imageview。 我做错了什么?

0 个答案:

没有答案