Android自定義View實(shí)現(xiàn)波浪動(dòng)畫(huà)
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)波浪動(dòng)畫(huà)的具體代碼,供大家參考,具體內(nèi)容如下
效果演示
代碼調(diào)用與實(shí)現(xiàn)效果
xml中調(diào)用
<developer.shivam.waveview.Wave android:layout_width='match_parent' android:layout_height='match_parent' app:amplitude='100' app:quadrant='0.5' app:speed='0.15'/>
實(shí)現(xiàn)原理
屬性配置
attrs.xml文件中,進(jìn)行屬性配置
<?xml version='1.0' encoding='utf-8'?><resources> <declare-styleable name='Wave'> <!--波浪顏色--> <attr name='waveColor' format='color'/> <!--波浪背景顏色--> <attr name='waveBackgroundColor' format='color'/> <!--波浪速度--> <attr name='speed' format='float'/> <!--正弦曲線相關(guān)--> <!--波浪振幅--> <attr name='amplitude' format='integer'/> <!--波浪相對(duì)于控件的位置--> <attr name='quadrant' format='float'/> <!--波浪的頻率--> <attr name='frequency' format='float'/> </declare-styleable></resources>
獲取屬性,同時(shí)對(duì)屬性賦默認(rèn)值
final TypedArray array = context.obtainStyledAttributes(set, R.styleable.Wave); mSpeed = array.getFloat(R.styleable.Wave_speed, DEFAULT_SPEED); mWaveColor = array.getColor(R.styleable.Wave_waveColor, DEFAULT_WAVE_COLOR); mWaveBKColor = array.getColor(R.styleable.Wave_waveBackgroundColor, DEFAULT_WAVE_BK_COLOR); mAmplitude = array.getInt(R.styleable.Wave_amplitude, DEFAULT_AMPLITUDE); mQuadrant = array.getFloat(R.styleable.Wave_quadrant, DEFAULT_QUADRANT); mFrequency = array.getFloat(R.styleable.Wave_frequency, DEFAULT_FREQUENCY); array.recycle();
繪制波浪
在onDraw()中使用Canvas進(jìn)行繪制即可,這里需要注意的正弦曲線的繪制.
正弦曲線(y=Asin(ωx+φ)+k)的一些參數(shù)如下:
A——振幅,當(dāng)物體作軌跡符合正弦曲線的直線往復(fù)運(yùn)動(dòng)時(shí),其值為行程的1/2。 (ωx+φ)——相位,反映變量y所處的狀態(tài)。 φ——初相,x=0時(shí)的相位;反映在坐標(biāo)系上則為圖像的左右移動(dòng)。 k——偏距,反映在坐標(biāo)系上則為圖像的上移或下移。 ω——角速度, 控制正弦周期(單位角度內(nèi)震動(dòng)的次數(shù))。
onDraw中的代碼:
@Overrideprotected void onDraw(Canvas canvas) { super.onDraw(canvas); final int width = getWidth(); final int height = getHeight(); final int waveHeight = (int) (getHeight() * mQuadrant); // 繪制背景 canvas.drawColor(mWaveBKColor); mWavePath.moveTo(0, height); mWavePath.lineTo(0, waveHeight); for (int i = 1; i <= width; i++) { // 繪制正弦曲線 y = A Sin(ωt+ ρ) = A sin(2πft + ρ) final float y = (float) (waveHeight + mAmplitude * Math.sin(2 * Math.PI * i * mFrequency + mShift)); mWavePath.lineTo(i, y); } // 將曲線閉合 mWavePath.lineTo(width, height); canvas.drawPath(mWavePath, mWavePaint);}
波浪動(dòng)畫(huà)
這時(shí)波浪應(yīng)該已經(jīng)繪制完成了,下面使用Handler中的周期任務(wù)實(shí)現(xiàn)動(dòng)畫(huà)效果.
// 創(chuàng)建一個(gè)周期任務(wù),它的職責(zé)是改變正弦曲線的偏移量 final class WaveAnimation implements Runnable { @Override public void run() { mWavePath.reset(); mShift += mSpeed; invalidate(); Wave.this.postDelayed(this, DEFAULT_PERIOD); } }
在View被創(chuàng)建的時(shí)候讓它進(jìn)行執(zhí)行
// 開(kāi)始波浪動(dòng)畫(huà)postDelayed(new WaveAnimation(), DEFAULT_PERIOD);
完整代碼
public class Wave extends View { // 默認(rèn)屬性值 private static final int DEFAULT_AMPLITUDE = 200; private static final int DEFAULT_PERIOD = 16; private static final float DEFAULT_SPEED = .1F; private static final float DEFAULT_QUADRANT = .33F; private static final float DEFAULT_FREQUENCY = 1F / 360F; private static final int DEFAULT_WAVE_COLOR = Color.parseColor('#64B5F6'); private static final int DEFAULT_WAVE_BK_COLOR = Color.parseColor('#EEEEEE'); @SuppressWarnings('FieldCanBeLocal') @ColorInt private int mWaveColor; @ColorInt private int mWaveBKColor; // 振幅 private int mAmplitude; // 波浪位于View的位置 private float mQuadrant; // 波浪的頻率,這個(gè)值越大,波浪越密集 private float mFrequency; // 速度 private float mSpeed; private float mShift; private final Paint mWavePaint = new Paint(Paint.ANTI_ALIAS_FLAG); private final Path mWavePath = new Path(); public Wave(Context context) { this(context, null); } public Wave(Context context, AttributeSet attrs) { this(context, attrs, 0); } public Wave(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet set) { final TypedArray array = context.obtainStyledAttributes(set, R.styleable.Wave); mSpeed = array.getFloat(R.styleable.Wave_speed, DEFAULT_SPEED); mWaveColor = array.getColor(R.styleable.Wave_waveColor, DEFAULT_WAVE_COLOR); mWaveBKColor = array.getColor(R.styleable.Wave_waveBackgroundColor, DEFAULT_WAVE_BK_COLOR); mAmplitude = array.getInt(R.styleable.Wave_amplitude, DEFAULT_AMPLITUDE); mQuadrant = array.getFloat(R.styleable.Wave_quadrant, DEFAULT_QUADRANT); mFrequency = array.getFloat(R.styleable.Wave_frequency, DEFAULT_FREQUENCY); array.recycle(); mWavePaint.setStrokeWidth(2); mWavePaint.setColor(mWaveColor); // 開(kāi)始波浪動(dòng)畫(huà) postDelayed(new WaveAnimation(), DEFAULT_PERIOD); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final int width = getWidth(); final int height = getHeight(); final int waveHeight = (int) (getHeight() * mQuadrant); // 繪制背景 canvas.drawColor(mWaveBKColor); mWavePath.moveTo(0, height); mWavePath.lineTo(0, waveHeight); for (int i = 1; i <= width; i++) { // 繪制正弦曲線 y = A Sin(ωt+ ρ) = A sin(2πft + ρ) final float y = (float) (waveHeight + mAmplitude * Math.sin(2 * Math.PI * i * mFrequency + mShift)); mWavePath.lineTo(i, y); } // 將曲線閉合 mWavePath.lineTo(width, height); canvas.drawPath(mWavePath, mWavePaint); } final class WaveAnimation implements Runnable { @Override public void run() { mWavePath.reset(); mShift += mSpeed; invalidate(); Wave.this.postDelayed(this, DEFAULT_PERIOD); } }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. chat.asp聊天程序的編寫(xiě)方法2. JSP之表單提交get和post的區(qū)別詳解及實(shí)例3. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法4. PHP循環(huán)與分支知識(shí)點(diǎn)梳理5. Ajax請(qǐng)求超時(shí)與網(wǎng)絡(luò)異常處理圖文詳解6. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能7. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法8. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法9. JavaWeb Servlet中url-pattern的使用10. jsp EL表達(dá)式詳解
