缩放和旋转图像的Andr​​oid不使用的ImageView [英] Zoom and rotate image in android without using imageview

查看:197
本文介绍了缩放和旋转图像的Andr​​oid不使用的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid,我想为 缩放和旋转我的形象不使用的ImageView 。我可以拖动图像。

i am new to android and i want to zoom and rotate my image without using Imageview . i can drag the image.

code:

public class SimpImageView extends BaseView{

    private Bitmap image = null;

    private Paint borderPaint = null;

    PointF start = new PointF();
    PointF lastPosition = new PointF();

    private Matrix savedMatrix = new Matrix();

    private Paint imagePaint = null;

    private Matrix matrix = new Matrix();

    float initialRotation;
    float currentRotation;

    private float initialScale;

    private float currentScale;

    PointF mid = new PointF();
    float oldDist = 1f;

    private Bitmap rotatedImage;




    public Bitmap getImage() {
        return image;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }

    public SimpImageView(Bitmap image,int x,int y,int width,int height){
        super(x, y, width, height);
        this.image = image;
        imagePaint = new Paint();
        imagePaint.setARGB(32, 255, 255, 255);
        imagePaint.setStyle(Paint.Style.FILL);


    }

    /* (non-Javadoc)
     * @see com.simplogics.surfaceview.Views.BaseView#onDraw(android.graphics.Canvas)
     */
    @Override
    public void onDraw(Canvas canvas) {


        canvas.drawBitmap(getImage(), getBounds().left,getBounds().top, new Paint());

    }

    /* (non-Javadoc)
     * @see com.simplogics.surfaceview.Views.BaseView#onTouchEvent(android.view.MotionEvent)
     */

    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean handled = false;

        Log.d("SimpEditText", "(int)event.getX() " + (int)event.getX());
        Log.d("SimpEditText", "(int)event.getY() " + (int)event.getY());

        if(isTouchedInBounds((int)event.getX(), (int)event.getY())){
            Log.d("SimpEditText", "Touched in Bounds");
            handled = true;
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                lastPosition.x = getBounds().left;
                lastPosition.y = getBounds().top;

                Log.d("VerticalLabelView", "mode=DRAG");
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                mode = ZOOM;

                initialScale = spacing(event);
                midPoint(mid, event);
                Log.d("VerticalLabelView", "mode=ZOOM");
                currentRotation = rotation(event);

                break;
            case MotionEvent.ACTION_UP:
                mode = NONE;
                break;
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                Log.d("VerticalLabelView", "mode=NONE");
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    int x = (int)(event.getX() - start.x);
                    int y = (int)(event.getY() - start.y);
                    setPosition((int)lastPosition.x + x, (int)lastPosition.y + y);

                }else if(mode == ZOOM){

                    currentScale = spacing(event);
                    float scale =currentScale/initialScale;
                      this.image = rotatedImage;
                      this.setScale(0, 0,(int) (getBounds().width()*scale),(int) (getBounds().height()*scale));

                }
                break;
            }


        }




        return handled;

    }


    private float spacing(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return FloatMath.sqrt(x * x + y * y);
    }
    private void midPoint(PointF point, MotionEvent event) {
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
    }
    private float rotation(MotionEvent event) {
        double delta_x = (event.getX(0) - event.getX(1));
        double delta_y = (event.getY(0) - event.getY(1));
        double radians = Math.atan2(delta_y, delta_x);
        Log.d("Rotation ~~~~~~~~~~~~~~~~~", delta_x + " ## " + delta_y + " ## "
                + radians + " ## " + Math.toDegrees(radians));
        return (float) Math.toDegrees(radians);
    }

}

BaseView.java 类:

public abstract class BaseView {

    private int id = -1;

    private Object tag = null;

    private Rect bounds = null;

    public Rect getBounds() {
        return bounds;
    }

    public BaseView() {
        bounds = new Rect();
    }

    public BaseView(int x, int y, int width, int height) {
        bounds = new Rect(x, y, x + width, y + height);
    }

    public abstract void onDraw(Canvas canvas);

    public abstract  boolean onTouchEvent(MotionEvent event);

    public void setBounds(int x, int y, int width, int height){
        bounds.set(x, y, x + width, y + height);
    }

    public void setPosition(int x, int y){
        if(x <= 0 || y <= 0){
            Log.e("SimpEditText1", "----------------------------------------------------------");
            Log.e("SimpEditText1", "-------------0000000000000000000000000000000000------------");
            Log.e("SimpEditText1", "----------------------------------------------------------");
        }
        bounds.set(x, y, x + bounds.width(), y + bounds.height());
    }

    public void setScale(int x,int y,int width,int height){
        bounds.set(x, y, width,height);
    }

    protected boolean isTouchedInBounds(int xPos, int yPos){
        return bounds.contains(xPos, yPos);
    }

    public Object getTag() {
        return tag;
    }

    public void setTag(Object tag) {
        this.tag = tag;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    /**
     * @param w
     * @param h
     * @param oldw
     * @param oldh
     */
}

请告诉我的错误在我的code。

please tell me the error in my code.

推荐答案

:<一href=\"http://stackoverflow.com/questions/10549066/image-scaling-with-rotation-in-android-like-photoshop/10549876#10549876\">image用Photoshop等

它可以帮助你:

的http://机器人-ER。 blogspot.in/2010/07/skew-bitmap-image-using-matrix.html

这篇关于缩放和旋转图像的Andr​​oid不使用的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆