添加滑动手势到我的日历应用程序 - android

时间:2013-07-30 05:36:12

标签: android

我是开发应用程序的初学者。对不起,如果这是一个重复的问题,我几天来一直在努力解决这个问题。

所以我所拥有的是一个日历(这是我在互联网上找到的一个例子)。我想要做的是制作一个颜色编码的日历以符合我的工作时间表,所以我添加了一些东西来完成这项工作并用一些不同的drawable改变了它的外观。一切都有点好。我甚至能够弄清楚当选择某一天时如何添加它会将你的谷歌日历带到同一天以安排活动。但对于我的生活,我无法弄清楚这个手势部分。

所以下面我将提供我的代码,希望你们中的一个好人可以给我一些指示。我想我很接近。我已经构建了保存在其中的“next”和“previous”的手势库,并将其放入我创建的原始文件夹中。现在我把它设置好,它会干预预测名称,但它没有做吐司。我确实看到了向左或向右滑动的黄线。最终,我希望这可以在onClick方法中为下一个和之前的方法做一些在手势方法之下。

    package com.examples;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.trhodes.workcalendar2.R;

public class SimpleCalendarViewActivity extends Activity implements
        OnClickListener, OnGesturePerformedListener {
    private static final String tag = "SimpleCalendarViewActivity";
    Button selectedDayMonthYearButton;
    private Button currentMonth;
    private ImageView prevMonth;
    private ImageView nextMonth;
    private GridView calendarView;
    private GridCellAdapter adapter;
    private Calendar _calendar;
    private int month, year;
    private static final String dateTemplate = "MMMM yyyy";
    GestureLibrary mLibrary;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_calendar_view);
        // new GestureDetector(this, this);
        _calendar = Calendar.getInstance(Locale.getDefault());
        month = _calendar.get(Calendar.MONTH) + 1;
        year = _calendar.get(Calendar.YEAR);
        Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: "
                + year);

        selectedDayMonthYearButton = (Button) this
                .findViewById(R.id.selectedDayMonthYear);
        selectedDayMonthYearButton.setText("Selected: ");

        prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
        prevMonth.setOnClickListener(this);

        currentMonth = (Button) this.findViewById(R.id.currentMonth);
        currentMonth.setText(DateFormat.format(dateTemplate,
                _calendar.getTime()));

        nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
        nextMonth.setOnClickListener(this);

        calendarView = (GridView) this.findViewById(R.id.calendar);

        // Initialised
        adapter = new GridCellAdapter(getApplicationContext(),
                R.id.calendar_day_gridcell, month, year);
        adapter.notifyDataSetChanged();
        calendarView.setAdapter(adapter);


        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
        gestures.addOnGesturePerformedListener(this);

        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (!mLibrary.load()) {
            finish();
        }
    }
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

        // We want at least one prediction
        if (predictions.size() > 0 && predictions.get(0).score > 1.0
                && gesture.getStrokesCount() > 1) {
            String what = "";
            for (int i = 0; i < predictions.size(); i++) {
                if (predictions.get(i).name.equals("next")
                        || predictions.get(i).name.equals("previous")) {
                    what = predictions.get(i).name;
                    break;
                }
            }
            if(what.equals("next")){

//              Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
//                      + month + " Year: " + year);
//              setGridCellAdapterToDate(month, year);
                Toast.makeText(this, what, Toast.LENGTH_LONG).show();


            }else if(what.equals("previous")){

//                  Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
//                          + month + " Year: " + year);
//                  setGridCellAdapterToDate(month, year);
                Toast.makeText(this, what, Toast.LENGTH_LONG).show();
                }}}


    /**
     * 
     * @param month
     * @param year
     */
    private void setGridCellAdapterToDate(int month, int year) {
        adapter = new GridCellAdapter(getApplicationContext(),
                R.id.calendar_day_gridcell, month, year);
        _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
        currentMonth.setText(DateFormat.format(dateTemplate,
                _calendar.getTime()));
        adapter.notifyDataSetChanged();
        calendarView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        if (v == prevMonth) {
            if (month <= 1) {
                month = 12;
                year--;
            } else {
                month--;
            }
            Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
                    + month + " Year: " + year);
            setGridCellAdapterToDate(month, year);
            Toast.makeText(this, "Previous", Toast.LENGTH_LONG).show();
            return;
        }
        if (v == nextMonth) {
            if (month > 11) {
                month = 1;
                year++;
            } else {
                month++;
            }
            Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
                    + month + " Year: " + year);
            setGridCellAdapterToDate(month, year);
            Toast.makeText(this, "Next", Toast.LENGTH_LONG).show();
            return;
        }

    }

    @Override
    public void onDestroy() {
        Log.d(tag, "Destroying View ...");
        super.onDestroy();
    }

    // ///////////////////////////////////////////////////////////////////////////////////////
    // Inner Class
    public class GridCellAdapter extends BaseAdapter implements OnClickListener {
        private static final String tag = "GridCellAdapter";
        private final Context _context;

        private final List<String> list;
        private static final int DAY_OFFSET = 1;
        private final String[] weekdays = new String[] { "Sun", "Mon", "Tue",
                "Wed", "Thu", "Fri", "Sat" };
        private final String[] months = { "January", "February", "March",
                "April", "May", "June", "July", "August", "September",
                "October", "November", "December" };
        private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
                31, 30, 31 };
        private int daysInMonth;
        private int currentDayOfMonth;
        private int currentWeekDay;
        private Button gridcell;
        private TextView num_events_per_day;
        private final HashMap<String, Integer> eventsPerMonthMap;

        // Days in Current Month
        public GridCellAdapter(Context context, int textViewResourceId,
                int month, int year) {
            super();
            this._context = context;
            this.list = new ArrayList<String>();
            Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
                    + "Year: " + year);
            Calendar calendar = Calendar.getInstance();
            setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
            setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
            // setCurrentMonth(calendar.get(Calendar.MONTH));
            Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
            Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
            Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());

            // Print Month
            printMonth(month, year);

            // Find Number of Events
            eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
        }

        private String getMonthAsString(int i) {
            return months[i];
        }

        private String getWeekDayAsString(int i) {
            return weekdays[i];
        }

        private int getNumberOfDaysOfMonth(int i) {
            return daysOfMonth[i];
        }

        public String getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        /**
         * Prints Month
         * 
         * @param mm
         * @param yy
         */
        private void printMonth(int mm, int yy) {
            Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
            // The number of days to leave blank at
            // the start of this month.
            int trailingSpaces = 0;
            int daysInPrevMonth = 0;
            int prevMonth = 0;
            int prevYear = 0;
            int nextMonth = 0;
            int nextYear = 0;

            // ////////////////////////////////////////////////////
            // added this string to hold the results for which color workColor
            // will use
            // initialized with the word "clear" so string won't be empty
            String workColor = "clear";
            // /////////////////////////////////////////////////////

            int currentMonth = mm - 1;
            String currentMonthName = getMonthAsString(currentMonth);
            daysInMonth = getNumberOfDaysOfMonth(currentMonth);

            Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
                    + daysInMonth + " days.");

            // Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
            GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
            Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

            if (currentMonth == 11) {
                prevMonth = currentMonth - 1;
                daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
                nextMonth = 0;
                prevYear = yy;
                nextYear = yy + 1;
                Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
                        + prevMonth + " NextMonth: " + nextMonth
                        + " NextYear: " + nextYear);
            } else if (currentMonth == 0) {
                prevMonth = 11;
                prevYear = yy - 1;
                nextYear = yy;
                daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
                nextMonth = 1;
                Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
                        + prevMonth + " NextMonth: " + nextMonth
                        + " NextYear: " + nextYear);
            } else {
                prevMonth = currentMonth - 1;
                nextMonth = currentMonth + 1;
                nextYear = yy;
                prevYear = yy;
                daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
                Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
                        + prevMonth + " NextMonth: " + nextMonth
                        + " NextYear: " + nextYear);
            }

            // Compute how much to leave before before the first day of the
            // month.
            // getDay() returns 0 for Sunday.
            int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
            trailingSpaces = currentWeekDay;

            Log.d(tag, "Week Day:" + currentWeekDay + " is "
                    + getWeekDayAsString(currentWeekDay));
            Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
            Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

            if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 2) {
                ++daysInMonth;
            }

            // Trailing Month days
            for (int i = 0; i < trailingSpaces; i++) {
                Log.d(tag,
                        "PREV MONTH:= "
                                + prevMonth
                                + " => "
                                + getMonthAsString(prevMonth)
                                + " "
                                + String.valueOf((daysInPrevMonth
                                        - trailingSpaces + DAY_OFFSET)
                                        + i));

                // /////////////////////////////////////////////////////////////////
                // added to the line below this code -> + "-" + workColor <-
                // was -> list.add(String.valueOf((daysInPrevMonth -
                // trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" +
                // getMonthAsString(prevMonth) + "-" + prevYear);
                // now -> list.add(String.valueOf((daysInPrevMonth -
                // trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" +
                // getMonthAsString(prevMonth) + "-" + prevYear + "-" +
                // workColor);
                list.add(String
                        .valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
                                + i)
                        + "-GREY"
                        + "-"
                        + getMonthAsString(prevMonth)
                        + "-"
                        + prevYear + "-" + workColor);
                // /////////////////////////////////////////////////////////////////////
            }

            // Current Month Days
            for (int i = 1; i <= daysInMonth; i++) {

                // ///////////////////////////////////////////////////////////////
                // reusing david's calendar code here and resetting the date
                // every time it loops thru the "for" statement
                cal.set(yy, currentMonth, i); // yy = year, currentMonth =
                                                // month, i = day
                // ////////////////////////////////////////////////////////////////

                // //////////////////////////////////////////////
                // you will need to implement some code here that will count the
                // number of weeks
                // from a set date. So roughly it would say
                // weeksFromSetDate = setDate (count the weeks to) todaysDate
                // then weeksFromSetDate would take the place of
                // "cal.get(Calendar.WEEK_OF_MONTH)" below
                // in the "if" statement. So, if weeksFromSetDate is an even
                // number it will
                // have no remainder and go to the related "switch" statement.
                // otherwise it will go to the lower "switch" statement below
                // "else".
                // Currently if you run the code it will only give you the
                // number of the week
                // within the given month. So the order of the work weeks from
                // month to month will be off.
                // This is just to give you an example to work with.
                // //////////////////////////////////////////////

                // int ordinalDay = cal.get(Calendar.DAY_OF_YEAR);
                // int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1; // Sunday =
                // 0
                // int numberOfWeeks = (ordinalDay - weekDay + 10) / 7;
                // System.out.println(numberOfWeeks);

                // ///////////////////////////////////////////////////////
                // added this "if" and "switch" statement
                // the "if" statement looks for odd or even weeks of the month
                // so, if the number of the week of the month has a zero
                // remainder after dividing by two is true go to switch
                // statement below

                if (cal.get(Calendar.WEEK_OF_YEAR) % 2 == 0) { // will find even
                                                                // week number
                    // the "switch" statement looks for the number of the day of
                    // the week
                    // so, if the number of the day of the week is 1, 2, 3, or 4
                    // then workColor will equal blue
                    // or , if the number of the day of the week is 5, 6, or 7
                    // then workColor will equal red
                    switch (cal.get(Calendar.DAY_OF_WEEK)) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        workColor = "blue";
                        break;
                    case 5:
                    case 6:
                    case 7:
                        workColor = "red";
                        break;
                    }
                    // if the number of the week of the month has a remainder
                    // after dividing by two then go to switch statement below
                } else { // will be an odd week number
                            // this switch statement will do the same as the one
                            // above with the exception that the workColor will
                            // change
                    switch (cal.get(Calendar.DAY_OF_WEEK)) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        workColor = "red";
                        break;
                    case 5:
                    case 6:
                    case 7:
                        workColor = "blue";
                        break;
                    }
                }

                Log.d(currentMonthName, String.valueOf(i) + " "
                        + getMonthAsString(currentMonth) + " " + yy + " "
                        + workColor);
                if (i == getCurrentDayOfMonth()) {

                    // /////////////////////////////////////////////////////////////////
                    // added to the line below this code -> + "-" + workColor <-
                    // was -> list.add(String.valueOf(i) + "-BLUE" + "-" +
                    // getMonthAsString(currentMonth) + "-" + yy);
                    //

                    // list.add(String.valueOf(i) + "-BLUE" + "-" +
                    // getMonthAsString(currentMonth) + "-" + yy + "-" +
                    // workColor);
                    list.add(String.valueOf(i) + "-GREEN" + "-"
                            + getMonthAsString(currentMonth) + "-" + yy + "-"
                            + workColor);
                    // ////////////////////////////////////////////////////////////////
                } else {

                    // /////////////////////////////////////////////////////////////////
                    // added to the line below this code -> + "-" + workColor <-
                    // was -> list.add(String.valueOf(i) + "-WHITE" + "-" +
                    // getMonthAsString(currentMonth) + "-" + yy);
                    // now -> list.add(String.valueOf(i) + "-WHITE" + "-" +
                    // getMonthAsString(currentMonth) + "-" + yy + "-" +
                    // workColor);
                    list.add(String.valueOf(i) + "-WHITE" + "-"
                            + getMonthAsString(currentMonth) + "-" + yy + "-"
                            + workColor);
                    // //////////////////////////////////////////////////////////////////
                }
            }

            // Leading Month days
            for (int i = 0; i < list.size() % 7; i++) {
                Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));

                // /////////////////////////////////////////////////////////////////
                // added to the line below this code -> + "-" + workColor <-
                // was -> list.add(String.valueOf(i + 1) + "-GREY" + "-" +
                // getMonthAsString(nextMonth) + "-" + nextYear);
                // now -> list.add(String.valueOf(i + 1) + "-GREY" + "-" +
                // getMonthAsString(nextMonth) + "-" + nextYear + "-" +
                // workColor);
                list.add(String.valueOf(i + 1) + "-GREY" + "-"
                        + getMonthAsString(nextMonth) + "-" + nextYear + "-"
                        + workColor);
                // /////////////////////////////////////////////////////////////////
            }
        }

        /**
         * NOTE: YOU NEED TO IMPLEMENT THIS PART Given the YEAR, MONTH, retrieve
         * ALL entries from a SQLite database for that month. Iterate over the
         * List of All entries, and get the dateCreated, which is converted into
         * day.
         * 
         * @param year
         * @param month
         * @return
         */
        private HashMap<String, Integer> findNumberOfEventsPerMonth(int year,
                int month) {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            // DateFormat dateFormatter2 = new DateFormat();

            // String day = dateFormatter2.format("dd", dateCreated).toString();

            // if (map.containsKey(day))
            // {
            // Integer val = (Integer) map.get(day) + 1;
            // map.put(day, val);
            // }
            // else
            // {
            // map.put(day, 1);
            // }
            return map;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater) _context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.calendar_day_gridcell, parent,
                        false);
            }
            // Get a reference to the Day gridcell
            gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
            gridcell.setOnClickListener(this);

            // ACCOUNT FOR SPACING

            Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
            Log.d(tag, "day_color " + list.get(position).toString());
            String[] day_color = list.get(position).split("-");
            String theday = day_color[0];
            String themonth = day_color[2];
            String theyear = day_color[3];

            // ///////////////////////////////////////////////////////////////////////////////
            // added day_color[4] it holds the information on which work color
            // will be used in this method
            String theworkcolor = day_color[4]; // theworkcolor will be the
                                                // string that holds it
            // /////////////////////////////////////////////////////////////////////////////////

            if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) {
                if (eventsPerMonthMap.containsKey(theday)) {
                    num_events_per_day = (TextView) row
                            .findViewById(R.id.num_events_per_day);
                    Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
                    num_events_per_day.setText(numEvents.toString());
                }
            }

            // Set the Day GridCell
            gridcell.setText(theday);
            gridcell.setTag(themonth + "/" + theday + "/" + theyear);
            Log.d(tag, "Setting GridCell " + themonth + "/" + theday + "/"
                    + theyear);

            if (day_color[1].equals("GREY")) {
                gridcell.setTextColor(Color.LTGRAY);
            }

            if (day_color[1].equals("WHITE"))

                // added this if statement below to check if theworkcolor is
                // equal to "red"
                if (theworkcolor.equalsIgnoreCase("red")) {
                    // if theworkcolor equals "red" then make the text for the
                    // day red
                    gridcell.setTextColor(Color.RED);
                    // if not make it the text for the day blue
                } else {
                    gridcell.setTextColor(Color.BLUE);
                }
            // //////////////////////////////////////////////////////////////////

            if (day_color[1].equals("GREEN")) {
                if (theworkcolor.equalsIgnoreCase("red")) {
                    // if theworkcolor equals "red" then make the text for the
                    // day red
                    gridcell.setTextColor(Color.RED);
                    // if not make it the text for the day blue
                } else {
                    gridcell.setTextColor(Color.BLUE);
                }

                if (day_color[1].equals("GREEN")) {
                    Calendar cal = Calendar.getInstance();// <== These 2 lines
                                                            // highlight current
                                                            // day of
                    if ((cal.get(Calendar.MONTH) + 1) == month)// <== current
                                                                // month
                                                                // only.

                        gridcell.setBackgroundResource(R.drawable.current_day);
                }
            }
            return row;
        }

        // Section below opens google calendar and puts the same date as date
        @SuppressLint("SimpleDateFormat")
        // pressed
        @Override
        public void onClick(View view) {
            String date_month_year = (String) view.getTag();
            Date startdate = null;

            try {

                startdate = new SimpleDateFormat("MMM/dd/yyyy")
                        .parse(date_month_year);

            } catch (ParseException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            // String eventStartInMillis = (String) date_month_year;
            intent.putExtra("beginTime", startdate.getTime());
            String eventEndInMillis = null;
            intent.putExtra("endTime", eventEndInMillis);
            startActivity(intent);

        }

        public int getCurrentDayOfMonth() {
            return currentDayOfMonth;
        }

        private void setCurrentDayOfMonth(int currentDayOfMonth) {
            this.currentDayOfMonth = currentDayOfMonth;
        }

        public void setCurrentWeekDay(int currentWeekDay) {
            this.currentWeekDay = currentWeekDay;
        }

        public int getCurrentWeekDay() {
            return currentWeekDay;
        }

    }
}

这也是我的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:background="@drawable/bg"
    android:orientation="vertical" >

<Button
    android:id="@+id/selectedDayMonthYear"
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:layout_gravity="center"
    android:background="@drawable/calendar_top_header"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:visibility="invisible"
    android:textColor="#FFFFFF" >

    </Button>
        <Button
            android:id="@+id/currentMonth"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/calendar_bar"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#4682B4" >

        </Button>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <ImageView
            android:id="@+id/calendarheader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/blue_bg_with_text" >
        </ImageView>
    </LinearLayout>
<LinearLayout 
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <android.gesture.GestureOverlayView
                android:id="@+id/gestures" android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:gestureStrokeType="multiple"
                android:eventsInterceptionEnabled="true" android:orientation="vertical">

    <GridView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:listSelector="@android:color/transparent"
        android:numColumns="7"
        android:stretchMode="columnWidth" >
    </GridView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom|center_vertical"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/prevMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.30"
            android:src="@drawable/calendar_left_arrow_selector" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_vertical|center"
            android:layout_weight="0.66"
            android:gravity="center"
            android:text="Press any day to schedule an event"
            android:textColor="#4682B4" />

        <ImageView
            android:id="@+id/nextMonth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="0.30"
            android:src="@drawable/calendar_right_arrow_selector" />

    </LinearLayout>
</android.gesture.GestureOverlayView>
</LinearLayout>
</LinearLayout>

我只是想提前感谢你的时间。我能得到的任何帮助将不胜感激。我希望我的问题很清楚。

2 个答案:

答案 0 :(得分:2)

好的,这是我提出的解决方案。我想确保以下面的方式发布这个可能需要帮助的人,这可能会有所帮助。感谢Maxim Efimov的回应,让我朝着正确的方向前进。此YouTube视频也为我提供了很多帮助GestureOverlayView tutorial

所以这是我的工作代码:

package com.examples;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.trhodes.workcalendar2.R;

public class SimpleCalendarViewActivity extends Activity implements
        OnClickListener, OnGesturePerformedListener {
    private static final String tag = "SimpleCalendarViewActivity";
    Button selectedDayMonthYearButton;
    private Button currentMonth;
    private ImageView prevMonth;
    private ImageView nextMonth;
    private GridView calendarView;
    private GridCellAdapter adapter;
    private Calendar _calendar;
    private int month, year;
    private static final String dateTemplate = "MMMM yyyy";
    private GestureLibrary mLibrary;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
        View inflate = getLayoutInflater().inflate(
                R.layout.simple_calendar_view, null);
        gestureOverlayView.addView(inflate);
        gestureOverlayView.addOnGesturePerformedListener(this);
        gestureOverlayView.setGestureColor(Color.TRANSPARENT);
        gestureOverlayView.setUncertainGestureColor(Color.TRANSPARENT);
        // speeds up the reaction time a little
        gestureOverlayView.setFadeOffset(0);
        gestureOverlayView.setHapticFeedbackEnabled(true);
        mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (!mLibrary.load()) {
            finish();
        }

        setContentView(gestureOverlayView);

        _calendar = Calendar.getInstance(Locale.getDefault());
        month = _calendar.get(Calendar.MONTH) + 1;
        year = _calendar.get(Calendar.YEAR);
        Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: "
                + year);

        selectedDayMonthYearButton = (Button) this
                .findViewById(R.id.selectedDayMonthYear);
        selectedDayMonthYearButton.setText("Selected: ");

        prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
        prevMonth.setOnClickListener(this);

        currentMonth = (Button) this.findViewById(R.id.currentMonth);
        currentMonth.setText(DateFormat.format(dateTemplate,
                _calendar.getTime()));

        nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
        nextMonth.setOnClickListener(this);

        calendarView = (GridView) this.findViewById(R.id.calendar);

        // Initialised
        adapter = new GridCellAdapter(getApplicationContext(),
                R.id.calendar_day_gridcell, month, year);
        adapter.notifyDataSetChanged();
        calendarView.setAdapter(adapter);

    }

    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
        ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
        for (Prediction prediction : predictions) {
            if (prediction.name.equals("next")) {

                if (month > 11) {
                    month = 1;
                    year++;
                } else {
                    month++;
                }
                Log.d(tag, "Setting Next Month in GridCellAdapter: "
                        + "Month: " + month + " Year: " + year);
                setGridCellAdapterToDate(month, year);
                return;
            } else {
                if (prediction.name.equals("previous")) {

                    if (month <= 1) {
                        month = 12;
                        year--;
                    } else {
                        month--;
                    }
                    Log.d(tag, "Setting Prev Month in GridCellAdapter: "
                            + "Month: " + month + " Year: " + year);
                    setGridCellAdapterToDate(month, year);
                    return;
                }
            }

        }

    }

    /**
     * 
     * @param month
     * @param year
     */
    private void setGridCellAdapterToDate(int month, int year) {
        adapter = new GridCellAdapter(getApplicationContext(),
                R.id.calendar_day_gridcell, month, year);
        _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
        currentMonth.setText(DateFormat.format(dateTemplate,
                _calendar.getTime()));
        adapter.notifyDataSetChanged();
        calendarView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        if (v == prevMonth) {
            if (month <= 1) {
                month = 12;
                year--;
            } else {
                month--;
            }
            Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
                    + month + " Year: " + year);
            setGridCellAdapterToDate(month, year);

            return;
        }
        if (v == nextMonth) {
            if (month > 11) {
                month = 1;
                year++;
            } else {
                month++;
            }
            Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
                    + month + " Year: " + year);
            setGridCellAdapterToDate(month, year);

            return;
        }

    }

    @Override
    public void onDestroy() {
        Log.d(tag, "Destroying View ...");
        super.onDestroy();
    }

    // ///////////////////////////////////////////////////////////////////////////////////////
    // Inner Class
    public class GridCellAdapter extends BaseAdapter implements OnClickListener {
        private static final String tag = "GridCellAdapter";
        private final Context _context;

        private final List<String> list;
        private static final int DAY_OFFSET = 1;
        private final String[] weekdays = new String[] { "Sun", "Mon", "Tue",
                "Wed", "Thu", "Fri", "Sat" };
        private final String[] months = { "January", "February", "March",
                "April", "May", "June", "July", "August", "September",
                "October", "November", "December" };
        private final int[] daysOfMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
                31, 30, 31 };
        private int daysInMonth;
        private int currentDayOfMonth;
        private int currentWeekDay;
        private Button gridcell;
        private TextView num_events_per_day;
        private final HashMap<String, Integer> eventsPerMonthMap;

        // Days in Current Month
        public GridCellAdapter(Context context, int textViewResourceId,
                int month, int year) {
            super();
            this._context = context;
            this.list = new ArrayList<String>();
            Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
                    + "Year: " + year);
            Calendar calendar = Calendar.getInstance();
            setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
            setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
            // setCurrentMonth(calendar.get(Calendar.MONTH));
            Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
            Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
            Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());

            // Print Month
            printMonth(month, year);

            // Find Number of Events
            eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
        }

        private String getMonthAsString(int i) {
            return months[i];
        }

        private String getWeekDayAsString(int i) {
            return weekdays[i];
        }

        private int getNumberOfDaysOfMonth(int i) {
            return daysOfMonth[i];
        }

        public String getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        /**
         * Prints Month
         * 
         * @param mm
         * @param yy
         */
        private void printMonth(int mm, int yy) {
            Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
            // The number of days to leave blank at
            // the start of this month.
            int trailingSpaces = 0;
            int daysInPrevMonth = 0;
            int prevMonth = 0;
            int prevYear = 0;
            int nextMonth = 0;
            int nextYear = 0;

            // ////////////////////////////////////////////////////
            // added this string to hold the results for which color workColor
            // will use
            // initialized with the word "clear" so string won't be empty
            String workColor = "clear";
            // /////////////////////////////////////////////////////

            int currentMonth = mm - 1;
            String currentMonthName = getMonthAsString(currentMonth);
            daysInMonth = getNumberOfDaysOfMonth(currentMonth);

            Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
                    + daysInMonth + " days.");

            // Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
            GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
            Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

            if (currentMonth == 11) {
                prevMonth = currentMonth - 1;
                daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
                nextMonth = 0;
                prevYear = yy;
                nextYear = yy + 1;
                Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
                        + prevMonth + " NextMonth: " + nextMonth
                        + " NextYear: " + nextYear);
            } else if (currentMonth == 0) {
                prevMonth = 11;
                prevYear = yy - 1;
                nextYear = yy;
                daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
                nextMonth = 1;
                Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
                        + prevMonth + " NextMonth: " + nextMonth
                        + " NextYear: " + nextYear);
            } else {
                prevMonth = currentMonth - 1;
                nextMonth = currentMonth + 1;
                nextYear = yy;
                prevYear = yy;
                daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
                Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
                        + prevMonth + " NextMonth: " + nextMonth
                        + " NextYear: " + nextYear);
            }

            // Compute how much to leave before before the first day of the
            // month.
            // getDay() returns 0 for Sunday.
            int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
            trailingSpaces = currentWeekDay;

            Log.d(tag, "Week Day:" + currentWeekDay + " is "
                    + getWeekDayAsString(currentWeekDay));
            Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
            Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

            if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 2) {
                ++daysInMonth;
            }

            // Trailing Month days
            for (int i = 0; i < trailingSpaces; i++) {
                Log.d(tag,
                        "PREV MONTH:= "
                                + prevMonth
                                + " => "
                                + getMonthAsString(prevMonth)
                                + " "
                                + String.valueOf((daysInPrevMonth
                                        - trailingSpaces + DAY_OFFSET)
                                        + i));

                // /////////////////////////////////////////////////////////////////
                // added to the line below this code -> + "-" + workColor <-
                // was -> list.add(String.valueOf((daysInPrevMonth -
                // trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" +
                // getMonthAsString(prevMonth) + "-" + prevYear);
                // now -> list.add(String.valueOf((daysInPrevMonth -
                // trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" +
                // getMonthAsString(prevMonth) + "-" + prevYear + "-" +
                // workColor);
                list.add(String
                        .valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
                                + i)
                        + "-GREY"
                        + "-"
                        + getMonthAsString(prevMonth)
                        + "-"
                        + prevYear + "-" + workColor);
                // /////////////////////////////////////////////////////////////////////
            }

            // Current Month Days
            for (int i = 1; i <= daysInMonth; i++) {

                // ///////////////////////////////////////////////////////////////
                // reusing david's calendar code here and resetting the date
                // every time it loops thru the "for" statement
                cal.set(yy, currentMonth, i); // yy = year, currentMonth =
                                                // month, i = day
                // ////////////////////////////////////////////////////////////////

                // //////////////////////////////////////////////
                // you will need to implement some code here that will count the
                // number of weeks
                // from a set date. So roughly it would say
                // weeksFromSetDate = setDate (count the weeks to) todaysDate
                // then weeksFromSetDate would take the place of
                // "cal.get(Calendar.WEEK_OF_MONTH)" below
                // in the "if" statement. So, if weeksFromSetDate is an even
                // number it will
                // have no remainder and go to the related "switch" statement.
                // otherwise it will go to the lower "switch" statement below
                // "else".
                // Currently if you run the code it will only give you the
                // number of the week
                // within the given month. So the order of the work weeks from
                // month to month will be off.
                // This is just to give you an example to work with.
                // //////////////////////////////////////////////

                // int ordinalDay = cal.get(Calendar.DAY_OF_YEAR);
                // int weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1; // Sunday =
                // 0
                // int numberOfWeeks = (ordinalDay - weekDay + 10) / 7;
                // System.out.println(numberOfWeeks);

                // ///////////////////////////////////////////////////////
                // added this "if" and "switch" statement
                // the "if" statement looks for odd or even weeks of the month
                // so, if the number of the week of the month has a zero
                // remainder after dividing by two is true go to switch
                // statement below

                if (cal.get(Calendar.WEEK_OF_YEAR) % 2 == 0) { // will find even
                                                                // week number
                    // the "switch" statement looks for the number of the day of
                    // the week
                    // so, if the number of the day of the week is 1, 2, 3, or 4
                    // then workColor will equal blue
                    // or , if the number of the day of the week is 5, 6, or 7
                    // then workColor will equal red
                    switch (cal.get(Calendar.DAY_OF_WEEK)) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        workColor = "blue";
                        break;
                    case 5:
                    case 6:
                    case 7:
                        workColor = "red";
                        break;
                    }
                    // if the number of the week of the month has a remainder
                    // after dividing by two then go to switch statement below
                } else { // will be an odd week number
                            // this switch statement will do the same as the one
                            // above with the exception that the workColor will
                            // change
                    switch (cal.get(Calendar.DAY_OF_WEEK)) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        workColor = "red";
                        break;
                    case 5:
                    case 6:
                    case 7:
                        workColor = "blue";
                        break;
                    }
                }

                Log.d(currentMonthName, String.valueOf(i) + " "
                        + getMonthAsString(currentMonth) + " " + yy + " "
                        + workColor);
                if (i == getCurrentDayOfMonth()) {

                    // /////////////////////////////////////////////////////////////////
                    // added to the line below this code -> + "-" + workColor <-
                    // was -> list.add(String.valueOf(i) + "-BLUE" + "-" +
                    // getMonthAsString(currentMonth) + "-" + yy);
                    //

                    // list.add(String.valueOf(i) + "-BLUE" + "-" +
                    // getMonthAsString(currentMonth) + "-" + yy + "-" +
                    // workColor);
                    list.add(String.valueOf(i) + "-GREEN" + "-"
                            + getMonthAsString(currentMonth) + "-" + yy + "-"
                            + workColor);
                    // ////////////////////////////////////////////////////////////////
                } else {

                    // /////////////////////////////////////////////////////////////////
                    // added to the line below this code -> + "-" + workColor <-
                    // was -> list.add(String.valueOf(i) + "-WHITE" + "-" +
                    // getMonthAsString(currentMonth) + "-" + yy);
                    // now -> list.add(String.valueOf(i) + "-WHITE" + "-" +
                    // getMonthAsString(currentMonth) + "-" + yy + "-" +
                    // workColor);
                    list.add(String.valueOf(i) + "-WHITE" + "-"
                            + getMonthAsString(currentMonth) + "-" + yy + "-"
                            + workColor);
                    // //////////////////////////////////////////////////////////////////
                }
            }

            // Leading Month days
            for (int i = 0; i < list.size() % 7; i++) {
                Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));

                // /////////////////////////////////////////////////////////////////
                // added to the line below this code -> + "-" + workColor <-
                // was -> list.add(String.valueOf(i + 1) + "-GREY" + "-" +
                // getMonthAsString(nextMonth) + "-" + nextYear);
                // now -> list.add(String.valueOf(i + 1) + "-GREY" + "-" +
                // getMonthAsString(nextMonth) + "-" + nextYear + "-" +
                // workColor);
                list.add(String.valueOf(i + 1) + "-GREY" + "-"
                        + getMonthAsString(nextMonth) + "-" + nextYear + "-"
                        + workColor);
                // /////////////////////////////////////////////////////////////////
            }
        }

        /**
         * NOTE: YOU NEED TO IMPLEMENT THIS PART Given the YEAR, MONTH, retrieve
         * ALL entries from a SQLite database for that month. Iterate over the
         * List of All entries, and get the dateCreated, which is converted into
         * day.
         * 
         * @param year
         * @param month
         * @return
         */
        private HashMap<String, Integer> findNumberOfEventsPerMonth(int year,
                int month) {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            // DateFormat dateFormatter2 = new DateFormat();

            // String day = dateFormatter2.format("dd", dateCreated).toString();

            // if (map.containsKey(day))
            // {
            // Integer val = (Integer) map.get(day) + 1;
            // map.put(day, val);
            // }
            // else
            // {
            // map.put(day, 1);
            // }
            return map;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater) _context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.calendar_day_gridcell, parent,
                        false);
            }
            // Get a reference to the Day gridcell
            gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
            gridcell.setOnClickListener(this);

            // ACCOUNT FOR SPACING

            Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
            Log.d(tag, "day_color " + list.get(position).toString());
            String[] day_color = list.get(position).split("-");
            String theday = day_color[0];
            String themonth = day_color[2];
            String theyear = day_color[3];

            // ///////////////////////////////////////////////////////////////////////////////
            // added day_color[4] it holds the information on which work color
            // will be used in this method
            String theworkcolor = day_color[4]; // theworkcolor will be the
                                                // string that holds it
            // /////////////////////////////////////////////////////////////////////////////////

            if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) {
                if (eventsPerMonthMap.containsKey(theday)) {
                    num_events_per_day = (TextView) row
                            .findViewById(R.id.num_events_per_day);
                    Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
                    num_events_per_day.setText(numEvents.toString());
                }
            }

            // Set the Day GridCell
            gridcell.setText(theday);
            gridcell.setTag(themonth + "/" + theday + "/" + theyear);
            Log.d(tag, "Setting GridCell " + themonth + "/" + theday + "/"
                    + theyear);

            if (day_color[1].equals("GREY")) {
                gridcell.setTextColor(Color.LTGRAY);
            }

            if (day_color[1].equals("WHITE"))

                // added this if statement below to check if theworkcolor is
                // equal to "red"
                if (theworkcolor.equalsIgnoreCase("red")) {
                    // if theworkcolor equals "red" then make the text for the
                    // day red
                    gridcell.setTextColor(Color.RED);
                    // if not make it the text for the day blue
                } else {
                    gridcell.setTextColor(Color.BLUE);
                }
            // //////////////////////////////////////////////////////////////////

            if (day_color[1].equals("GREEN")) {
                if (theworkcolor.equalsIgnoreCase("red")) {
                    // if theworkcolor equals "red" then make the text for the
                    // day red
                    gridcell.setTextColor(Color.RED);
                    // if not make it the text for the day blue
                } else {
                    gridcell.setTextColor(Color.BLUE);
                }

                if (day_color[1].equals("GREEN")) {
                    Calendar cal = Calendar.getInstance();// <== These 2 lines
                                                            // highlight current
                                                            // day of
                    if ((cal.get(Calendar.MONTH) + 1) == month)// <== current
                                                                // month
                                                                // only.

                        gridcell.setBackgroundResource(R.drawable.current_day);
                }
            }
            return row;
        }

        // Section below opens google calendar and puts the same date as date
        @SuppressLint("SimpleDateFormat")
        // pressed
        @Override
        public void onClick(View view) {
            String date_month_year = (String) view.getTag();
            Date startdate = null;

            try {

                startdate = new SimpleDateFormat("MMM/dd/yyyy")
                        .parse(date_month_year);

            } catch (ParseException e) {
                e.printStackTrace();
            }
            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            // String eventStartInMillis = (String) date_month_year;
            intent.putExtra("beginTime", startdate.getTime());
            String eventEndInMillis = null;
            intent.putExtra("endTime", eventEndInMillis);
            startActivity(intent);

        }

        public int getCurrentDayOfMonth() {
            return currentDayOfMonth;
        }

        private void setCurrentDayOfMonth(int currentDayOfMonth) {
            this.currentDayOfMonth = currentDayOfMonth;
        }

        public void setCurrentWeekDay(int currentWeekDay) {
            this.currentWeekDay = currentWeekDay;
        }

        public int getCurrentWeekDay() {
            return currentWeekDay;
        }

    }
}

还要提一下,我 DID NOT 必须在xml文件中添加GestureOverlayView。我不打算包含那些代码,因为我认为这不是必须的。它看起来像我的问题而没有GestureOverlayView部分。

答案 1 :(得分:1)

我发现检测手势的最简单方法是GestureOverlayView

你可以像这样使用它         GestureOverlayView gestureOverlayView = new GestureOverlayView(this);

    View inflate = getLayoutInflater().inflate(R.layout.main, null);
    gestureOverlayView.addView(inflate);
    gestureOverlayView.addOnGesturePerformedListener(this);
    gestureOverlayView.setGestureColor(Color.TRANSPARENT);
    gestureOverlayView.setUncertainGestureColor(Color.TRANSPARENT);
    GestureLibrary gestureLib = GestureLibraries.fromRawResource(this, 
           R.raw.gestures);
    if (!gestureLib.load()) {
        finish();
    }
    setContentView(gestureOverlayView);

手势调度员看起来像这样

@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
    for (Prediction prediction : predictions) {
        if (prediction.score > 1.0) {

            ControllerMain.getInstance().processSwipe(prediction.name);
            // Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT)
            // .show();
        }
    }
}

为了制作gestureLib,您应该使用手势构建器应用程序。您可以通过本手册Gestures Builder isn't installed on my emulator安装它。