Android studio 學習3:實戰簡單 倒計時APP、畫板APP

Android studio 學習:實戰簡單倒計時APP、畫板APP
1、倒計時APP
運行圖示:

在這裏插入圖片描述
在這裏插入圖片描述

xml代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <EditText
        android:gravity="center"
        android:textSize="25dp"
        android:hint="請您輸入倒計時時間/秒"
        android:id="@+id/input_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:layout_gravity="center"
        android:textSize="20dp"
        android:textColor="@android:color/white"
        android:background="@drawable/d_bule"
        android:id="@+id/get_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取倒計時時間" />

    <TextView
        android:hint="顯示區"
        android:textColor="#EE7600"
        android:gravity="center"
        android:textSize="80dp"
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_marginTop="120dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_weight="1"
            android:background="@drawable/d_bule"
            android:id="@+id/start_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:text="開  始" />
        <Button
            android:layout_toRightOf="@id/start_time"
            android:layout_weight="1"
            android:background="@drawable/d_bule"
            android:id="@+id/stop_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:text="停  止" />
    </LinearLayout>

</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.example.drawingboard.MyView
        android:id="@+id/draw"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清理畫布"/>
</LinearLayout>

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章