Android簡單實現水波紋
阿新 • • 發佈:2018-12-14
public class WeatherView extends View { private Path path1,path2; private Paint paint1,paint2; private float Φ; public WeatherView(Context context) { super(context); init(context); } public WeatherView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context); } public WeatherView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { paint1 = new Paint(); paint2 = new Paint(); paint1.setColor(Color.WHITE); paint2.setColor(Color.WHITE); paint1.setAntiAlias(true); paint2.setAntiAlias(true); //paint2.setAlpha(90); path1 = new Path(); path2 = new Path(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); path1.reset(); path2.reset(); //路徑的起始位置 path1.moveTo(getLeft(),getBottom()); path2.moveTo(getLeft(),getBottom()); double my=Math.PI*2/getWidth(); Φ-=0.1f; //路徑的移動位置 for(int i=0;i<=getWidth();i+=20){ path1.lineTo(i, (float) (10*(Math.cos(my*i+Φ))+10)); path2.lineTo(i, (float) (10*Math.sin(my*i+Φ))); } //路徑的結束位置 path1.lineTo(getRight(),getBottom()); path2.lineTo(getRight(),getBottom()); canvas.drawPath(path1,paint1); canvas.drawPath(path2,paint2); postInvalidateDelayed(20); } private AnimalLister lister; public void animal(AnimalLister lister){ this.lister=lister; }; public interface AnimalLister{ void animal(float f); } }