Android課本例4-5,例4-6

例4-5

代碼

MainActivity.java

package com.example.hpsyche.question4_2;

import android.os.Bundle;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private HandWrite handWrite = null;
    private Button clear = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //關聯view組件
        handWrite = (HandWrite) findViewById(R.id.handwriteview);
        clear = (Button) findViewById(R.id.clear);
        clear.setOnClickListener(new mClick());
    }

    private class mClick implements OnClickListener {
        public void onClick(View v) {
            //清屏
            handWrite.clear();
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <com.example.hpsyche.question4_2.HandWrite
        android:id="@+id/handwriteview"
        android:layout_width="match_parent"
        android:layout_height="380dp" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal">
        <Button
            android:id="@+id/clear"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="清屏" />
    </LinearLayout>
</LinearLayout>

HandWrite.java

package com.example.hpsyche.question4_2;

import android.content.Context;
import android.graphics.*;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class HandWrite extends View {
    //定義畫筆
    Paint paint = null;
    //存放原始圖像
    Bitmap originalBitmap = null;
    //存放從原始圖像複製的位圖圖像
    Bitmap new1_Bitmap = null;
    //存放處理後的圖像
    Bitmap new2_Bitmap = null;
    //畫線的起點座標
    float startX = 0, startY = 0;
    //畫線的終點座標
    float clickX = 0, clickY = 0;
    //設置是否畫線的標記
    boolean isMove = true;
    //設置是否清除塗鴉的標記
    boolean isClear = false;
    //設置畫筆的顏色(綠色)
    int color = Color.GREEN;
    //設置畫筆的寬度
    float strokeWidth = 2.0f;

    public HandWrite(Context context, AttributeSet attrs) {
        super(context, attrs);
        //從資源中獲取原始圖像
        originalBitmap = BitmapFactory
                .decodeResource(getResources(), R.drawable.test).copy(Bitmap.Config.ARGB_8888,true);
        //建立原始圖像的位圖
        originalBitmap= new1_Bitmap = Bitmap.createBitmap(originalBitmap);
    }

    //清除塗鴉
    public void clear() {
        isClear = true;
        new2_Bitmap = Bitmap.createBitmap(originalBitmap);
        invalidate();
    }
    public void setstyle(float strokeWidth) {
        this.strokeWidth = strokeWidth;
    }

    //顯示繪圖
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(HandWriting(new1_Bitmap), 0, 0, null);
    }

    //記錄繪畫圖形
    public Bitmap HandWriting(Bitmap o_Bitmap) {
        //定義畫布
        Canvas canvas = null;
        if (isClear){
            canvas = new Canvas(new2_Bitmap);
        }
        else{
            canvas = new Canvas(o_Bitmap);
        }
        paint = new Paint();
        paint.setStyle(Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStrokeWidth(strokeWidth);
        if (isMove) {
            //在畫布上畫線條
            canvas.drawLine(startX, startY, clickX, clickY, paint);
        }
        startX = clickX;
        startY = clickY;
        if (isClear) {
            return new2_Bitmap;
        }
        return o_Bitmap;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //獲取觸摸座標位置
        clickX = event.getX();
        clickY = event.getY();
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            isMove = false;
            invalidate();
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            isMove = true;
            invalidate();
            return true;
        }
        return super.onTouchEvent(event);
    }
}

運行結果

在這裏插入圖片描述

小結

  • 例4-5基本按照課本的例題來敲即可,不過此次實驗的繪畫功能較爲有意思,也有很多新的知識點;
  • 我們可以看到MVC的分層實現,其中佈局文件activity_main.xml代表了view,即視圖層的展示;而MainActivity.java代表了controller層,即控制層控制各種事件等;而HandWrite.java代表了model層,其記錄了模型數據的狀態更新等,封裝好塗鴉的結果返回給contoller層;

例 4-6

代碼

MainActivity.java

package com.example.hpsyche.example4_6;

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements GestureOverlayView.OnGesturePerformedListener{
    private  GestureOverlayView gestureOverlayView;
    private GestureLibrary gestureLibrary;
    private TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureOverlayView=findViewById(R.id.gestures1);
        gestureOverlayView.addOnGesturePerformedListener(this);
        txt=findViewById(R.id.textView1);
        gestureLibrary= GestureLibraries.fromRawResource(this,R.raw.gestures);
        if(!gestureLibrary.load()){
            finish();
        }
    }

    @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList predictions=gestureLibrary.recognize(gesture);
        if(predictions.size()>0){
            Prediction prediction= (Prediction) predictions.get(0);
            if(prediction.score>1.0){
                Toast.makeText(this,prediction.name,Toast.LENGTH_SHORT).show();
                txt.append(prediction.name);
            }
        }
    }
}

activity_man.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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text1"
        android:textSize="24sp"/>
    <!--繪製手勢的GestureOverlayView-->
    <android.gesture.GestureOverlayView
        android:id="@+id/gestures1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gestureStrokeType="multiple"
        android:eventsInterceptionEnabled="false"
        android:orientation="vertical"/>
</LinearLayout>

運行過程

  1. 打開手機虛擬機的的Gestures Builder,如下所示:
    在這裏插入圖片描述
  2. 加入各種手勢和對應的字母,如下所示
    在這裏插入圖片描述
  3. 添加入庫後,做如下操作:
    在這裏插入圖片描述
  4. 打開example4_6,即可識別到以添加的手勢,如下所示:
    在這裏插入圖片描述
    在這裏插入圖片描述
    即完成了我們的實驗!

小結

  • 例4-6看起來代碼較少,但由於課本關於配置的敘述不多,故在此次實驗中我也遇到了一些阻礙,通過搜索引擎的幫助最終得以成功!

總結

這次實驗基本代碼沒遇到什麼阻礙,就是在配置上需要去利用網上搜索,也是一個學習的過程,自己對觸摸屏事件的處理也有了進一步的認知與瞭解,覺得那些看起來很複雜的東西其實完成起來上也不是很難,下次實驗繼續努力!

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