android - 在android中的 View 上绘制和缩放自定义矩形

标签 android

我想自定义 View ,它可以包括一些效果:首先在 View 上绘制一个图片作为背景然后在图像上绘制一个矩形,并在矩形中绘制另一个名为A的图像,我们知道矩形有四个固定的点,当我拖动其中一个时,矩形可以缩放,同时 A 也是缩放,我已经阅读了更多链接,但没有找到好的例子,我做了一些事情,但无法完成缩放矩形,我的代码是:

public class DrawView extends View implements OnTouchListener {
private static final String TAG = "DrawView";

private static final int LineLength = 30;

Paint paint = new Paint();
float locationX, locationY;
private int mLastTouchX;
private int mLastTouchY;

private int mPosX;
private int mPosY;

private int mPosX1;
private int mPosY1;
Bitmap bitmap, bmp, xiao;


int screenWidth, screenHeight;

int xLength;

boolean isFirst = true;
boolean isLeft = false;

Rect r, rBig,outRect;

public DrawView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    screenHeight = wm.getDefaultDisplay().getHeight();
    screenWidth = wm.getDefaultDisplay().getWidth();
    mPosX = screenWidth / 2;
    mPosY = screenHeight / 2;

    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
    xiao = BitmapFactory.decodeResource(getResources(),
            R.drawable.msn_protocol);

    xLength = (int) Math.hypot(xiao.getWidth(), xiao.getHeight());

    r = new Rect();
    r.set((int) mPosX - LineLength - xiao.getWidth(), (int) mPosY
            - LineLength - xiao.getHeight(), (int) mPosX - LineLength,
            (int) mPosY - LineLength);

//  Log.i("r", r.left + " " + r.top + " " + r.right + " " + r.bottom);
    rBig = new Rect();
    rBig.set((int) mPosX - LineLength, (int) mPosY - LineLength,
            (int) mPosX + LineLength, (int) mPosY + LineLength);
    //Log.i("r", rBig.left + " " + rBig.top + " " + rBig.right + " " + rBig.bottom);



}

@Override
public void onDraw(Canvas canvas) {



    canvas.drawBitmap(bitmap, 0, 0, null);

    canvas.drawBitmap(xiao, mPosX - LineLength - xiao.getWidth(), mPosY
            - LineLength - xiao.getHeight(), null);


    canvas.drawLine(mPosX - LineLength,
            mPosY - LineLength - xiao.getHeight() / 2, mPosX + LineLength,
            mPosY - LineLength - xiao.getHeight() / 2, paint);


    canvas.drawLine(mPosX - LineLength - xiao.getWidth() / 2, mPosY
            - LineLength, mPosX - LineLength - xiao.getWidth() / 2, mPosY
            + LineLength, paint);


    canvas.drawBitmap(xiao, mPosX + LineLength,
            mPosY - LineLength - xiao.getHeight(), null);


    canvas.drawBitmap(xiao, mPosX - LineLength - xiao.getWidth(), mPosY
            + LineLength, null);


    canvas.drawBitmap(xiao, mPosX + LineLength, mPosY + LineLength, null);


    canvas.drawLine(mPosX + LineLength + xiao.getWidth() / 2, mPosY
            - LineLength, mPosX + LineLength + xiao.getWidth() / 2, mPosY
            + LineLength, paint);


    canvas.drawLine(mPosX - LineLength,
            mPosY + LineLength + xiao.getHeight() / 2, mPosX + LineLength,
            mPosY + LineLength + xiao.getHeight() / 2, paint);


    if (isLeft) {



        Matrix matrix = new Matrix();
        matrix.preScale(0.8f, 0.8f);
        Bitmap rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0,
                bmp.getWidth(), bmp.getHeight(), matrix, true);
        canvas.drawBitmap(rotatedBitmap, mPosX - LineLength, mPosY
                - LineLength, null);


    }

}

public boolean onTouch(View view, MotionEvent event) {

    isFirst = false;
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        mPosX1 = (int) event.getX();
        mPosY1 = (int) event.getY();
        mLastTouchX = mPosX1;
        mLastTouchY = mPosX1;


    //  Log.i("r", r.left + " " + r.top + " " + r.right + " " + r.bottom);
        Log.i("ACTION_DOWN", "" + mPosX1 + " " + mPosY1);
        if (r.contains(mPosX1, mPosY1)) {
            isLeft = true;

            invalidate();
        } 

        break;
    }

    case MotionEvent.ACTION_MOVE: {

         int x = (int) event.getX();
         int y = (int) event.getY();
         Log.i("aa",""+x+""+y);




         int dx = x - mLastTouchX;
         int dy = y - mLastTouchY;

        mLastTouchX = x;
        mLastTouchY = y;

        mPosX += dx;
        mPosY += dy;

        r.set((int) mPosX - LineLength - xiao.getWidth(), (int) mPosY
                - LineLength - xiao.getHeight(), (int) mPosX - LineLength,
                (int) mPosY - LineLength);

        rBig.set((int) mPosX - LineLength, (int) mPosY - LineLength,
                (int) mPosX + LineLength, (int) mPosY + LineLength);

    //  Log.i("r", rBig.left + " " + rBig.top + " " + rBig.right + " " + rBig.bottom);
             invalidate(); 


        break;
    }

    case MotionEvent.ACTION_UP: {

        break;
    }

    case MotionEvent.ACTION_CANCEL: {

        break;
    }

    }
    return true;
}


       }

图片为:http://i.stack.imgur.com/wxi35.png,the系统图库已经完成了效果,但是我调试了源,我失败了,因为我的另一个问题:Imitate crop function of system Galleryhttps://stackoverflow.com/questions/6724218/i-cannot-find-the-initial-value-in-gallery-the-source

最佳答案

我不知道如何绘制矩形,但是,这就是我移动图像的方式

MainPinchView.java

public class MainPinchView extends Activity {
int menuid = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MainPinchImageView obj = new MainPinchImageView(this);
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.my);
    obj.setImage(bmp, 100, 100);
    setContentView(obj);
  }

MainPinchImageView.java

   public class MainPinchImageView extends ImageView {

    private static final String TAG = "Touch";
    // These matrices will be used to move and zoom image
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();

    int flag = 0;

    // We can be in one of these 3 states
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;

    Context context;

    public MainPinchImageView(Context context) {
        super(context);
        super.setClickable(true);
        this.context = context;

        matrix.setTranslate(1f, 1f);
        setImageMatrix(matrix);      // sets the default matrix 
        setScaleType(ScaleType.MATRIX); //Controls how the image should be resized or moved to match the size of this ImageView.

        setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Handle touch events here...
                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    savedMatrix.set(matrix); // 
                    start.set(event.getX(), event.getY());
                    Log.d(TAG, "mode=DRAG");
                    mode = DRAG;
                    break;

                case MotionEvent.ACTION_MOVE:

                    if (mode == DRAG) {
                        // ...
                        matrix.set(savedMatrix);
                        matrix.postTranslate(event.getX() - start.x, event.getY()- start.y);
                    } 
                    break;
                }
                setImageMatrix(matrix);
                return true; // indicate event was handled
            }
        });
    }

    public void setImage(Bitmap bm, int displayWidth, int displayHeight) {
        super.setImageBitmap(bm);

        int displayheight = (getResources().getDisplayMetrics().heightPixels)/2;
        int displaywidth = (getResources().getDisplayMetrics().widthPixels)/2;

        int imgw = displayWidth/2;
        int imgh = displayHeight/2;




        // Fit to screen.
        float scale;
        if ((displayHeight / bm.getHeight()) >= (displayWidth / bm.getWidth())) {
            scale = (float) displayWidth / (float) bm.getWidth();
        } else {
            scale = (float) displayHeight / (float) bm.getHeight();
        }

        savedMatrix.set(matrix);
        matrix.set(savedMatrix);
        matrix.postScale(scale, scale, mid.x, mid.y);
        setImageMatrix(matrix);

        savedMatrix.set(matrix);
        matrix.set(savedMatrix);
        matrix.postTranslate(displaywidth - imgw, displayheight - imgh);

        setImageMatrix(matrix);
    }
}

关于android - 在android中的 View 上绘制和缩放自定义矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6771369/

相关文章:

android - Glide-4.0.0 缺少占位符,错误,GlideApp 并且不解析其方法占位符,错误

java - 带有 SimpleXML 库的 XML - Android 上的性能

android - ZXing 前置摄像头自动启动

android - 断开 USB 后,ADB over WiFi 连接丢失

android - 在 BroadcastReceiver 中使用 sqlite

Android - 按下按钮时将 TextView 添加到布局

android - Activity 构建变体没有测试 Artifact

Android EditText监听器输入

android - HTML anchor 标记(在 jquery mobile 中)在 Android 上不起作用

java - Gson在重建反序列化对象时丢失数据