Android自定義View實(shí)現(xiàn)水波紋引導(dǎo)動(dòng)畫(huà)
一、實(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òng)畫(huà)引導(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) {
//獲取加號(hào)圖片view
View mPlusChild = getChildAt(0);
//獲取提示圖片view
View mRefsChild = getChildAt(1);
if (mPlusChild == null || mRefsChild == null) return;
//獲取加號(hào)圖片大小
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;
//加號(hào)圖片中心點(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)畫(huà)過(guò)的path至原始狀態(tài)
mArcPath.reset();
//起始輪廓點(diǎn)移至x,y坐標(biāo)點(diǎn),即加號(hào)圖片正下方再往下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);
//保存畫(huà)布當(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毫秒后開(kāi)始運(yùn)行
postDelayed(this, 80);
}
@Override
public void run() {
//把run對(duì)象的引用從隊(duì)列里拿出來(lái),這樣,他就不會(huì)執(zhí)行了,但 run 沒(méi)有銷(xiāo)毀
removeCallbacks(this);
count += 2;
count %= mInterval;
invalidate();//重繪
}
/**
* 銷(xiāo)毀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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(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)水波紋動(dòng)畫(huà)效果
- Android自定義View 實(shí)現(xiàn)水波紋動(dòng)畫(huà)引導(dǎo)效果
- Android自定義view實(shí)現(xiàn)水波紋進(jìn)度球效果
- Android項(xiàng)目實(shí)戰(zhàn)手把手教你畫(huà)圓形水波紋loadingview
- Android自定義View實(shí)現(xiàn)簡(jiǎn)單水波紋效果
相關(guān)文章
Android 彩色Toast的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 彩色Toast的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
SafeList?in?Flutter?and?Dart小技巧
這篇文章主要為大家介紹了SafeList?in?Flutter?and?Dart小技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android自定義控件實(shí)現(xiàn)簡(jiǎn)單滑動(dòng)開(kāi)關(guān)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)簡(jiǎn)單滑動(dòng)開(kāi)關(guān)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android 模仿iPhone列表數(shù)據(jù)View刷新動(dòng)畫(huà)詳解
本文主要介紹Android 模仿iPhone列表數(shù)據(jù)view 刷新動(dòng)畫(huà)的資料,這里整理詳細(xì)的資料,并附示例代碼及實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下2016-09-09
Android應(yīng)用接入微信分享的實(shí)例代碼
本篇文章主要介紹了Android應(yīng)用接入微信分享的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-07-07
Android ActionBar制作時(shí)鐘實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了Android ActionBar制作時(shí)鐘的實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05

