Android自定義View實(shí)現(xiàn)水波紋引導(dǎo)動畫
一、實(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大小變化時(shí)系統(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)平滑曲線,前兩個(gè)參數(shù)為操作點(diǎn)坐標(biāo),后兩個(gè)參數(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對象的引用從隊(duì)列里拿出來,這樣,他就不會執(zhí)行了,但 run 沒有銷毀
removeCallbacks(this);
count += 2;
count %= mInterval;
invalidate();//重繪
}
/**
* 銷毀view時(shí)調(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"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <com.czhappy.showintroduce.view.RippleIntroView android:id="@+id/layout_ripple" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:fitsSystemWindows="true" android:background="#AA000000"> <ImageView android:id="@+id/iv_plus" android:layout_marginTop="36dp" android:src="@mipmap/ic_add" android:layout_alignParentRight="true" android:layout_marginRight="6dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:src="@mipmap/tips_subscribe" android:id="@+id/tv_title" android:layout_below="@id/iv_plus" android:layout_marginTop="50dp" android:layout_alignParentRight="true" android:layout_marginRight="40dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </com.czhappy.showintroduce.view.RippleIntroView> </FrameLayout>
三、源碼下載
http://xiazai.jb51.net/201701/yuanma/AndroidShowIntroduce(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android水波紋載入控件CircleWaterWaveView使用詳解
- android自定義WaveView水波紋控件
- Android自定義View控件實(shí)現(xiàn)多種水波紋漣漪擴(kuò)散效果
- Android自定義WaveProgressView實(shí)現(xiàn)水波紋加載需求
- Android自定義View實(shí)現(xiàn)水波紋效果
- Android 自定義view實(shí)現(xiàn)水波紋動畫效果
- Android自定義View 實(shí)現(xiàn)水波紋動畫引導(dǎo)效果
- Android自定義view實(shí)現(xiàn)水波紋進(jìn)度球效果
- Android項(xiàng)目實(shí)戰(zhàn)手把手教你畫圓形水波紋loadingview
- Android自定義View實(shí)現(xiàn)簡單水波紋效果
相關(guān)文章
Android編程實(shí)現(xiàn)獲取當(dāng)前系統(tǒng)語言及地區(qū)并更改語言的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取當(dāng)前系統(tǒng)語言及地區(qū)并更改語言的方法,涉及Android針對系統(tǒng)語言及地區(qū)的獲取與設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
取消Android Studio項(xiàng)目與SVN關(guān)聯(lián)的方法
今天小編就為大家分享一篇關(guān)于取消Android Studio項(xiàng)目與SVN關(guān)聯(lián)的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android入門之SubMenu的實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)SubMenu子菜單的效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Android有一定的幫助,感興趣的可以了解一下2022-11-11
Android TelephonyManager詳解及實(shí)現(xiàn)代碼
本文主要介紹Android TelephonyManager, 這里整理了關(guān)于Android TelephoneManager的相關(guān)資料,并附有示例代碼和實(shí)現(xiàn)效果圖,有需要的朋友可以參考下2016-08-08
Android?自定義開源庫?EasyView實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android自定義開源庫EasyView實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android應(yīng)用開發(fā):電話監(jiān)聽和錄音代碼示例
這篇文章主要介紹了Android應(yīng)用開發(fā)中電話監(jiān)聽和電話錄音的代碼實(shí)例,同時(shí)附錄了一個(gè)拍照、錄像的例子,需要的朋友可以參考下2014-04-04
Kotlin封裝RecyclerView Adapter實(shí)例教程
這篇文章主要給大家介紹了關(guān)于Kotlin封裝RecyclerView Adapter的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Android開發(fā)技巧之像QQ一樣輸入文字和表情圖像
QQ聊天輸入框,在輸入框中可以同時(shí)輸入文字和表情圖像。實(shí)際上,這種效果在Android SDK中只需要幾行代碼就可以實(shí)現(xiàn),本文將會介紹如何實(shí)現(xiàn)像QQ一樣輸入表情圖像2013-01-01

