Android自定義View實(shí)現(xiàn)葉子飄動(dòng)旋轉(zhuǎn)效果(四)
上一篇實(shí)現(xiàn)了葉子飄動(dòng)功能,《Android自定義葉子飄動(dòng)》 現(xiàn)在實(shí)現(xiàn)旋轉(zhuǎn)效果
要實(shí)現(xiàn)這個(gè)效果,要在之前的功能上添加2個(gè)功能
1、通過(guò)matrix.postTranslate(int x, int y)在添加在Y軸上滑動(dòng)
2、通過(guò)matrix.postRotate(float degrees, float px, float py)實(shí)現(xiàn)葉子旋轉(zhuǎn)
代碼實(shí)現(xiàn)
1、獲取Y坐標(biāo)
private float getMatrixY() { float w = (float) ((float) 2 * Math.PI / width); int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2; return y; }
math.sin(Math.PI....)的取值范圍貌似是-1~1之間。通過(guò)X坐標(biāo)在整個(gè)width的比例,獲取到Y(jié)坐標(biāo)的比例。
這里方法有很多,我這邊葉子Y坐標(biāo)默認(rèn)在Y軸中間,然后上下+-18px實(shí)現(xiàn)在Y軸的滑動(dòng),18滑動(dòng)浮動(dòng)比較大了
public LeafView(Context context, AttributeSet attrs) { super(context, attrs); mResources = getResources(); bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap(); leafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null))).getBitmap(); mLeafHeight = leafBitmap.getWidht(); bgPaint = new Paint(); bgPaint.setColor(mResources.getColor(R.color.bg_color)); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; bgDestRect = new Rect(0, 0 , width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); bgRect = new RectF(0, 0 , width, height); //添加黃色背景 canvas.drawRect(bgRect, bgPaint); //添加背景圖片 canvas.drawBitmap(bgBitmap, null, bgDestRect, null); //添加葉子 Matrix matrix = new Matrix(); matrix.postTranslate(getMatriX(), getMatrixY); canvas.drawBitmap(leafBitmap, new Matrix(), new Paint()); //重復(fù)調(diào)用onDraw() postInvalidate(); } long cycleTime = 5000; //葉子滑動(dòng)一周的時(shí)間5秒 long startTime = 0; //葉子滑動(dòng)開(kāi)始時(shí)間 private float getMatriX() { float betweenTime = startTime - System.currentTimeMillis(); //周期結(jié)束再加一個(gè)cycleTime if(betweenTime < 0) { startTime = System.currentTimeMillis() + cycleTime; betweenTime = cycleTime; } //通過(guò)時(shí)間差計(jì)算出葉子的坐標(biāo) float fraction = (float) betweenTime / cycleTime; float x = (int)(width * fraction); return x; } private float getMatrixY() { float w = (float) ((float) 2 * Math.PI / width); int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2; return y; }
看下效果就這樣,在Y軸上實(shí)現(xiàn)上下浮動(dòng),如果要浮動(dòng)小點(diǎn),可以把18改小
2、實(shí)現(xiàn)旋轉(zhuǎn)
主要通過(guò)matrix.postRotate(float degrees, float px, float py)
degrees就是角度(0~360),px,py就是圖片的中心點(diǎn)
private int getRotate() { float scale = ((startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime; int rotate = (int)(scale * 360); return rotate; }
同樣,通過(guò)當(dāng)前葉子在X軸的比例,來(lái)計(jì)算出旋轉(zhuǎn)的角度(0~360)
完整代碼:
public class LeafView extends View { private Resources mResources; private Bitmap mLeafBitmap, bgBitmap; private int width, height; private int mLeafWidth,mLeafHeight; private Paint bgPaint; private RectF bgRect; private Rect bgDestRect; public LeafView(Context context, AttributeSet attrs) { super(context, attrs); mResources = getResources(); mLeafBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf, null)).getBitmap(); mLeafWidth = mLeafBitmap.getWidht(); mLeafHeight = mLeafBitmap.getHeight(); bgBitmap = ((BitmapDrawable) mResources.getDrawable(R.drawable.leaf_kuang, null)).getBitmap(); bgPaint = new Paint(); bgPaint.setColor(mResources.getColor(R.color.bg_color)); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); width = w; height = h; bgDestRect = new Rect(0, 0 , width, height); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); bgRect = new RectF(0, 0 , width, height); //添加黃色白金 canvas.drawRect(bgRect, bgPaint); //添加背景圖片 canvas.drawBitmap(bgBitmap, null, bgDestRect, null); canvas.save(); Matrix matrix = new Matrix(); //添加滑動(dòng) matrix.postTranslate(getMatrixX(), getMatrixY()); //添加旋轉(zhuǎn) matrix.postRotate(getRotate(), getMatrixX() + mLeafWidth / 2, getMatrixY() + mLeafHeight / 2); canvas.drawBitmap(mLeafBitmap, matrix, new Paint()); canvas.restore(); postInvalidate(); } long cycleTime = 5000; //葉子滑動(dòng)一周的時(shí)間5秒 long startTime = 0; private float getMatrixX() { float betweenTime = startTime - System.currentTimeMillis(); //周期結(jié)束再加一個(gè)cycleTime if(betweenTime < 0) { startTime = System.currentTimeMillis() + cycleTime; betweenTime = cycleTime; } //通過(guò)時(shí)間差計(jì)算出葉子的坐標(biāo) float fraction = (float) betweenTime / cycleTime; float x = (int)(width * fraction); return x; } private float getMatrixY() { float w = (float) ((float) 2 * Math.PI / width); int y = (int) (18 * Math.sin(w * getMatrixX())) + (height-mLeafHeight)/2; return y; } private int getRotate() { float scale = ((startTime - System.currentTimeMillis())%cycleTime)/ (float)cycleTime; int rotate = (int)(scale * 360); return rotate; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義View實(shí)現(xiàn)QQ運(yùn)動(dòng)積分轉(zhuǎn)盤(pán)抽獎(jiǎng)功能
- Android 自定View實(shí)現(xiàn)仿QQ運(yùn)動(dòng)步數(shù)圓弧及動(dòng)畫(huà)效果
- Android自定義View仿微博運(yùn)動(dòng)積分動(dòng)畫(huà)效果
- Android UI之ImageView實(shí)現(xiàn)圖片旋轉(zhuǎn)和縮放
- Android使用RotateImageView 旋轉(zhuǎn)ImageView
- Android UI設(shè)計(jì)系列之ImageView實(shí)現(xiàn)ProgressBar旋轉(zhuǎn)效果(1)
- Android自定義View葉子旋轉(zhuǎn)完整版(六)
- Android自定義View實(shí)現(xiàn)QQ音樂(lè)中圓形旋轉(zhuǎn)碟子
- Android中imageView圖片放大縮小及旋轉(zhuǎn)功能示例代碼
- Android自定義View圖片按Path運(yùn)動(dòng)和旋轉(zhuǎn)
相關(guān)文章
OpenGL ES正交投影實(shí)現(xiàn)方法(三)
這篇文章主要為大家詳細(xì)介紹了OpenGL ES正交投影的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android點(diǎn)擊EditText文本框之外任何地方隱藏鍵盤(pán)的解決辦法
這篇文章主要介紹了Android點(diǎn)擊EditText文本框之外任何地方隱藏鍵盤(pán)的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android開(kāi)發(fā)之選項(xiàng)卡功能的實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android開(kāi)發(fā)之選項(xiàng)卡功能的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android選項(xiàng)卡功能的原理、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06Android開(kāi)發(fā)實(shí)現(xiàn)加載網(wǎng)絡(luò)圖片并下載至本地SdCard的方法
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)加載網(wǎng)絡(luò)圖片并下載至本地SdCard的方法,涉及Android圖片文件的讀取、保存及權(quán)限相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android Studio 創(chuàng)建自定義控件的方法
這篇文章主要介紹了Android Studio 創(chuàng)建自定義控件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Android編程實(shí)現(xiàn)自定義輸入法功能示例【輸入密碼時(shí)防止第三方竊取】
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義輸入法功能,可實(shí)習(xí)輸入密碼時(shí)防止第三方竊取的效果,結(jié)合實(shí)例形式詳細(xì)分析了Android布局、控件及輸入法相關(guān)操作技巧,需要的朋友可以參考下2017-01-01Android實(shí)現(xiàn)萬(wàn)能自定義陰影控件實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)萬(wàn)能自定義陰影控件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08