Seismic Wave Custom View

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                                        ) ;
 //The button that controls the seismic wave
 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 ) ; //The color here can be changed to your favorite
 alphaList .add ( "260" ) ; //The opacity of the center of the circle
 startWidthList . add( "0" ) ;
 }                            

    @Override
 public void onDraw (Canvas canvas) {
         super .onDraw(canvas) ;
 setBackgroundColor(Color. TRANSPARENT ) ; //Color: fully transparent
         // Draw concentric circles in turn
 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" ) ;
 }
         //Concentric circles When the number reaches 6, delete the outermost circle
 if ( isStarting && startWidthList .size()== 4 )                            
        {
            startWidthList .remove( 0 ) ;
 alphaList .remove( 0 ) ;
 }
         //Refresh the interface
 invalidate() ;
 }                                

    //The seismic wave starts/continues
 public void start () {
         isStarting = true;
 }        

    //Seismic wave pause
 public void stop () {
         isStarting = false;
 }        

    public boolean isStarting() {
        return isStarting;
    }

}

MainActivity layout

<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>












































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324685405&siteId=291194637