balainfant
Newbie
I am developing an android app which has feature that resizing and rotating the imageview by dragging its bottom right corner button. I saw one app which has feature that if we drag the bottom right corner button diagonally imageview size had resized or else if we drag the button left or right side direction imageview had rotated as per direction. I wish to implement this feature in my app I am struggling to implement single finger rotation as well as resizing the imageview. I could successfully implement resizing the imageview by dragging its bottom right corner button. But I do not have enough knowledge to add rotation to the image view Please guide me in right way.
am done with scale logic but rotation not able to do ,please help me anybody.
my code is
---------------------
package com.example.cropandroidimage; import java.io.ByteArrayOutputStream; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.util.AttributeSet; import android.view.Menu; import android.view.MotionEvent; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; public class MainActivity extends Activity { ViewGroup lLayout; static ImageView img, backgrndImg; Canvas mCanvas; int newWidth = 400; int newHeight = 400; private float mAspectQuotient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lLayout = (FrameLayout)findViewById(R.id.lLayout); // img = (ImageView)findViewById(R.id.imageView); // backgrndImg = (ImageView)findViewById(R.id.imageView_back); // img.setDrawingCacheEnabled(true); // backgrndImg.setDrawingCacheEnabled(true); final CropView cv = new CropView(this); lLayout.addView(cv); // Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.aviary_adjust_knob); // TouchImageView tmv = new TouchImageView(this); // tmv.setImageBitmap(bitmap); // tmv.setMaxZoom(4f); // lLayout.addView(tmv); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public class CropView extends ImageView { private static final int SELECTION_RECT_PAINT_COLOR = 0xFF000000; private static final int SELECTION_RECT_FILL_COLOR = 0x70FFFFFF; private static final int TOUCH_TOLERANCE = 25; private static final int xInc = 25; private static final int yInc = 25; Paint paint = new Paint(); private int initial_size = 200; private Point leftTop, rightBottom, center, previous, currentPoint, rectPos; private Paint fillPaint; private Paint rectPaint; protected Rect selection, dest; private boolean isAffectedBottom = false; Bitmap bitmap, backgroundBitmap; Rect rectf; Rect knobRect; private Context mContext; int width, height; // Adding parent class constructors public CropView(Context context) { super(context); // bitmapDrawable = getResources().getDrawable(R.drawable.aviary_adjust_knob); // bitmapDrawable.setBounds(0, 0, bitmapDrawable.getIntrinsicWidth(), bitmapDrawable.getIntrinsicHeight()); mContext = context; backgroundBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.toast_bkgrd); bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.aviary_adjust_knob); rectPaint = new Paint(); rectPaint.setStyle(Style.STROKE); rectPaint.setColor(SELECTION_RECT_PAINT_COLOR); fillPaint = new Paint(); fillPaint.setStyle(Style.FILL); fillPaint.setColor(SELECTION_RECT_FILL_COLOR); currentPoint = new Point(getWidth()/2, getHeight()/2); width = backgroundBitmap.getWidth(); height = backgroundBitmap.getHeight(); initCropView(); } public CropView(Context context, AttributeSet attrs) { super(context, attrs, 0); initCropView(); } public CropView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initCropView(); } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mCanvas = canvas; if(leftTop.equals(0, 0)) resetPoints(); mCanvas.save(); mCanvas.drawBitmap(backgroundBitmap, null, selection, rectPaint); mCanvas.drawBitmap(bitmap, selection.right-25, selection.bottom-25, null); rectPos.set(selection.left, selection.top); mCanvas.restore(); } private void calculateAspectQuotient() { if (backgroundBitmap != null) { mAspectQuotient = (((float)backgroundBitmap.getWidth()) / backgroundBitmap.getHeight()) / (((float)getWidth()) / getHeight()); } } @Override public boolean onTouchEvent(MotionEvent event) { int eventaction = event.getAction(); switch (eventaction) { case MotionEvent.ACTION_DOWN: touchDown((int)event.getX(), (int)event.getY()); // currentPoint.set((int)event.getX(), (int)event.getY()); previous.set((int)event.getX(), (int)event.getY()); break; case MotionEvent.ACTION_MOVE: touchMove((int)event.getX(), (int)event.getY()); if(isActionInsideRectangle(event.getX(), event.getY())&&!isAffectedBottom) { drag((int)event.getX(), (int)event.getY()); invalidate(); // redraw rectangle previous.set((int)event.getX(), (int)event.getY()); } previous.set((int)event.getX(), (int)event.getY()); break; case MotionEvent.ACTION_UP: touchUp((int)event.getX(), (int)event.getY()); previous = new Point(); // bitmapPreviousPoint = new Point(); break; } return true; } private void initCropView() { paint.setColor(Color.WHITE); paint.setStyle(Style.STROKE); paint.setStrokeWidth(5); leftTop = new Point(); rightBottom = new Point(); center = new Point(); previous = new Point(); rectPos = new Point(); // bitmapPreviousPoint = new Point(); } public void resetPoints() { center.set(getWidth()/2, getHeight()/2); leftTop.set((getWidth()-initial_size)/2,(getHeight()-initial_size)/2); rightBottom.set(leftTop.x+initial_size, leftTop.y+initial_size); selection = new Rect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y); knobRect = new Rect(selection.right, selection.bottom, bitmap.getWidth(), bitmap.getHeight()); dest = selection; } private boolean isActionInsideRectangle(float x, float y) { int buffer = 10; return (x>=(selection.left)&&x<=(selection.right)&& y>=(selection.top)&&y<=(selection.bottom))?true:false; } void touchDown(int x, int y){ // if ((int)event.getX() >= rightBottom.x-5 && (int)event.getX() < ((rightBottom.x-5) + bitmap.getWidth()) // && (int)event.getY() >= rightBottom.y-5 && (int)event.getY() < ((rightBottom.y-5) + bitmap.getHeight())) { // //tada, if this is true, you've started your click inside your bitmap // System.out.println("touched bitmap images"); // previous.set((int)event.getX(), (int)event.getY()); //// bitmapPreviousPoint.set((int)event.getX(), (int)event.getY()); // } // System.out.println("isAffectedBottom "+isAffectedBottom); System.out.println("selection "+selection); currentPoint.set(x, y); if(pointsAreClose(x, y, selection.right , selection.bottom)) { isAffectedBottom = true; System.out.println("isAffectedBottom "+isAffectedBottom); } } void touchMove(int x, int y){ currentPoint.set(x, y); if(isAffectedBottom){ int dx = (previous.x - x)/2; int dy = (previous.y - y)/2; selection.inset(dx, dy); invalidate(); } } void touchUp(int x, int y){ currentPoint.set(x, y); isAffectedBottom = false; } private boolean pointsAreClose(float x1, float y1, float x2, float y2) { return Math.hypot(x1-x2, y1-y2) < TOUCH_TOLERANCE; } private void drag(int x, int y){ int movement; movement = x-previous.x; int movementY = y-previous.y; // if(isInImageRange(new PointF(selection.left+movement,selection.top+movementY)) && isInImageRange(new PointF(selection.right+movement,selection.bottom+movementY))) { // leftTop.set(leftTop.x+movement,leftTop.y+movementY); // rightBottom.set(rightBottom.x+movement,rightBottom.y+movementY); // selection.set(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y); selection.set(selection.left+movement, selection.top+movementY, selection.right+movement, selection.bottom+movementY); // mCanvas.drawBitmap(backgroundBitmap, selection, selection, rectPaint); selection.sort(); invalidate(); // } } public Bitmap getCroppedImage() { BitmapDrawable drawable = (BitmapDrawable)getDrawable(); float x = leftTop.x-center.x+(drawable.getBitmap().getWidth()/2); float y = leftTop.y-center.y+(drawable.getBitmap().getHeight()/2); Bitmap cropped = Bitmap.createBitmap(drawable.getBitmap(),(int)x,(int)y,(int)rightBottom.x-(int)leftTop.x,(int)rightBottom.y-(int)leftTop.y); ByteArrayOutputStream stream = new ByteArrayOutputStream(); cropped.compress(Bitmap.CompressFormat.PNG, 100, stream); return cropped; } } }
am done with scale logic but rotation not able to do ,please help me anybody.
my code is
---------------------
package com.example.cropandroidimage; import java.io.ByteArrayOutputStream; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.util.AttributeSet; import android.view.Menu; import android.view.MotionEvent; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; public class MainActivity extends Activity { ViewGroup lLayout; static ImageView img, backgrndImg; Canvas mCanvas; int newWidth = 400; int newHeight = 400; private float mAspectQuotient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lLayout = (FrameLayout)findViewById(R.id.lLayout); // img = (ImageView)findViewById(R.id.imageView); // backgrndImg = (ImageView)findViewById(R.id.imageView_back); // img.setDrawingCacheEnabled(true); // backgrndImg.setDrawingCacheEnabled(true); final CropView cv = new CropView(this); lLayout.addView(cv); // Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.aviary_adjust_knob); // TouchImageView tmv = new TouchImageView(this); // tmv.setImageBitmap(bitmap); // tmv.setMaxZoom(4f); // lLayout.addView(tmv); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public class CropView extends ImageView { private static final int SELECTION_RECT_PAINT_COLOR = 0xFF000000; private static final int SELECTION_RECT_FILL_COLOR = 0x70FFFFFF; private static final int TOUCH_TOLERANCE = 25; private static final int xInc = 25; private static final int yInc = 25; Paint paint = new Paint(); private int initial_size = 200; private Point leftTop, rightBottom, center, previous, currentPoint, rectPos; private Paint fillPaint; private Paint rectPaint; protected Rect selection, dest; private boolean isAffectedBottom = false; Bitmap bitmap, backgroundBitmap; Rect rectf; Rect knobRect; private Context mContext; int width, height; // Adding parent class constructors public CropView(Context context) { super(context); // bitmapDrawable = getResources().getDrawable(R.drawable.aviary_adjust_knob); // bitmapDrawable.setBounds(0, 0, bitmapDrawable.getIntrinsicWidth(), bitmapDrawable.getIntrinsicHeight()); mContext = context; backgroundBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.toast_bkgrd); bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.aviary_adjust_knob); rectPaint = new Paint(); rectPaint.setStyle(Style.STROKE); rectPaint.setColor(SELECTION_RECT_PAINT_COLOR); fillPaint = new Paint(); fillPaint.setStyle(Style.FILL); fillPaint.setColor(SELECTION_RECT_FILL_COLOR); currentPoint = new Point(getWidth()/2, getHeight()/2); width = backgroundBitmap.getWidth(); height = backgroundBitmap.getHeight(); initCropView(); } public CropView(Context context, AttributeSet attrs) { super(context, attrs, 0); initCropView(); } public CropView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initCropView(); } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mCanvas = canvas; if(leftTop.equals(0, 0)) resetPoints(); mCanvas.save(); mCanvas.drawBitmap(backgroundBitmap, null, selection, rectPaint); mCanvas.drawBitmap(bitmap, selection.right-25, selection.bottom-25, null); rectPos.set(selection.left, selection.top); mCanvas.restore(); } private void calculateAspectQuotient() { if (backgroundBitmap != null) { mAspectQuotient = (((float)backgroundBitmap.getWidth()) / backgroundBitmap.getHeight()) / (((float)getWidth()) / getHeight()); } } @Override public boolean onTouchEvent(MotionEvent event) { int eventaction = event.getAction(); switch (eventaction) { case MotionEvent.ACTION_DOWN: touchDown((int)event.getX(), (int)event.getY()); // currentPoint.set((int)event.getX(), (int)event.getY()); previous.set((int)event.getX(), (int)event.getY()); break; case MotionEvent.ACTION_MOVE: touchMove((int)event.getX(), (int)event.getY()); if(isActionInsideRectangle(event.getX(), event.getY())&&!isAffectedBottom) { drag((int)event.getX(), (int)event.getY()); invalidate(); // redraw rectangle previous.set((int)event.getX(), (int)event.getY()); } previous.set((int)event.getX(), (int)event.getY()); break; case MotionEvent.ACTION_UP: touchUp((int)event.getX(), (int)event.getY()); previous = new Point(); // bitmapPreviousPoint = new Point(); break; } return true; } private void initCropView() { paint.setColor(Color.WHITE); paint.setStyle(Style.STROKE); paint.setStrokeWidth(5); leftTop = new Point(); rightBottom = new Point(); center = new Point(); previous = new Point(); rectPos = new Point(); // bitmapPreviousPoint = new Point(); } public void resetPoints() { center.set(getWidth()/2, getHeight()/2); leftTop.set((getWidth()-initial_size)/2,(getHeight()-initial_size)/2); rightBottom.set(leftTop.x+initial_size, leftTop.y+initial_size); selection = new Rect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y); knobRect = new Rect(selection.right, selection.bottom, bitmap.getWidth(), bitmap.getHeight()); dest = selection; } private boolean isActionInsideRectangle(float x, float y) { int buffer = 10; return (x>=(selection.left)&&x<=(selection.right)&& y>=(selection.top)&&y<=(selection.bottom))?true:false; } void touchDown(int x, int y){ // if ((int)event.getX() >= rightBottom.x-5 && (int)event.getX() < ((rightBottom.x-5) + bitmap.getWidth()) // && (int)event.getY() >= rightBottom.y-5 && (int)event.getY() < ((rightBottom.y-5) + bitmap.getHeight())) { // //tada, if this is true, you've started your click inside your bitmap // System.out.println("touched bitmap images"); // previous.set((int)event.getX(), (int)event.getY()); //// bitmapPreviousPoint.set((int)event.getX(), (int)event.getY()); // } // System.out.println("isAffectedBottom "+isAffectedBottom); System.out.println("selection "+selection); currentPoint.set(x, y); if(pointsAreClose(x, y, selection.right , selection.bottom)) { isAffectedBottom = true; System.out.println("isAffectedBottom "+isAffectedBottom); } } void touchMove(int x, int y){ currentPoint.set(x, y); if(isAffectedBottom){ int dx = (previous.x - x)/2; int dy = (previous.y - y)/2; selection.inset(dx, dy); invalidate(); } } void touchUp(int x, int y){ currentPoint.set(x, y); isAffectedBottom = false; } private boolean pointsAreClose(float x1, float y1, float x2, float y2) { return Math.hypot(x1-x2, y1-y2) < TOUCH_TOLERANCE; } private void drag(int x, int y){ int movement; movement = x-previous.x; int movementY = y-previous.y; // if(isInImageRange(new PointF(selection.left+movement,selection.top+movementY)) && isInImageRange(new PointF(selection.right+movement,selection.bottom+movementY))) { // leftTop.set(leftTop.x+movement,leftTop.y+movementY); // rightBottom.set(rightBottom.x+movement,rightBottom.y+movementY); // selection.set(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y); selection.set(selection.left+movement, selection.top+movementY, selection.right+movement, selection.bottom+movementY); // mCanvas.drawBitmap(backgroundBitmap, selection, selection, rectPaint); selection.sort(); invalidate(); // } } public Bitmap getCroppedImage() { BitmapDrawable drawable = (BitmapDrawable)getDrawable(); float x = leftTop.x-center.x+(drawable.getBitmap().getWidth()/2); float y = leftTop.y-center.y+(drawable.getBitmap().getHeight()/2); Bitmap cropped = Bitmap.createBitmap(drawable.getBitmap(),(int)x,(int)y,(int)rightBottom.x-(int)leftTop.x,(int)rightBottom.y-(int)leftTop.y); ByteArrayOutputStream stream = new ByteArrayOutputStream(); cropped.compress(Bitmap.CompressFormat.PNG, 100, stream); return cropped; } } }