這篇文章將為大家詳細(xì)講解有關(guān)在Android開發(fā)中使用View制作一個引導(dǎo)動畫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián)公司專注于唐山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供唐山營銷型網(wǎng)站建設(shè),唐山網(wǎng)站制作、唐山網(wǎng)頁設(shè)計、唐山網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造唐山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供唐山網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
一、實(shí)現(xiàn)效果圖
關(guān)于貝塞爾曲線
二、實(shí)現(xiàn)代碼
1.自定義view
package com.czhappy.showintroduce.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.View; import android.widget.RelativeLayout; /** * Description: 水波紋動畫引導(dǎo)view * User: chenzheng * Date: 2017/1/14 0014 * Time: 18:01 */ public class RippleIntroView extends RelativeLayout implements Runnable { private int mMaxRadius = 70; private int mInterval = 20; private int count = 0; private Bitmap mCacheBitmap; private Paint mRipplePaint; private Paint mCirclePaint; private Path mArcPath; public RippleIntroView(Context context) { this(context, null); } public RippleIntroView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RippleIntroView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mRipplePaint = new Paint(); mRipplePaint.setAntiAlias(true); mRipplePaint.setStyle(Paint.Style.STROKE); mRipplePaint.setColor(Color.WHITE); mRipplePaint.setStrokeWidth(2.f); mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.setStyle(Paint.Style.FILL); mCirclePaint.setColor(Color.WHITE); mArcPath = new Path(); } /** * view大小變化時系統(tǒng)調(diào)用 * @param w * @param h * @param oldw * @param oldh */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (mCacheBitmap != null) { mCacheBitmap.recycle(); mCacheBitmap = null; } } @Override protected void onDraw(Canvas canvas) { //獲取加號圖片view View mPlusChild = getChildAt(0); //獲取提示圖片view View mRefsChild = getChildAt(1); if (mPlusChild == null || mRefsChild == null) return; //獲取加號圖片大小 final int pw = mPlusChild.getWidth(); final int ph = mPlusChild.getHeight(); //獲取提示圖片大小 final int fw = mRefsChild.getWidth(); final int fh = mRefsChild.getHeight(); if (pw == 0 || ph == 0) return; //加號圖片中心點(diǎn)坐標(biāo) final float px = mPlusChild.getX() + pw / 2; final float py = mPlusChild.getY() + ph / 2; //提示圖片左上角坐標(biāo) final float fx = mRefsChild.getX(); final float fy = mRefsChild.getY(); final int rw = pw / 2; final int rh = ph / 2; if (mCacheBitmap == null) { mCacheBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); Canvas cv = new Canvas(mCacheBitmap); super.onDraw(cv); //清空所有已經(jīng)畫過的path至原始狀態(tài) mArcPath.reset(); //起始輪廓點(diǎn)移至x,y坐標(biāo)點(diǎn),即加號圖片正下方再往下20位置 mArcPath.moveTo(px, py + rh + mInterval); //設(shè)置二次貝塞爾,實(shí)現(xiàn)平滑曲線,前兩個參數(shù)為操作點(diǎn)坐標(biāo),后兩個參數(shù)為結(jié)束點(diǎn)坐標(biāo) mArcPath.quadTo(px, fy - mInterval, fx + fw * 0.618f, fy - mInterval); //0~255,數(shù)值越小越透明 mRipplePaint.setAlpha(255); cv.drawPath(mArcPath, mRipplePaint); //繪制半徑為6的實(shí)心圓點(diǎn) cv.drawCircle(px, py + rh + mInterval, 6, mCirclePaint); } //繪制背景圖片 canvas.drawBitmap(mCacheBitmap, 0, 0, mCirclePaint); //保存畫布當(dāng)前的狀態(tài) int save = canvas.save(); for (int step = count; step <= mMaxRadius; step += mInterval) { //step越大越靠外就越透明 mRipplePaint.setAlpha(255 * (mMaxRadius - step) / mMaxRadius); canvas.drawCircle(px, py, (float) (rw + step), mRipplePaint); } //恢復(fù)Canvas的狀態(tài) canvas.restoreToCount(save); //延遲80毫秒后開始運(yùn)行 postDelayed(this, 80); } @Override public void run() { //把run對象的引用從隊列里拿出來,這樣,他就不會執(zhí)行了,但 run 沒有銷毀 removeCallbacks(this); count += 2; count %= mInterval; invalidate();//重繪 } /** * 銷毀view時調(diào)用,收尾工作 */ @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if (mCacheBitmap != null) { mCacheBitmap.recycle(); mCacheBitmap = null; } } }
2.MainActivity.java
package com.czhappy.showintroduce.activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import com.czhappy.showintroduce.R; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View view = findViewById(R.id.layout_ripple); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ((ViewGroup) v.getParent()).removeView(v); } }); } }
3.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
關(guān)于在Android開發(fā)中使用View制作一個引導(dǎo)動畫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。