Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve tilt effect on Android like on Windows Phone?

I want to achieve a tilt effect when a button is clicked, on Android OS.

Tilt Effect: Not the whole button will be seen as pressed. Only the part that touch event occured should seem to be pressed.

Is this easily possible on Android?

enter image description here

like image 381
frankish Avatar asked Mar 16 '13 14:03

frankish


1 Answers

A simple way would be to use canvas draws to draw 4 sided shapes.

Consider each 4 corners. The "untouched" rectangle would be full size the touched rectangle would be smaller.

Touched and untouched boxes

You just need to draw your four sided shape using a point you calculate for each part of the rectangle. You can get the touch position, then figure out how much "weight" to give each point.

to calculate each corner, you need to figure out how much "weight" to give the touched coordinate, and how much "weight" to give the untouched coordinate. If you touch the top left corner, that corner would use 100% of the touched coordinate, and the other three corners would all use the untouched coordinate.

touched top left corner

If you touched the top middle, you would get a shape like this:

touched top middle

We can calculate the corners for any touch spot, by calculating how far from the corner your touch is

touched bottom left

    float untouchedXWeight1 = Math.abs(xt - x1)/width;
    //maximum of 1, minimum of 0

    float untouchedYWeight1 = Math.abs(yt - y1)/height;

    float untouchedWeight1 = (untouchedXWeight1 + untouchedYWeight1)/2;
    //also maximum of 1, minimum of 0

    float touchedWeight1 = 1 - untouchedWeight1;

so with those weights, you can calculate your x and y positions for that corner:

x1 = xUntouched1 * untouchedWeight + xTouched1 * touchedWeight1;
y1 = yUntouched1 * untouchedWeight + yTouched1 * touchedWeight1;

Then do similarly for the other 3 corners.

like image 100
HalR Avatar answered Oct 12 '22 15:10

HalR