这阶段忙着做实验,更新就搁置下来了。。。
同时学习了android自定义控件,下面介绍水波纹的具体写法:
package com.example111.sxy.a09; 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.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by sxy on 2017/10/26. */ public class WaveView extends View { private Paint paint; private int radio = 5; public WaveView(Context context, @Nullable AttributeSet attrs) { super(context, attrs);
//画笔 radio=5; paint = new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(radio/3); } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); radio +=5; int alpha = paint.getAlpha(); alpha -=5; if(alpha <0) { alpha = 0; } paint.setAlpha(alpha); paint.setStrokeWidth(radio/3); invalidate(); } }; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(paint.getAlpha()>0 && downX>0 &&downY>0) { canvas.drawCircle(downX,downY,radio,paint); handler.sendEmptyMessageDelayed(0,50); } } private float downX; private float downY; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); radio=5; paint = new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(radio/3); invalidate(); break; } return super.onTouchEvent(event); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example111.sxy.a09.MainActivity"> <com.example111.sxy.a09.WaveView android:layout_height="match_parent" android:layout_width="match_parent" /> </RelativeLayout>