安卓手势检测

3
我正在开发一款基于Android的应用程序,主要用于绘制形状。我希望用户在屏幕上进行手势操作,最接近手势的形状应该被绘制在屏幕上。
在我的应用程序中,我可以检测屏幕上执行的手势,例如圆形、直线、矩形等等,但是我的代码存在一些问题。它实际上可以检测手势并绘制相应的形状,但这只会发生一次。
例如,如果我在屏幕上画一条直线,则该直线会在我的视图中绘制出来,但之后如果我画圆形或矩形等,则手势被识别,但形状未被绘制。
以下是完整的代码:
package com.pck.ShapeMaker;

import java.util.ArrayList;

import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;

public class GestureDetection extends Activity {
    private GestureLibrary gLib;    
    private LinearLayout l1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gesture);
        l1 = (LinearLayout) findViewById(R.id.playArea);

        gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
        if (!gLib.load()) 
            finish();   
        GestureOverlayView gesturesView = (GestureOverlayView) findViewById(R.id.gestures);
        myGestureHandler handler = new myGestureHandler();      
        gesturesView.addOnGesturePerformedListener(handler);


    }

    class myGestureHandler implements OnGesturePerformedListener{

        public void onGesturePerformed(GestureOverlayView gestureView, Gesture gesture){
            ArrayList<Prediction> predictions = gLib.recognize(gesture);        


            if (predictions.size() > 0 && predictions.get(0).score > 1.0) {
                String action = predictions.get(0).name;
                if ("l".equals(action)) {
                    Line line = new Line(getApplicationContext(),20,230,200,230);
                    l1.addView(line);
                } else if ("r".equals(action)) {
                    Rectangle rect = new Rectangle(getApplicationContext());
                    l1.addView(rect);
                    Toast.makeText(getApplicationContext(), "rect", Toast.LENGTH_SHORT).show();
                } else if ("t".equals(action)) {
                    Triangle tri = new Triangle(getApplicationContext(), 300,300,250, 350, 350, 350);
                    l1.addView(tri);
                    Toast.makeText(getApplicationContext(), "trianlge", Toast.LENGTH_SHORT).show();
                }else if ("c".equals(action)) {
                    Circle c1 = new Circle(getApplicationContext(),50,50,30);
                    l1.addView(c1);
                    Toast.makeText(getApplicationContext(), "circle", Toast.LENGTH_SHORT).show();
                }else if ("d".equals(action)) {
                     Toast.makeText(getApplicationContext(), "diamond", Toast.LENGTH_SHORT).show();
                }
            }




            }

    }
}

你解决了你的问题吗?我也有同样的问题。 - LoveMeow
1个回答

0

看起来你的代码将在LinearLayout中产生越来越多的视图。这是你想要的吗?如果不是,您可以尝试在添加新视图之前从布局中删除过时的视图。问题可能是第一个视图添加后布局中没有空间容纳后续视图。

此外,像这样动态添加视图似乎是绘制图形的奇怪方法。为什么不定义一个单独的自定义视图来覆盖所有类型的绘画,并在XML中静态地包含它?您的onGesturePerformed()方法将调用您的视图的某些方法来指定要执行的绘画类型,然后调用invalidate()以触发重新绘制。您的视图的onDraw()方法将随后绘制刚指定的任何图形。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接