1. 程式人生 > >自定義水波擴散圖片

自定義水波擴散圖片

公司產品需要一個按鈕有一個水波紋擴散的效果,就簡單的做了一個。先上圖

然後就是原始碼了,程式碼很簡單 我就不解釋了,就是想留著做個紀念

package com.example.administrator.myapplication.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint; import android.graphics.RectF; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.ImageView; import com.example.administrator.myapplication.R;
/** * Created by Xuyan on 2017/6/13 0013. */ public class MyImageView extends android.support.v7.widget.AppCompatImageView { /** * 畫邊框的畫筆 */ private Paint paint; /** *邊框的顏色 */ private int bordColor; /** * 邊框總共的數量 * @param context */ private final static int
Total = 3; /** * 是否要建立新的邊框 * @param context */ private boolean isBord = true; /** * 邊框半徑 */ private float bordRadio = 0; private RectF rectF ; private int alpha=255; private int bordWith =10; public MyImageView(Context context) { super(context); } public MyImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public MyImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a =context.obtainStyledAttributes(attrs, R.styleable.MyImageView); bordColor = a.getColor(R.styleable.MyImageView_bordcolor, Color.parseColor("#ffffff")); a.recycle(); init(); } public void init(){ paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(bordWith); paint.setStyle(Paint.Style.STROKE); rectF = new RectF(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int with = setMeasure(widthMeasureSpec,100); int height = setMeasure(heightMeasureSpec,100); if (with!=height){ with=Math.max(with,height); height =with; } setMeasuredDimension(with,height); rectF.set(0,0,getMeasuredWidth(),getMeasuredWidth()); new Thread(runnable).start(); } public int setMeasure(int MeasureSpec,int defultsize){ int model = View.MeasureSpec.getMode(MeasureSpec); int size = View.MeasureSpec.getSize(MeasureSpec); int result; if (model==View.MeasureSpec.EXACTLY){ result = size; }else { if (model== View.MeasureSpec.AT_MOST){ result = Math.max(size,defultsize); }else { result = defultsize; } } return result; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i=0;i<Total;i++){ canvas.drawCircle(getMeasuredHeight()/2,getMeasuredHeight()/2,bordRadio-i*100,paint); } } Handler hanl = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==0){ paint.setAlpha(alpha); invalidate(); } } }; Runnable runnable = new Runnable() { @Override public void run() { while (true){ if (bordRadio>getMeasuredWidth()/2){ bordRadio = 0; bordWith =10; alpha=255; }else { bordRadio=bordRadio+2; alpha =(alpha-(int)(bordRadio/255))<0?0:(alpha-(int)(bordRadio/255)); bordWith +=10; } try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } hanl.sendEmptyMessage(0); } } }; /** * 設定圓環半徑 */ public void setRadio(int bordRadio){ if (bordRadio>getMeasuredHeight()/2){ this.bordRadio = getMeasuredHeight()/2; }else { this.bordRadio = bordRadio; } } /** * 設定圓環初始的邊框寬度 */ public void setBordWith(int bordWith){ if (bordWith>50||bordWith<0){ bordWith=10; }else { this.bordWith =bordWith; } } }