开发者

New class which extends imageview does not rotate like regular imageview class?

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) Ma开发者_C百科th.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;

}

I am trying to rotate the imageview. It works all fine when I use imageview class but it will not work for the imageview I create with my own class which extends the imageview. What did I do wrong?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜