0

私はアンドロイドを初めて使用し、使用せずに画像をズームおよび回転Imageviewしたいと考えています。画像をドラッグできます。

コード:

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
     */
}

私のコードのエラーを教えてください。

4

1 に答える 1

1

from : Photoshop のような Android での回転による画像のスケーリング

それはあなたを助けるかもしれません:

http://android-er.blogspot.in/2010/07/skew-bitmap-image-using-matrix.html

于 2012-05-28T09:43:22.260 に答える