AndroidStudio實現水波紋
阿新 • • 發佈:2018-12-14
public class FragmentOne extends Fragment { private ViewPager view_pager; private ImageView img; private WeatherView weather_view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_fragment_one, container, false); view_pager=(ViewPager)view.findViewById(R.id.view_pager); weather_view=(WeatherView)view.findViewById(R.id.weather_view); img=(ImageView)view.findViewById(R.id.img); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) img.getLayoutParams(); weather_view.animal(new WeatherView.AnimalLister() { @Override public void animal(float f) { params.setMargins(0,0,0, (int) f); img.setLayoutParams(params); } }); } }
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); } }