#Override protected void onDraw(Canvas canvas) {
//canvas.drawPicture();
canvas.drawColor(Color.CYAN);
Paint p= new Paint();
p.setColor(Color.RED);
p.setStrokeWidth(20);
canvas.drawLine(100, 200, 400, 500, p);
if(movingObjects.size() > 0){
for (MovingObject ball : movingObjects) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
}
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
boolean redraw = false;
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:
currentMovingObjectId = 0;
for (MovingObject movingObject : movingObjects) {
int topLeftX = movingObject.getX() ;
int topLeftY = movingObject.getY() ;
double imageSize = movingObject.getSize();
double radCircle = Math.sqrt( (double) (((topLeftX-X)*(topLeftX-X)) + (topLeftY-Y)*(topLeftY-Y)));
if ( (radCircle < imageSize) &&
(X- topLeftX) < movingObject.getWidth() && (Y - topLeftY) < movingObject.getHeight() &&
(X- topLeftX) > 0 && (Y- topLeftY) > 0){
currentMovingObjectId = movingObjects.indexOf(movingObject) + 1;
offsetX = X- topLeftX;
offsetY = Y - topLeftY;
redraw = true;
break;
}
}
break;
case MotionEvent.ACTION_MOVE:
if ( currentMovingObjectId > 0) {
drawAtX = X - offsetX;
drawAtY = Y - offsetY;
if(drawAtX < 0 || (drawAtX + movingObjects.get(currentMovingObjectId-1).getWidth()) > MovingObject.screenWidth)
drawAtX = movingObjects.get(currentMovingObjectId-1).getX();
if(drawAtY < 0 || (drawAtY + movingObjects.get(currentMovingObjectId-1).getHeight()) > MovingObject.screenHeight)
drawAtY = movingObjects.get(currentMovingObjectId-1).getY();
if(drawAtX != movingObjects.get(currentMovingObjectId-1).getX() || drawAtY != movingObjects.get(currentMovingObjectId-1).getY()){
movingObjects.get(currentMovingObjectId-1).setX(drawAtX);
movingObjects.get(currentMovingObjectId-1).setY(drawAtY);
redraw = true;
}
}
break;
case MotionEvent.ACTION_UP:
break;
}
if(redraw)
invalidate();
return true;
}
The above code describes the onTouch and onDraw method in my custom view class and i have to prevent image cross the line.As we can implement using matrix where we can move the object horizontal and vertical avoiding the object by setting the wall on 4 sides of cell but i have to move object freely inside a puzzle.Any logic would be helpful i am stuck.Thanks in advance
Related
I want to put marker on image with below functionality
-Dynamically adding multiple markers on image with zoom and scroll functionality on image.
-Blink animation on marker after added on image.
-Marker can be click-able and drag & drop on image.
-Marker's x & y position on image to sync same with other devices
I have used https://github.com/davemorrissey/subsampling-scale-image-view library
Java Code :
public class PinMarkerView extends SubsamplingScaleImageView implements View.OnTouchListener {
private PointF sPin;
private Bitmap pin;
private int resId;
public float vX;
public float vY;
private PointF vPrevious;
private PointF vStart;
private boolean drawing = false;
private boolean markerTouch = false;
private int strokeWidth;
private ArrayList<MapPins> allPins = new ArrayList<MapPins>();
private ArrayList<DrawPins> drawnPins = new ArrayList<DrawPins>();
public PinView(Context context) {
this(context, null);
}
public PinView(Context context, AttributeSet attr) {
super(context, attr);
// initialise();
}
public void setPin(PointF sPin, #DrawableRes int resid, String extras) {
MapPins mapPins = new MapPins();
mapPins.setPointF(sPin);
mapPins.setResId(resid);
mapPins.setExtrasData(extras);
mapPins.setX(sPin.x);
mapPins.setY(sPin.y);
allPins.add(mapPins);
//this.sPin = sPin;
// this.resId = resid;
initialise();
invalidate();
}
public PointF getPin() {
return sPin;
}
public Bitmap getMarker() {
return pin;
}
private void initialise() {
setOnTouchListener(this);
float density = getResources().getDisplayMetrics().densityDpi;
strokeWidth = (int) (density / 60f);
for (int i = 0; i < allPins.size(); i++) {
MapPins mapPins = allPins.get(i);
Bitmap localpin = BitmapFactory.decodeResource(this.getResources(), mapPins.getResId());
float w = (density / 100f) * localpin.getWidth();
float h = (density / 100f) * localpin.getHeight();
//pin = Bitmap.createScaledBitmap(pin, (int) w, (int) h, true);
pin = Bitmap.createScaledBitmap(localpin, localpin.getWidth(), localpin.getHeight(), true);
mapPins.setCreatedBitmap(pin);
allPins.set(i, mapPins);
//-------
}
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Don't draw pin before image is ready so it doesn't move around during setup.
if (!isReady()) {
return;
}
Paint paint = new Paint();
paint.setAntiAlias(true);
for (int i = 0; i < allPins.size(); i++) {
MapPins mapPins = allPins.get(i);
if (mapPins.getPointF() != null && mapPins.getCreatedBitmap() != null) {
PointF vPin = sourceToViewCoord(mapPins.getPointF());
vX = vPin.x - (mapPins.getCreatedBitmap().getWidth() / 2);
vY = vPin.y - mapPins.getCreatedBitmap().getHeight();
canvas.drawBitmap(mapPins.getCreatedBitmap(), vX, vY, paint);
}
}
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
setMaxScale(5f);
return false;
}
#Override
public boolean onTouchEvent(#NonNull MotionEvent event) {
/* if (isZoomEnabled()) {
return super.onTouchEvent(event);
}*/
boolean consumed = false;
int touchCount = event.getPointerCount();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_1_DOWN:
vStart = new PointF(event.getX(), event.getY());
vPrevious = new PointF(event.getX(), event.getY());
getPinIdByPoint(vStart, viewToSourceCoord(event.getX(), event.getY()));
handleActionDown((int) event.getX(), (int) event.getY());
break;
case MotionEvent.ACTION_POINTER_2_DOWN:
// Abort any current drawing, user is zooming
vStart = null;
vPrevious = null;
break;
case MotionEvent.ACTION_MOVE:
if (markerTouch) {
setPanEnabled(false);
PointF sCurrentF = viewToSourceCoord(event.getX(), event.getY());
PointF sCurrent = new PointF(sCurrentF.x, sCurrentF.y);
PointF sStart = vStart == null ? null : new PointF(viewToSourceCoord(vStart).x, viewToSourceCoord(vStart).y);
if (touchCount == 1 && vStart != null) {
float vDX = Math.abs(event.getX() - vPrevious.x);
float vDY = Math.abs(event.getY() - vPrevious.y);
if (vDX >= strokeWidth * 5 || vDY >= strokeWidth * 5) {
if (sPin == null) {
sPin = sStart;
}
sPin = sCurrent;
vPrevious.x = event.getX();
vPrevious.y = event.getY();
drawing = true;
}
consumed = true;
invalidate();
} else if (touchCount == 1) {
// Consume all one touch drags to prevent odd panning effects handled by the superclass.
consumed = true;
}
} else {
return super.onTouchEvent(event);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
invalidate();
setPanEnabled(true);
drawing = false;
markerTouch = false;
vPrevious = null;
vStart = null;
}
// Use parent to handle pinch and two-finger pan.
return consumed || super.onTouchEvent(event);
}
public void handleActionDown(int eventX, int eventY) {
if (eventX >= (sPin.x - pin.getWidth()) && (eventX <= (sPin.x + pin.getWidth()))) {
if (eventY >= (sPin.y - pin.getHeight()) && (sPin.y <= (sPin.y + pin.getHeight()))) {
markerTouch = true;
} else {
markerTouch = false;
}
} else {
markerTouch = false;
}
}
public int getPinIdByPoint(PointF tappedCoordinate, PointF deeplinkCoordinate) {
for (int i = allPins.size() - 1; i >= 0; i--) {
MapPins dPin = allPins.get(i);
int blockWidth = dPin.getCreatedBitmap().getWidth();
int blockHeight = dPin.getCreatedBitmap().getHeight();
int deeplinkX = (int) (deeplinkCoordinate.x - (dPin.getCreatedBitmap().getWidth() / 2));
int deeplinkY = (int) (deeplinkCoordinate.y - dPin.getCreatedBitmap().getHeight());
// center coordinate -/+ blockWidth actually sets touchable area to 2x icon size
if (tappedCoordinate.x >= deeplinkX - blockWidth && tappedCoordinate.x <= deeplinkX + blockWidth &&
tappedCoordinate.y >= deeplinkY - blockHeight && tappedCoordinate.y <= deeplinkY + blockHeight) {
sPin = dPin.getPointF();
pin = dPin.getCreatedBitmap();
return dPin.getId();
}
}
return -1; //negative no means no pin selected
}
}
Currently, I am able to add multiple marker, but I am facing issue of marker click and dragging on image.
in my game ball moves from top to bottom and bottom to top,my requirement is when ball touches top edge or bottom edge i want to print a toast message like "game over".later i want to move control some other intent.to animate the ball i am using following code
public class AnimatedView extends ImageView{
static int count=0;
private Context mContext;
int x = 130;
int y = 450;
private float a,b;
private int xVelocity = 25;
private int yVelocity = 20;
private Handler h;
private final int FRAME_RATE = 30;
BitmapDrawable ball;
boolean touching;
boolean dm_touched = false;
float move=3;
int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h;
boolean paused;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
#Override
public void buildDrawingCache() {
// TODO Auto-generated method stub
super.buildDrawingCache();
}
private Runnable r = new Runnable() {
#Override
public void run() {
//Log.e("game","run called");
if(touching = true)
invalidate();
}
};
#Override
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
y += yVelocity;
if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||y <0) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
//Log.e("sarat",""+touching);
if(touching){
// Log.e("game","iftouch called called");
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Log.d("game","ontouch called");
int touchType = event.getAction();
switch(touchType){
case MotionEvent.ACTION_MOVE:
a = event.getX();
b = event.getY();
touching = true;
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y))
{
if(bm_h+a>600 ||bm_h+b<0){
Toast.makeText(getContext(),"game over",Toast.LENGTH_LONG).show();
}
break;
case MotionEvent.ACTION_DOWN:
//x and y give you your touch coordinates
a = event.getX();
b = event.getY();
touching = true;
Log.e("s",""+ a);
Log.e("s",""+ b);
Log.e("s",""+ x);
Log.e("s",""+ y);
Log.e("s",""+ bm_w);
Log.e("s",""+ bm_h);
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
count++;
MainActivity.setCount(count);
Log.i("score",""+count);
}
if (dm_touched) {
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
move+=2;
y = yVelocity*-1;
}}
case MotionEvent.ACTION_UP:
a = event.getX();
b = event.getY();
if(a>x+20&&a<330&&b<=y+320&&b>y){
invalidate();}
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
Log.e("game","clicked");
}
default:
dm_touched = true;
}
return true;
}
#Override
public void destroyDrawingCache() {
count=0;
super.destroyDrawingCache();
}
}
i have image it moves all directions on the canvas
my requirement is when i click on moving image it change the direction to the top but i unable to do this,below i will drop total code,please help me
animatedview.java:
public class AnimatedView extends ImageView{
int c=0;
static int count=0;
private Context mContext;
int x = 150;
int y = 450;
private float a,b;
private int xVelocity = 20;
private int yVelocity = 20;
private Handler h;
private final int FRAME_RATE = 25;
BitmapDrawable ball;
boolean touching;
boolean dm_touched = false;
float move=3;
int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
//Log.e("bharat","run called");
if(touching = true)
invalidate();
}
};
#Override
protected void onDraw(Canvas c) {
//Log.e("bharat","ondraw called");
//int z= c.getHeight()/2;
//Log.e("bharat","z is"+z);
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
//x = this.getWidth()/2;
y = c.getHeight()/2;
} else {
x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity*-1;
}
if (y >( this.getHeight() - ball.getBitmap().getHeight()) ||y <0) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
//Log.e("sarat",""+touching);
if(touching){
// Log.e("bharat","iftouch called called");
h.postDelayed(r, FRAME_RATE);
bm_w=ball.getBitmap().getWidth();
bm_h=ball.getBitmap().getHeight();
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// Log.d("bharat","ontouch called");
int touchType = event.getAction();
switch(touchType){
case MotionEvent.ACTION_MOVE:
a = event.getX();
b = event.getY();
touching = true;
/* if (dm_touched) {
x = (int) a - bm_offsetx;
y = (int) b - bm_offsety;
}*/
//invalidate();
break;
case MotionEvent.ACTION_DOWN:
//x and y give you your touch coordinates
a = event.getX();
b = event.getY();
touching = true;
//Log.d("bharat","action_down called");
Log.e("s",""+ a);
Log.e("s",""+ b);
Log.e("s",""+ x);
Log.e("s",""+ y);
Log.e("s",""+ bm_w);
Log.e("s",""+ bm_h);
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
count++;
MainActivity.setCount(count);
invalidate();
Log.i("bharat",""+count);
/* if(b>450)
{
// Toast.makeText(getContext(),"game over",Toast.LENGTH_LONG).show();
}*/
}
if (dm_touched) {
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
move+=2;
//x = (int) a - bm_offsetx;
y = (int)b-300;
}}
// dm_touched = true;
case MotionEvent.ACTION_UP:c++;
a = event.getX();
b = event.getY();
if(a>x+20&&a<330&&b<=y+320&&b>y){
invalidate();}
if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) {
Log.e("bharat","clicked");
}
default:
dm_touched = true;
//touching = true;
}
return true;
}
#Override
public void destroyDrawingCache() {
count=0;
super.destroyDrawingCache();
}
}
I am working on a Compass type Android application, where I would have an image of a Compass, with letters indicating various directions ('N' for North, 'E' for East and so on). The user could press any of the letters and get some info about a given direction. I am looking for a way how to bind the touch coordinates to the image, i.e. if a user press a letter 'N' they would always get similar coordinates irrelevant to which direction handset is facing. Below is the code I am using now.
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.compass_fragment, container, false);
compassView = (CompassView) v.findViewById(R.id.compass_view);
compassView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motion) {
Log.i(TAG, "In onTouch");
// Get the action that was done on this touch event
switch (motion.getAction())
{
case MotionEvent.ACTION_DOWN:
{
return true;
}
case MotionEvent.ACTION_UP:
{
float x = motion.getX();
float y = motion.getY();
Log.i(TAG, "ACTION UP x = " + x + " y = " + y);
break;
}
}
// if you return false, these actions will not be recorded
return true;
}
});
mSensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
return(v);
}
I worked it out the answer to my question. It works the way I wanted, without using the buttons. I attached below the code, hope it might be useful for someone else.
To get the touched direction on the compass I calculate the offset angle between the current compass bearing (degrees variable which updated by setDegrees method called from another class) and the angle of the touch location in relation to the Y axis.
public class CompassView extends ImageView {
private float degrees=0;
private String touchDirection;
public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
int height = this.getHeight();
int width = this.getWidth();
canvas.rotate(360-degrees, width/2, height/2);
super.onDraw(canvas);
}
public void setDegrees(float degrees) {
this.degrees = degrees;
this.invalidate();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
return true;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
float eventX = event.getX();
float eventY = event.getY();
touchDirection = getTouchDirection(eventX, eventY);
Context context = getContext();
Intent intent = new Intent(context, CompassDirectionInfo.class);
Bundle bundle = new Bundle();
bundle.putString("DIRECTION", touchDirection);
intent.putExtras(bundle);
context.startActivity(intent);
break;
default:
return false;
}
return true;
}
private String getTouchDirection (float eventX, float eventY) {
String direction = "";
float centreX = getWidth()/2, centreY = getHeight()/2;
float tx = (float) (eventX - centreX), ty = (float) (eventY - centreY);
float radius = (float) Math.sqrt(tx*tx + ty*ty);
float offsetX = 0, offsetY = radius, adjEventX = eventX - centreX, adjEventY = centreY - eventY;
double cosaU = ((offsetX * adjEventX) + (offsetY * adjEventY));
double cosaD = ( Math.sqrt((offsetX * offsetX) + (offsetY * offsetY)) * Math.sqrt((adjEventX * adjEventX) + (adjEventY * adjEventY)));
double cosa = cosaU / cosaD;
double degr = ( Math.acos(cosa) * (180 / Math.PI));
if (adjEventX < 0)
degr = 360 - degr;
float offsetDegrees = (float) (degrees + degr);
if (offsetDegrees > 360)
offsetDegrees = offsetDegrees - 360;
if (offsetDegrees < 22.5 || offsetDegrees > 336.5)
direction = "NORTH";
else if (offsetDegrees > 22.5 && offsetDegrees < 67.5)
direction = "NORTHEAST";
else if (offsetDegrees > 67.5 && offsetDegrees < 112.5)
direction = "EAST";
else if (offsetDegrees > 112.5 && offsetDegrees < 156.5)
direction = "SOUTHEAST";
else if (offsetDegrees > 156.5 && offsetDegrees < 201.5)
direction = "SOUTH";
else if (offsetDegrees > 201.5 && offsetDegrees < 246.5)
direction = "SOUTHWEST";
else if (offsetDegrees > 246.5 && offsetDegrees < 291.5)
direction = "WEST";
else if (offsetDegrees > 291.5 && offsetDegrees < 336.5)
direction = "NORTHWEST";
return direction;
}
}
I am trying to rotate image single round from it's center point but I am not able to stop at desire position as I can do rotation but I want to stop rotation after 360'(1 round).
public class RotateRoundActivity extends Activity implements OnTouchListener
{
private ImageView dialer;
//private float y=0;
private float x=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialer = (ImageView) findViewById(R.id.big_button);
dialer.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
int rotation=(int)Math.toDegrees(r);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
x=event.getX();
// y=event.getY();
updateRotation(rotation);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}
Rotation method#
private void updateRotation(double rot){
float newRot=new Float(rot);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Matrix matrix=new Matrix();
matrix.postRotate(newRot,bitmap.getWidth(),bitmap.getHeight());
Log.i("demo===>", "matrix==>" + matrix);
// Log.i("demo===", "y===>" + y);
Log.i("demo===", "x===>" + x);
if(x>250){
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
}
else{
Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
dialer.setImageBitmap(reDrawnBitmap);
}
}
}
Your suggestions are appreciable.
You have to save previous rot value. And add check in updateRotation method if previousRot is at the left of 360' degrees and rot is at the right of 360' degrees then we made 1 round and need stop rotating.
Sample code for clockwise case
if (previousRot >= 300 && previousRot <= 360 && rot >= 0 && rot <= 60) {
rot = 359.99; // or here can be 360'
}
For counter clockwise case it is almost the same, but the values swapped
if (previousRot >= 0 && previousRot <= 60 && rot >= 300 && rot <= 360) {
rot = 0;
}
This code will stop rotation. From the beginning previousRot should be 0 for clockwise case and 359.99 for counter clockwise
Another approach is to add one more variable to store total traveled angle. From the beginning traveledAngle have to be equal to 0. And if you're rotating in clockwise direction you have to increase it by the difference between rot and previousRot. When rotating counter clockwise decrease it by the same value.
traveledAngle += rot - previousRot;
When traveledAngle becomes greater than 360' you need to stop rotating in clockwise direction, and when it becomes less than 0, you need to stop rotating in counter clockwise direction.
I have used your demo and added some logic, the newer demo is as below:
public class RotateRoundActivity extends Activity implements OnTouchListener {
float rot1=0.0F, rot2=0.0F;
boolean clockwise, rotationDone = false, halfrotated = false;
int rotcall=0;
private ImageView dialer;
//private float y=0;
private int x=0;
//private int y=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialer = (ImageView) findViewById(R.id.big_button);
dialer.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY());
int rotation=(int)Math.toDegrees(r);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
x=(int) event.getX();
//y=(int) event.getY();
updateRotation(rotation);
break;
case MotionEvent.ACTION_UP:
break;
}//switch
return true;
}
private void updateRotation(double rot){
float newRot = new Float(rot);
rotcall++;
if(rotcall == 1)
rot1 = new Float(rot);
if(rotcall == 2)
rot2 = new Float(rot);
if(rot1 != 0.0F && rot2 != 0.0F)
if(rot1 < rot2)
clockwise = true;
else
clockwise = false;
System.out.println("Rotate :: "+newRot);
if(clockwise && rot1>=0 ) {
if(newRot < 0)
halfrotated = true;
if(halfrotated && newRot > 0)
rotationDone = true;
if(rotationDone)
newRot = 0;
}
if(clockwise && rot1<0) {
if(newRot > 0)
halfrotated = true;
if(halfrotated && newRot < 0)
rotationDone = true;
if(rotationDone)
newRot = 0;
}
if(!clockwise && rot1<0) {
if(newRot > 0)
halfrotated = true;
if(halfrotated && newRot < 0)
rotationDone = true;
if(rotationDone)
newRot = 0;
}
if(!clockwise && rot1>=0) {
if(newRot < 0)
halfrotated = true;
if(halfrotated && newRot > 0)
rotationDone = true;
if(rotationDone)
newRot = 0;
}
System.out.println("Rotation Done :: "+rotationDone);
if(!rotationDone) {
//BitmapDrawable bitmapDrawable = (BitmapDrawable) dialer.getDrawable();
//Bitmap bitmap = bitmapDrawable.getBitmap();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable. YOUR_DRBL );
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(newRot, width, height);
System.out.println("x===>" + x);
//System.out.println("y===>" + y);
//if (x > 250) {
Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
dialer.setImageBitmap(reDrawnBitmap);
/*} else {
Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0,
width, height, matrix, true);
dialer.setImageBitmap(reDrawnBitmap);
}*/
}
}
}