package com.myView; import com.androidphoto.unique.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Bitmap.Config; import android.util.FloatMath; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class Draw extends View { Bmp bmp[]; public Draw(Context context) { super(context); int len ; len = 4; bmp = new Bmp[len]; { for(int i = 0; i < len; i++) { // bmp[i] = new Bmp(BitmapFactory.decodeFile(path[i]), i, i * 50f, i * 60f); bmp[i] = new Bmp(Bitmap.createScaledBitmap(BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.you)), 240, 240, false), i, i * 50f, i * 60f); bmp[i].width = bmp[i].getPic().getWidth(); bmp[i].height = bmp[i].getPic().getWidth(); } } this.pic = bmp; this.canvas.drawColor(-232432445); for(int i = 0; i < 4; i++) { tempBitmap = pic[0].findByPiority(pic, i); tempBitmap.matrix.preTranslate(tempBitmap.getXY(1) - tempBitmap.getWidth() / 2, tempBitmap.getXY(2) - tempBitmap.getHeight() / 2); this.canvas.drawBitmap(tempBitmap.pic, tempBitmap.matrix, null); } } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(canvasBitmap, 0, 0, null); } @Override public boolean onTouchEvent(MotionEvent event) { // 向上弹起 if(//event.getAction() == MotionEvent.ACTION_UP event.getAction() == MotionEvent.ACTION_POINTER_1_UP || event.getAction() == MotionEvent.ACTION_POINTER_2_UP) { bool = true; if(twoPoint > 0) twoPoint --; } if(event.getAction() == MotionEvent.ACTION_DOWN && event.getPointerCount() == 1) { order(event); this.X = event.getX(); this.Y = event.getY(); CX = pic[3].findByPiority(pic, 3).getXY(1) - event.getX(); CY = pic[3].findByPiority(pic, 3).getXY(2) - event.getY(); Begin = true; } if(event.getAction() == MotionEvent.ACTION_MOVE && Begin && event.getPointerCount() == 1 //&& twoPoint == 0 ) { this.X = event.getX(); this.Y = event.getY(); this.canvas.drawColor(-232432445); for(int i = 0; i < 3; i++) { tempBitmap = pic[0].findByPiority(pic, i); // tempBitmap.matrix.preTranslate(0f, 0f); canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null); } tempBitmap = pic[0].findByPiority(pic, 3); rotalP = rotalPoint(new float[]{this.X, this.Y}, tempBitmap.preX, tempBitmap.preY, tempBitmap.width / 2, tempBitmap.height / 2, tempBitmap.matrix); if( (Math.abs(X - pic[0].findByPiority(pic, 3).getXY(1)) < pic[0].findByPiority(pic, 3).getWidth() / 2) && (Math.abs(Y - pic[0].findByPiority(pic, 3).getXY(2)) < pic[0].findByPiority(pic, 3).getHeight() / 2) ) { Log.i("the reeal width", String.valueOf(tempBitmap.getWidth())); rotalC = this.getT(tempBitmap.width / 2, tempBitmap.height / 2 , X + CX, Y + CY, tempBitmap.matrix); canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null); tempBitmap.preX = X + CX; tempBitmap.preY = Y + CY; } else { // tempBitmap.matrix.preTranslate(0f, 0f); canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null); } } // 两指移动 if(event.getPointerCount() >= 2 && event.getAction() == MotionEvent.ACTION_MOVE) { twoPoint = 2; this.canvas.drawColor(-232432445); invalidate(); for(int i = 0; i < 3; i++) { tempBitmap = pic[0].findByPiority(pic, i); this.canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null); } tempBitmap = pic[0].findByPiority(pic, 3); rotalP = rotalPoint(new float[]{event.getX(0), event.getY(0)}, tempBitmap.preX, tempBitmap.preY, tempBitmap.width / 2, tempBitmap.height / 2, tempBitmap.matrix); rotalP_2 = rotalPoint(new float[]{event.getX(1), event.getY(1)}, tempBitmap.preX, tempBitmap.preY, tempBitmap.width / 2, tempBitmap.height / 2, tempBitmap.matrix); if( (Math.abs(rotalP[0] - pic[0].findByPiority(pic, 3).getXY(1)) < pic[0].findByPiority(pic, 3).width / 2) && (Math.abs(rotalP[1] - pic[0].findByPiority(pic, 3).getXY(2)) < pic[0].findByPiority(pic, 3).height / 2) && (Math.abs(rotalP_2[0] - pic[0].findByPiority(pic, 3).getXY(1)) < pic[0].findByPiority(pic, 3).width / 2) && (Math.abs(rotalP_2[1] - pic[0].findByPiority(pic, 3).getXY(2)) < pic[0].findByPiority(pic, 3).height / 2) || true ) { if(bool) { preLength = spacing(event); preCos = cos(event); bool = false; } // 获取角度和长度 length = spacing(event); cos = cos(event); // 放大和缩小 if(length - preLength != 0) { tempBitmap.width *= (1.0f + (length - preLength) / length); tempBitmap.height *= (1.0f + (length - preLength) / length); tempBitmap.pic = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.you)), (int)tempBitmap.width, (int) tempBitmap.height, false); Log.i("new Width", String.valueOf(pic[0].findByPiority(pic, 3).getWidth())); scale(tempBitmap.width / 2, tempBitmap.height / 2 , tempBitmap.preX, tempBitmap.preY, tempBitmap.matrix); } // 旋转 if(Math.abs(cos) > 3 && Math.abs(cos) < 177 && Math.abs(cos - preCos) < 15) { tempBitmap.matrix.postRotate(cos - preCos); this.getT(tempBitmap.width / 2, tempBitmap.height / 2 , tempBitmap.preX, tempBitmap.preY, tempBitmap.matrix); } preCos = cos; preLength = length; // this.getT(tempBitmap.width / 2, tempBitmap.height / 2 , tempBitmap.preX, tempBitmap.preY, tempBitmap.matrix); } // 初始位置 canvas.drawBitmap(tempBitmap.getPic(), tempBitmap.matrix, null); Log.i("preX and preY", String.valueOf(tempBitmap.preX) + " : " + String.valueOf(tempBitmap.preY)); } if(event.getAction() == MotionEvent.ACTION_UP) { CX = 0f; CY = 0f; Begin = false; } invalidate(); Log.i("preX and preY", String.valueOf(tempBitmap.preX) + " : " + String.valueOf(tempBitmap.preY)); return true; } public void order(MotionEvent event) { Bmp temp = null; for(int i = 3; i > -1; i--) { rotalP = rotalPoint(new float[]{event.getX(), event.getY()}, pic[0].findByPiority(pic, i).preX, pic[0].findByPiority(pic, i).preY, pic[0].findByPiority(pic, i).width / 2, pic[0].findByPiority(pic, i).height / 2, pic[0].findByPiority(pic, i).matrix); if( (Math.abs(pic[0].findByPiority(pic, i).getXY(1) - rotalP[0]) < pic[0].findByPiority(pic, i).width / 2) && (Math.abs(pic[0].findByPiority(pic, i).getXY(2) - rotalP[1]) < pic[0].findByPiority(pic, i).height / 2) ) { temp = pic[0].findByPiority(pic, i); for(Bmp bmp: pic) { if(bmp.getPriority() > pic[0].findByPiority(pic, i).getPriority()) { bmp.priority--; } } temp.setPiority(3); Begin = true; return; } } } public float[] getT(float preX, float preY, float x, float y, Matrix matrix) { float[] re = new float[2]; float[] matrixArray = new float[9]; matrix.getValues(matrixArray); float a = x - preX * matrixArray[0] - preY * matrixArray[1]; float b = y - preX * matrixArray[3] - preY * matrixArray[4]; matrixArray[2] = a; matrixArray[5] = b; matrix.setValues(matrixArray); re[0] = a; re[1] = b; return re; } public void scale(float preX, float preY, float x, float y, Matrix matrix) { float[] matrixArray = new float[9]; matrix.getValues(matrixArray); float a = x - preX; float b = y - preY; matrixArray[2] = a; matrixArray[5] = b; matrix.setValues(matrixArray); } public void setToO(Matrix matrix) { float[] matrixArray = new float[9]; matrix.getValues(matrixArray); float a = 0f; float b = 0f; matrixArray[2] = a; matrixArray[5] = b; matrix.setValues(matrixArray); } public float[] rotalPoint(float[] p, float X, float Y, float width, float height, Matrix matrix) { float re[] = new float[2]; float matrixArray[] = new float[9]; matrix.getValues(matrixArray); float a = p[0] - X; float b = p[1] - Y; re[0] = a * matrixArray[0] - b * matrixArray[1] + X; re[1] = - a * matrixArray[3] + b * matrixArray[4] + Y; return re; } // 计算长度 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 float cos(MotionEvent event) { // Log.i("XY", String.valueOf(event.getX(0)) // + " " + String.valueOf(event.getY(0)) // + " " + String.valueOf(event.getX(1)) // + " " + String.valueOf(event.getY(1))); if((event.getX(0) - event.getX(1)) * (event.getY(0) - event.getY(1)) > 0) { return (float) ((float) Math.acos(Math.abs(event.getX(0) - event.getX(1)) / spacing(event)) / Math.PI * 180f); } if((event.getX(0) - event.getX(1)) * (event.getY(0) - event.getY(1)) < 0) { return (float) ((float) Math.acos(- Math.abs(event.getX(0) - event.getX(1)) / spacing(event)) / Math.PI * 180f); } if(event.getX(0) - event.getX(1) == 0) { return (float) 90f; } if(event.getY(0) - event.getY(1) == 0) { return 0f; } return 45f; } private Bitmap canvasBitmap = Bitmap.createBitmap(480, 500, Config.ARGB_8888); private Bmp tempBitmap = null; private Canvas canvas = new Canvas(canvasBitmap); private float X = 0f; private float Y = 0f; @SuppressWarnings("unused") private float DownX = 0f; @SuppressWarnings("unused") private float DownY = 0f; private Bmp[] pic = new Bmp[4]; private float CX = 0f; private float CY = 0f; private boolean Begin = true; float rotalC[] = new float[2]; float rotalP[] = new float[2]; float rotalP_2[] = new float[2]; int twoPoint = 0; private float preLength = 480.0f; private float length = 480.0f; private float preCos = 0f; private float cos = 0f; private boolean bool = true; } // @param pic:the Bitmap to draw // @param piority: the order to draw picture // @param preX,preY: the X and Y class Bmp { // 构造器 public Bmp(Bitmap pic, int piority) { this.pic = pic; this.priority = piority; } // 构造器 public Bmp(Bitmap pic, int priority, float preX, float preY) { this(pic, priority); this.preX = preX + pic.getWidth() / 2 * 1.5f; this.preY = preY + pic.getHeight() / 2 * 1.5f; } // findPiority public Bmp findByPiority(Bmp[] pic, int priority) { for(Bmp p : pic) { if(p.priority == priority) { return p; } } return null; } // set Priority public void setPiority(int priority) { this.priority = priority; } // return Priority public int getPriority() { return this.priority; } // return X and Y public float getXY(int i) { if(i == 1) { return this.preX; } else if(i == 2) { return this.preY; } return (Float) null; } // getPicture public Bitmap getPic() { return this.pic; } // getWidth public float getWidth() { return width; } // getHeight public float getHeight() { return height; } float preX = 0; float preY = 0; float width = 0; float height = 0; Bitmap pic = null; int priority = 0; Matrix matrix = new Matrix(); }