Android two ways to achieve similar wave diffusion effects

Source: Internet
Author: User

Original link HTTPS://MP.WEIXIN.QQ.COM/S/M19TP_SHOO6ESKDOZI7NLG

Two ways to achieve a similar wave diffusion effect, first

    1. Custom View implementations
    2. Animation implementation

Custom View implementations

Thinking Analysis: Through the canvas to draw a circle, each time changing the radius and transparency, when the radius reached a certain degree, again from the center to draw a circle, to achieve different levels of effect, through continuous drawing to achieve the view diffusion effect

private Paint centerPaint; //中心圆paintprivate int radius = 100; //中心圆半径private Paint spreadPaint; //扩散圆paintprivate float centerX;//圆心xprivate float centerY;//圆心yprivate int distance = 5; //每次圆递增间距private int maxRadius = 80; //最大圆半径private int delayMilliseconds = 33;//扩散延迟间隔,越大扩散越慢private List<Integer> spreadRadius = new ArrayList<>();//扩散圆层级数,元素为扩散的距离private List<Integer> alphas = new ArrayList<>();//对应每层圆的透明度

Custom properties in the style file

<declare-styleable name="SpreadView">    <!--中心圆颜色-->    <attr name="spread_center_color" format="color" />    <!--中心圆半径-->    <attr name="spread_radius" format="integer" />    <!--扩散圆颜色-->    <attr name="spread_spread_color" format="color" />    <!--扩散间距-->    <attr name="spread_distance" format="integer" />    <!--扩散最大半径-->    <attr name="spread_max_radius" format="integer" />    <!--扩散延迟间隔-->    <attr name="spread_delay_milliseconds" format="integer" /></declare-styleable>

Initialization

Public Spreadview (Context context) {This (context, NULL, 0);} Public Spreadview (context context, @Nullable AttributeSet attrs) {This (context, attrs, 0);} Public Spreadview (context context, @Nullable attributeset attrs, int defstyleattr) {Super (context, Attrs, defstyleattr    );    TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.spreadview, defstyleattr, 0);    Radius = A.getint (R.styleable.spreadview_spread_radius, RADIUS);    Maxradius = A.getint (R.styleable.spreadview_spread_max_radius, Maxradius); int centercolor = A.getcolor (R.styleable.spreadview_spread_center_color, Contextcompat.getcolor (context,    R.color.coloraccent)); int spreadcolor = A.getcolor (R.styleable.spreadview_spread_spread_color, Contextcompat.getcolor (context,    R.color.coloraccent));    Distance = A.getint (r.styleable.spreadview_spread_distance, distance);    A.recycle ();    Centerpaint = new Paint ();    Centerpaint.setcolor (Centercolor);    Centerpaint.setantialias (TRUE); The initial opacity and diffusion distance is0 Alphas.add (255);    Spreadradius.add (0);    Spreadpaint = new Paint ();    Spreadpaint.setantialias (TRUE);    Spreadpaint.setalpha (255); Spreadpaint.setcolor (Spreadcolor);}

Determine the center position

@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {    super.onSizeChanged(w, h, oldw, oldh);    //圆心位置    centerX = w / 2;    centerY = h / 2;}

Customize the drawing of a view

@Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);        for (int i = 0; i < spreadradius.size (); i++) {int alpha = Alphas.get (i);        Spreadpaint.setalpha (Alpha);        int width = Spreadradius.get (i);        Draw the diffused circle Canvas.drawcircle (CenterX, CenterY, radius + width, spreadpaint);  Each diffusion circle radius is incremented, the circle transparency is decremented if (Alpha > 0 && width <) {Alpha = alpha-distance > 0? alpha            -Distance:1;            Alphas.set (i, alpha);        Spreadradius.set (i, width + distance); }}//When the outermost diffusion circle radius reaches the maximum radius, add a new diffusion circle if (Spreadradius.get (Spreadradius.size ()-1) > Maxradius) {Spreadradius.        Add (0);    Alphas.add (255);        }//More than 8 diffusion circles, delete the first drawn circle, that is, the outermost circle if (spreadradius.size () >= 8) {alphas.remove (0);    Spreadradius.remove (0);    }//In the middle of the circle Canvas.drawcircle (CenterX, CenterY, radius, centerpaint); TODO can draw text or images//delay updates in the middle circle to reach the diffuse visual difference effect postinvalidatedelayed (Delaymilliseconds);} 

XML styles

<com.airsaid.diffuseview.widget.SpreadView    android:id="@+id/spreadView"    android:layout_width="match_parent"    android:layout_height="wrap_content"    app:spread_center_color="@color/colorAccent"    app:spread_delay_milliseconds="35"    app:spread_distance="5"    app:spread_max_radius="90"    app:spread_radius="100"    app:spread_spread_color="@color/colorAccent" />


Center circle can be customized to write text, pictures and so on ...

Animation implementation

Thinking Analysis: Through animation, imageView do not stop animation zoom + gradient
The center imageView remains the same.
The middle layer is imageView enlarged from the original to 1.4 times times, while the opacity changes from opaque to translucent
The outermost layer is imageView magnified from 1.4 times times to 1.8 times times, and from translucent to full transparency
Use shape to draw a circle as an animated base view

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <corners android:radius="65dp"/>    <solid android:color="@color/colorAccent"/></shape>

Layout view

<FrameLayout    android:layout_width="match_parent"    android:layout_height="match_parent">    <!--中心imageView-->    <ImageView        android:id="@+id/iv_wave"        android:layout_width="130dp"        android:layout_height="130dp"        android:layout_gravity="center"        android:background="@drawable/shape_circle" />    <!--中间的imageView-->    <ImageView        android:id="@+id/iv_wave_1"        android:layout_width="130dp"        android:layout_height="130dp"        android:layout_gravity="center"        android:background="@drawable/shape_circle" />    <!--最外层imageView-->    <ImageView        android:id="@+id/iv_wave_2"        android:layout_width="130dp"        android:layout_height="130dp"        android:layout_gravity="center"        android:background="@drawable/shape_circle" /></FrameLayout>

Middle imageView of the animation

private void setAnim1() {    AnimationSet as = new AnimationSet(true);    //缩放动画,以中心从原始放大到1.4倍    ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.4f, 1.0f, 1.4f,            ScaleAnimation.RELATIVE_TO_SELF, 0.5f,            ScaleAnimation.RELATIVE_TO_SELF, 0.5f);    //渐变动画    AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);    scaleAnimation.setDuration(800);    scaleAnimation.setRepeatCount(Animation.INFINITE);    alphaAnimation.setRepeatCount(Animation.INFINITE);    as.setDuration(800);    as.addAnimation(scaleAnimation);    as.addAnimation(alphaAnimation);    iv1.startAnimation(as);}

The outermost imageView animation

private void setAnim2() {    AnimationSet as = new AnimationSet(true);    //缩放动画,以中心从1.4倍放大到1.8倍    ScaleAnimation scaleAnimation = new ScaleAnimation(1.4f, 1.8f, 1.4f, 1.8f,            ScaleAnimation.RELATIVE_TO_SELF, 0.5f,            ScaleAnimation.RELATIVE_TO_SELF, 0.5f);    //渐变动画    AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 0.1f);    scaleAnimation.setDuration(800);    scaleAnimation.setRepeatCount(Animation.INFINITE);    alphaAnimation.setRepeatCount(Animation.INFINITE);    as.setDuration(800);    as.addAnimation(scaleAnimation);    as.addAnimation(alphaAnimation);    iv2.startAnimation(as);}


In comparison, custom view works better, and animations are easier to implement.

Two ways to achieve the diffusion effect is finished, the specific project is to be changed on demand.

Welcome to follow my blog: https://blog.manjiexiang.cn/
At the same time, welcome attention to public

Android two ways to achieve similar wave diffusion effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.