地震波自定义View

xiaoxiao2021-02-28  40

MainActivity

public class MainActivity extends Activity { private Button btn; private SeismicWaveView seismicWaveView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.button); btn.setText("开始"); seismicWaveView = (SeismicWaveView) findViewById(R.id.seismicwaveview); //控制地震波的按钮 btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (seismicWaveView.isStarting()) { btn.setText("继续"); seismicWaveView.stop(); } else { btn.setText("停止"); seismicWaveView.start(); } } }); } }

SeismicWaveView

public class SeismicWaveView extends View { private Paint paint; private int maxWidth = 260; private boolean isStarting = false; private List<String> alphaList = new ArrayList<String>(); private List<String> startWidthList = new ArrayList<String>(); public SeismicWaveView(Context context) { super(context); init(); } public SeismicWaveView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public SeismicWaveView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { paint = new Paint(); paint.setColor(Color.GRAY);//此处颜色可以改为自己喜欢的 alphaList.add("260");//圆心的不透明度 startWidthList.add("0"); } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); setBackgroundColor(Color.TRANSPARENT);//颜色:完全透明 //依次绘制 同心圆 for (int i = 0; i < alphaList.size(); i++) { int alpha = Integer.parseInt(alphaList.get(i)); int startWidth = Integer.parseInt(startWidthList.get(i)); paint.setAlpha(alpha); canvas.drawCircle(getWidth() / 2, getHeight() / 2, startWidth,paint); //同心圆扩散 if (isStarting && alpha > 0 && startWidth < maxWidth) { alphaList.set(i, (alpha-1)+""); startWidthList.set(i, (startWidth+1)+""); } } if (isStarting&&Integer.parseInt(startWidthList.get(startWidthList.size() - 1)) == maxWidth / 5) { alphaList.add("260"); startWidthList.add("0"); } //同心圆数量达到6个,删除最外层圆 if(isStarting&&startWidthList.size()==4) { startWidthList.remove(0); alphaList.remove(0); } //刷新界面 invalidate(); } //地震波开始/继续进行 public void start() { isStarting = true; } //地震波暂停 public void stop() { isStarting = false; } public boolean isStarting() { return isStarting; } }

MainActivity布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > > <com.example.maleking.platform.SeismicWaveView android:id="@+id/seismicwaveview" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@mipmap/ic_launcher" /> </RelativeLayout> </LinearLayout>

转载请注明原文地址: https://www.6miu.com/read-1950099.html

最新回复(0)