简体   繁体   English

如何在Windows Phone上实现Android上的倾斜效果?

[英]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. 我希望在Android操作系统上点击按钮时实现倾斜效果。

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? 这在Android上很容易实现吗?

在此输入图像描述

A simple way would be to use canvas draws to draw 4 sided shapes. 一种简单的方法是使用画布绘制来绘制4面形状。

Consider each 4 corners. 考虑每个角落。 The "untouched" rectangle would be full size the touched rectangle would be smaller. “未触摸”的矩形将是全尺寸,触摸的矩形将更小。

触摸和未触动过的盒子

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. 如果触摸左上角,该角将使用100%的触摸坐标,其他三个角将全部使用未触摸的坐标。

触及左上角

If you touched the top middle, you would get a shape like this: 如果你碰到了顶部中间,你会得到这样的形状:

触及顶部中间

We can calculate the corners for any touch spot, by calculating how far from the corner your touch is 我们可以通过计算触摸角落的距离来计算任何触摸点的角点

触及左下角

    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: 所以使用这些权重,您可以计算该角落的x和y位置:

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

Then do similarly for the other 3 corners. 然后对其他3个角进行类似的操作。

I've created a first draft here : https://github.com/flavienlaurent/TiltEffect 我在这里创建了初稿: https//github.com/flavienlaurent/TiltEffect

In a second step, I will make it usable with Button etc. 第二步,我将使用Button等。

Unfortunatly, I didn't use the very good (but too mathematical for me) answer of HalR 不幸的是,我没有使用HalR的非常好(但对我来说太数学)的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM