Android studio 学习3:实现倒计时、画板

1、倒计时APP
运行图示:**
Android studio 学习3:实现倒计时、画板_第1张图片
Android studio 学习3:实现倒计时、画板_第2张图片

xml代码



    

    

java代码


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText input_time;
    private Button getTime,startTime,stopTime;
    private TextView time;
    private int i = 0;
    private Timer timer = null;
    private TimerTask task = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        initView();
    }

    private void initView(){
        input_time = (EditText) findViewById( R.id.input_time );
        getTime = (Button) findViewById( R.id.get_time );
        startTime = (Button) findViewById( R.id.start_time );
        stopTime = (Button) findViewById( R.id.stop_time );
        time = (TextView) findViewById( R.id.time );
        getTime.setOnClickListener( this );
        stopTime.setOnClickListener( this );
        startTime.setOnClickListener( this );
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_time:
                time.setText( input_time.getText().toString() );
                i = Integer.parseInt( input_time.getText().toString() );
                break;
            case R.id.start_time:
                StartTime();
                break;
            case R.id.stop_time:
                StopTime();
                break;
        }
    }


    private Handler mHandler = new Handler(){
        public void handleMessage(Message msg) {
            time.setText(msg.arg1+"");
            StartTime();
        };
    };

    public void StartTime(){
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                i--;
                Message message = mHandler.obtainMessage();
                message.arg1 = i;
                mHandler.sendMessage( message );
            }
        };
        timer.schedule( task, 1000 );
    }

    public void StopTime(){
        timer.cancel();
    }

}

2、画板APP
xml代码




    

    

java代码1

package com.example.drawingboard;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private MyView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        btn = (Button)findViewById( R.id.btn );
        view = (MyView)findViewById( R.id.draw ) ;
        btn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view.clear();
            }
        } );
    }
}

java代码2

package com.example.drawingboard;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class MyView extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {

    private Paint p = new Paint();
    private Path path = new Path();

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback( this );
        p.setColor( Color.RED  );
        p.setTextSize( 50 );
        p.setStyle( Paint.Style.STROKE );
//        p.setAntiAlias( true );
        setOnTouchListener( this );
    }

    public void draw(){
        Canvas canvas = getHolder().lockCanvas();
        canvas.drawColor( Color.WHITE );
        canvas.drawPath( path, p );
        getHolder().unlockCanvasAndPost( canvas );
    }

    public void clear(){
        path.reset();
        draw();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        draw();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                path.moveTo( event.getX(),event.getY() );
                draw();
             break;

            case MotionEvent.ACTION_MOVE:
                path.lineTo( event.getX(),event.getY() );
                draw();
             break;
        }
        return true;
    }
}

你可能感兴趣的:(Android,studio,android,studio,apk)