android自定義view實(shí)現(xiàn)圓周運(yùn)動
本文實(shí)例為大家分享了android自定義view實(shí)現(xiàn)圓周運(yùn)動的具體代碼,供大家參考,具體內(nèi)容如下

思想
自定義Animation,自己定義半徑,相當(dāng)于原來控件的位置為(0,0),按照每個角度區(qū)間,計算新的位置,跟著時間變動

逆時針轉(zhuǎn)動
public class VenusCircleAnimation extends Animation {
private int radii;
public VenusCircleAnimation(int radii) {
this.radii = radii;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
//根據(jù)取值范圍 確定圓周運(yùn)動的角度范圍。360-0
float d = 360 * interpolatedTime;//interpolatedTime 取值范圍 0-1,表示時間
if (d > 360) { //算法二
d = d-360;
}
int[] ps = getNewLocation((int) d, radii);//
t.getMatrix().setTranslate(ps[0], ps[1]);
}
public int[] getNewLocation(int newAngle, int r) {
int newAngle1;
int newX = 0, newY = 0;
if (newAngle >= 0 && newAngle <= 90) {
// Math.PI/180得到的結(jié)果就是1°,然后再乘以角度得到角度
newX = (int) ( - (r * Math.cos(newAngle * Math.PI / 180)));
newY = (int) (r * Math.sin(newAngle * Math.PI / 180));
} else if (newAngle >= 90 && newAngle <= 180) {// 90-180
newAngle1 = 180 - newAngle;
newX = (int) (r * Math.cos(newAngle1 * Math.PI / 180));
newY = (int) (r * Math.sin(newAngle1 * Math.PI / 180));
} else if (newAngle >= 180 && newAngle <= 270) {//180-270
newAngle1 = 270 - newAngle;
newX = (int) (r * Math.sin(newAngle1 * Math.PI / 180));
newY = (int) ( - (r * Math.cos(newAngle1 * Math.PI / 180)));
} else if (newAngle >= 270) {//270-360
newAngle1 = 360 - newAngle;
newX = (int) ( - (r * Math.cos(newAngle1 * Math.PI / 180)));
newY = (int) ( - (r * Math.sin(newAngle1 * Math.PI / 180)));
}
return new int[]{newX, newY};
}
}
順時針
public class CircleAnimation extends Animation {
private int radii;
public CircleAnimation(int radii) {
this.radii = radii;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float d = 360 * interpolatedTime ;
if (d > 360) {
d = d - 360;
}
int[] ps = getNewLocation((int) d, radii);//
t.getMatrix().setTranslate(ps[0], ps[1]);
}
public int[] getNewLocation(int newAngle, int r) {
int newAngle1;
int newX = 0, newY = 0;
if (newAngle >= 0 && newAngle <= 90) {
newX = (int) (r * Math.sin(newAngle * Math.PI / 180));
newY = (int) ( - (r * Math.cos(newAngle * Math.PI / 180)));
} else if (newAngle >= 90 && newAngle <= 180) {// 90-180
newAngle1 = 180 - newAngle;
newX = (int) (r * Math.sin(newAngle1 * Math.PI / 180));
newY = (int) (r * Math.cos(newAngle1 * Math.PI / 180));
} else if (newAngle >= 180 && newAngle <= 270) {//180-270
newAngle1 = 270 - newAngle;
newX = (int) ( - (r * Math.cos(newAngle1 * Math.PI / 180)));
newY = (int) (r * Math.sin(newAngle1 * Math.PI / 180));
} else if (newAngle >= 270 && newAngle <= 360) {//270-360
newAngle1 = 360 - newAngle;
newX = (int) ( - (r * Math.sin(newAngle1 * Math.PI / 180)));
newY = (int) ( - (r * Math.cos(newAngle1 * Math.PI / 180)));
}
return new int[]{newX, newY};
}
}
使用
CircleAnimation animationw = new CircleAnimation(m); animationw.setDuration(d); animationw.setRepeatCount(-1); animationw.setInterpolator(new LinearInterpolator()); imageView.startAnimation(animationw);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- canvas雪花效果核心代碼分享
- Canvas實(shí)現(xiàn)動態(tài)的雪花效果
- jquery實(shí)現(xiàn)漫天雪花飛舞的圣誕祝福雪花效果代碼分享
- Android自定義view實(shí)現(xiàn)輸入框效果
- Android自定義View實(shí)現(xiàn)雪花特效
- Android自定義view之太極圖的實(shí)現(xiàn)教程
- Android自定義View實(shí)現(xiàn)分段選擇按鈕的實(shí)現(xiàn)代碼
- Android自定義View圓形圖片控件代碼詳解
- Android自定義View實(shí)現(xiàn)跟隨手指移動的小兔子
- Android自定義view實(shí)現(xiàn)倒計時控件
- Android如何用自定義View實(shí)現(xiàn)雪花效果
相關(guān)文章
Android實(shí)現(xiàn)App中導(dǎo)航Tab欄懸浮的功能
相信大家在玩手機(jī)的過程中應(yīng)該會注意到很多的app都有這種功能,比如說外賣達(dá)人常用的“餓了么”。所以這篇文章給大家分享了Android如何實(shí)現(xiàn)app中的導(dǎo)航Tab欄懸浮的功能,有需要的朋友們可以參考借鑒。2016-10-10
Android向Excel寫入數(shù)據(jù)導(dǎo)出U盤并發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了Android將數(shù)據(jù)寫入Excel格式導(dǎo)出U盤、發(fā)送郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Android車載多媒體開發(fā)MediaSession框架示例詳解
這篇文章主要為大家介紹了Android車載多媒體開發(fā)MediaSession框架示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android?Jetpack庫剖析之LiveData組件篇
LiveData是Jetpack組件的一部分,更多的時候是搭配ViewModel來使用,相對于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動生命周期狀態(tài)的時候才會更新數(shù)據(jù)2022-07-07
Flutter懸浮按鈕FloatingActionButton使用詳解
本文主要介紹了Flutter懸浮按鈕FloatingActionButton使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07

