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

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

逆時(shí)針轉(zhuǎn)動(dòng)
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)動(dòng)的角度范圍。360-0
float d = 360 * interpolatedTime;//interpolatedTime 取值范圍 0-1,表示時(shí)間
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};
}
}
順時(shí)針
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);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- canvas雪花效果核心代碼分享
- Canvas實(shí)現(xiàn)動(dòng)態(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)跟隨手指移動(dòng)的小兔子
- Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
- Android如何用自定義View實(shí)現(xiàn)雪花效果
相關(guān)文章
Flutter使用?input?chip?標(biāo)簽組件示例詳解
這篇文章主要為大家介紹了Flutter使用?input?chip?標(biāo)簽組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android內(nèi)存泄漏排查利器LeakCanary
這篇文章主要為大家詳細(xì)介紹了Android內(nèi)存泄漏排查利器LeakCanary的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android LuBan與Compressor圖片壓縮方式
本篇文章主要介紹了Android LuBan與Compressor圖片壓縮方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Flutter手勢(shì)密碼的實(shí)現(xiàn)示例(附demo)
本文主要介紹了Flutter手勢(shì)密碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽流容器FlowLayout
這篇文章主要介紹了Android自定義ViewGroup實(shí)現(xiàn)FlowLayout標(biāo)簽流容器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android源碼學(xué)習(xí)之組合模式定義及應(yīng)用
將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性,需要了解的朋友可以參考下2013-01-01

