水波纹
public class weater extends View { private Path mPathTop,mPathBottom; private Paint mPaintTop,mPaintBottom; float φ; public weater(context context) { super(context); init(context); } public weater(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context); } public weater(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { mPathTop = new Path(); mPathBottom = new Path(); mPaintTop = new Paint(); mPaintTop.setcolor(Color.WHITE); mPaintTop.setAntiAlias(true); mPaintBottom = new Paint(); mPaintBottom.setColor(Color.WHITE); mPaintBottom.setAntiAlias(true); mPaintBottom.setAlpha(10); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPathTop.reset(); mPathBottom.reset(); mPathTop.moveTo(getLeft(),getBottom()); mPathBottom.moveTo(getLeft(),getTop()); double mY = Math.PI*2/getWidth(); float y,y2; φ-=0.1f; for(float x=0;x<=getWidth();x+=20){ y= (float) (10*Math.cos(mY*x+φ))+10; y2 = (float) (10*Math.sin(mY*x+φ)); mPathTop.lineTo(x,y); mPathBottom.lineTo(x,y2); listener.shezhiheight(y-20); } mPathTop.lineTo(getRight(),getBottom()); mPathBottom.lineTo(getRight(),getBottom()); canvas.drawPath(mPathTop,mPaintTop); canvas.drawPath(mPathBottom,mPaintBottom); postinvalidateDelayed(20); } private WeaterListener listener; public void reslt(WeaterListener listener){ this.listener = listener; } public interface WeaterListener{ void shezhiheight(float a); } }
=============================Activity========================================
protected void onCreate(@Nullable Bundle savedinstanceState) { super.onCreate(savedInstanceState); setcontentView(R.layout.activity_weater); imageView = (ImageView) findViewById(R.id.img_weater); water_view = (weater) findViewById(R.id.water_view); final relativelayout.layoutparams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); water_view.reslt(new weater.WeaterListener() { @Override public void shezhiheight(float a) { layoutParams.setMargins(0,0,0, (int) a); imageView.setLayoutParams(layoutParams); } }); }
==============================布局文件===========================================
<RelativeLayout Android:background="#d43c3c" android:layout_width="match_parent" android:layout_height="200dp"> <ImageView android:id="@+id/img_weater" android:layout_above="@+id/water_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:src="@mipmap/ic_launcher"/> <yuekao.zhaoyufeng.bwie.com.day03_demo1.weater android:layout_alignParentBottom="true" android:id="@+id/water_view" android:layout_width="match_parent" android:layout_height="20dp" /> </RelativeLayout>