Android开发-自定义View-AndroidStudio(十七)自定义水波纹(1)

时间:2023-02-09 09:20:39
转载请注明出处: http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~


老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
2个动图,上面的动图只实现了圆环内径自增和圆环宽度的自增,下面的动图实现了透明度的改变。 Android开发-自定义View-AndroidStudio(十七)自定义水波纹(1)
WaveView.java:
package com.iwanghang.waveview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

/**
* 水波纹
*/
public class WaveView extends View {

private Paint paint;
private int radio = 5; // 圆环半径

public WaveView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}

// 1、创建画笔
private void initView() {
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true); // 抗锯齿
paint.setStyle(Paint.Style.STROKE); // 样式为圆环
paint.setStrokeWidth(radio); // 圆环宽度为radio
}

// 2、开始绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(200,200,radio,paint); // 重绘的圆环的内径A1
handler.sendEmptyMessageDelayed(0,50);
}

// 3、让圆环动起来 实例化一个Handler
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
radio += 5; // 这里的自增会影响每一次重绘的圆环的内径A1和圆环的宽度A2
paint.setStrokeWidth(radio); // 重绘的圆环的宽度A2
invalidate(); // onDraw();
}
};

}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.iwanghang.waveview.MainActivity">    <com.iwanghang.waveview.WaveView        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

Android开发-自定义View-AndroidStudio(十七)自定义水波纹(1)
WaveView.java:
package com.iwanghang.waveview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.View;/** * 水波纹 */public class WaveView extends View {    private Paint paint;    private int radio = 5; // 圆环半径    public WaveView(Context context, AttributeSet attrs) {        super(context, attrs);        initView();    }    // 1、创建画笔    private void initView() {        paint = new Paint();        paint.setColor(Color.RED);        paint.setAntiAlias(true); // 抗锯齿        paint.setStyle(Paint.Style.STROKE); // 样式为圆环        paint.setStrokeWidth(radio); // 圆环宽度为radio    }    // 2、开始绘制    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawCircle(200,200,radio,paint); // 重绘的圆环的内径A1        handler.sendEmptyMessageDelayed(0,50);    }    // 3、让圆环动起来 实例化一个Handler    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            radio += 5; // 这里的自增会影响每一次重绘的圆环的内径A1和圆环的宽度A2            paint.setStrokeWidth(radio); // 重绘的圆环的宽度A2            // 4、让圆环透明度递减            int alpha = paint.getAlpha();            alpha -= 5;            if (alpha <= 0){                alpha = 0;            }            paint.setAlpha(alpha);            invalidate(); // onDraw();        }    };}

问题来了,现在是无休止的重绘,我们需要加一些判断,比如透明为0,就不要在重绘了。请按B1->B2->B3顺序看代码~

WaveView.java:
package com.iwanghang.waveview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.View;/** * 水波纹 */public class WaveView extends View {    private Paint paint;    private int radio = 5; // 圆环半径    public WaveView(Context context, AttributeSet attrs) {        super(context, attrs);        initView();    }    // 1、创建画笔    private void initView() {        paint = new Paint();        paint.setColor(Color.RED);        paint.setAntiAlias(true); // 抗锯齿        paint.setStyle(Paint.Style.STROKE); // 样式为圆环        paint.setStrokeWidth(radio); // 圆环宽度为radio    }    // 2、开始绘制    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawCircle(200,200,radio,paint); // 重绘的圆环的内径A1        if (paint.getAlpha()>0){ // 只有当透明度大于0 才继续给handler发消息 B3            handler.sendEmptyMessageDelayed(0,50); // 每次重绘后,等待50毫秒,给handler发消息 B1        }    }    // 3、让圆环动起来 实例化一个Handler    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            // 每次handler接到消息都要invalidate()从而onDraw(); B2            super.handleMessage(msg);            radio += 5; // 这里的自增会影响每一次重绘的圆环的内径A1和圆环的宽度A2            paint.setStrokeWidth(radio); // 重绘的圆环的宽度A2            // 4、让圆环透明度递减            int alpha = paint.getAlpha();            alpha -= 5;            if (alpha <= 0){                alpha = 0;            }            paint.setAlpha(alpha);            invalidate(); // onDraw();        }    };}























转载请注明出处: http://blog.csdn.net/iwanghang/



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
Android开发-自定义View-AndroidStudio(十七)自定义水波纹(1)
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~