而变焦和捏的Andr​​oid的绘画路径 [英] Android Drawing Path while Zoom and Pinch

查看:99
本文介绍了而变焦和捏的Andr​​oid的绘画路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画位图,并尝试使用Canvas.for图纸我加入做一些画了它
每当用户借鉴它表面的东西路径。我已经实现双指缩放和拖动拥有超过位图,但是当用户放大位图和借鉴的东西就可以了,然后路径不恰当地获得平局。我使用的是矩阵捏缩放和拖动功能。
与矩阵我的问题是使用的路径。

I'm drawing bitmap and trying to do some drawing over it using Canvas.for drawing i'm adding path whenever user draw something on it surface. I have implemented pinch Zoom and drag features over bitmap but when user Zoom the bitmap and draw something on it then Path doesn't get draw appropriately. I'm using Matrix for pinch zoom and drag features. My problem is using the Path with with Matrix.

推荐答案

这是一个工作的例子,你可以看到如何实现拖动和缩放路径:

This is a working example where you can see how to implement a draggable and zoomable Path:

MainActivity

MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class DrawActivity extends Activity {

        DrawView drawView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Set full screen view
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
            requestWindowFeature(Window.FEATURE_NO_TITLE);

            drawView = new DrawView(DrawActivity.this);

            setContentView(drawView);
        }    
}

DrawView

DrawView

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.WindowManager;

public class DrawView extends View {

    Context ctx;

    static final String TAG = "DrawView";

    Paint paint = new Paint();

    //These two constants specify the minimum and maximum zoom
    private static float MIN_ZOOM = 3f;
    private static float MAX_ZOOM = 10f;

    private float scaleFactor = 3.f;
    private ScaleGestureDetector detector;

    //These two variables keep track of the X and Y coordinate of the finger when it first
    //touches the screen
    private float startX = 0f;
    private float startY = 0f;

    //These two variables keep track of the amount we need to translate the canvas along the X
    //and the Y coordinate
    private float translateX = 0f;
    private float translateY = 0f;

    //These two variables keep track of the amount we translated the X and Y coordinates, the last time we
    //panned.
    private float previousTranslateX = 0f;
    private float previousTranslateY = 0f; 

    private boolean dragged = false;

// Used for set first translate to a quarter of screen
    private float displayWidth;
    private float displayHeight;

    public DrawView(Context context)
    {
        super(context);

        ctx = context;

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        DisplayMetrics metrics = new DisplayMetrics();

        display.getMetrics(metrics);

        displayWidth = metrics.widthPixels;
        displayHeight = metrics.heightPixels;

        translateX = displayWidth/4;
        translateY = displayHeight/4; 

        previousTranslateX = displayWidth/4;
        previousTranslateY = displayHeight/4;

        detector = new ScaleGestureDetector(context, new ScaleListener());

        setFocusable(true);
        setFocusableInTouchMode(true);       

// Path's color
        paint.setColor(Color.GRAY);
        paint.setAntiAlias(false); 
    }

    @Override
    public void onDraw(Canvas canvas) {     
        super.onDraw(canvas);

        canvas.save();

        //We're going to scale the X and Y coordinates by the same amount
        canvas.scale(scaleFactor, scaleFactor, 0, 0);

        //We need to divide by the scale factor here, otherwise we end up with excessive panning based on our zoom level
        //because the translation amount also gets scaled according to how much we've zoomed into the canvas.
        canvas.translate((translateX) / scaleFactor, (translateY) / scaleFactor);

        canvas.drawRect(0, 0, 100, 100, paint);

        canvas.restore(); 

    }   

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN:

// First finger is on screen

            //We assign the current X and Y coordinate of the finger to startX and startY minus the previously translated
            //amount for each coordinates This works even when we are translating the first time because the initial
            //values for these two variables is zero.
            startX = event.getX() - previousTranslateX;
            startY = event.getY() - previousTranslateY;
            break;

        case MotionEvent.ACTION_MOVE:

// first finger is moving on screen  
// update translation value to apply on Path

            translateX = event.getX() - startX;
            translateY = event.getY() - startY;             

            break;

        case MotionEvent.ACTION_UP:

// No more fingers on screen

            // All fingers went up, so let's save the value of translateX and translateY into previousTranslateX and
            //previousTranslate
            previousTranslateX = translateX;
            previousTranslateY = translateY;
            break;

// All touch events are sended to ScaleListener
        detector.onTouchEvent(event);

        return true;
    }   

    class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {


            scaleFactor *= detector.getScaleFactor();
            scaleFactor = Math.max(MIN_ZOOM, Math.min(scaleFactor, MAX_ZOOM));

            invalidate();
            return true;
        }   
    }
}

这篇关于而变焦和捏的Andr​​oid的绘画路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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