android自定義view實(shí)現(xiàn)圓周運(yùn)動
本文實(shí)例為大家分享了android自定義view實(shí)現(xiàn)圓周運(yùn)動的具體代碼,供大家參考,具體內(nèi)容如下
思想
自定義Animation,自己定義半徑,相當(dāng)于原來控件的位置為(0,0),按照每個(gè)角度區(qū)間,計(jì)算新的位置,跟著時(shí)間變動
逆時(shí)針轉(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,表示時(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);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案3. 告別AJAX實(shí)現(xiàn)無刷新提交表單4. 使用純HTML的通用數(shù)據(jù)管理和服務(wù)5. CSS hack用法案例詳解6. css代碼優(yōu)化的12個(gè)技巧7. css進(jìn)階學(xué)習(xí) 選擇符8. HTML DOM setInterval和clearInterval方法案例詳解9. 使用css實(shí)現(xiàn)全兼容tooltip提示框10. Vue+elementUI下拉框自定義顏色選擇器方式
