setGesture() 在屏幕外显示手势:Android

发布于 2024-11-29 19:11:10 字数 1170 浏览 0 评论 0原文

我正在开发一个 Android 应用程序,我需要在其中向用户显示保存的手势。我尝试使用 GestureOverlayView 对象的 setGesture() 函数来执行此操作,但它在屏幕外显示手势。在 Android 模拟器 2.1 中,屏幕的左上角仅可见一小部分。 这是我使用过的代码..

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.savegesture);
    gestureoverlay = (GestureOverlayView) findViewById(R.id.gestures_overlay2);
    mOldGesture = (Gesture) getIntent().getExtras().get("gesture");
    mDoneButton = findViewById(R.id.done);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    gestureoverlay.setLayoutParams(new FrameLayout.LayoutParams(dm.widthPixels,dm.heightPixels,0x50));
    gestureoverlay.setGesture(mOldGesture);
    gestureoverlay.invalidate();
}

“savegesture.xml”中定义的android GestureOverlayView如下:-

<android.gesture.GestureOverlayView android:scaleType="center" android:layout_gravity="center"
    android:id="@+id/gestures_overlay2" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gestureStrokeType="multiple" />

我不知道问题是什么,但它不会在屏幕上显示手势....

I am developing an android app in which I need to display the saved gesture to the user. I have tried doing it using the GestureOverlayView object's setGesture() function but it displays the gesture well outside the screen. Only small portion is visible in the left top corner of the screen in the Android emulator 2.1.
Here is the code that I have used..

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.savegesture);
    gestureoverlay = (GestureOverlayView) findViewById(R.id.gestures_overlay2);
    mOldGesture = (Gesture) getIntent().getExtras().get("gesture");
    mDoneButton = findViewById(R.id.done);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    gestureoverlay.setLayoutParams(new FrameLayout.LayoutParams(dm.widthPixels,dm.heightPixels,0x50));
    gestureoverlay.setGesture(mOldGesture);
    gestureoverlay.invalidate();
}

The android GestureOverlayView defined in "savegesture.xml" is as follows:-

<android.gesture.GestureOverlayView android:scaleType="center" android:layout_gravity="center"
    android:id="@+id/gestures_overlay2" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gestureStrokeType="multiple" />

I don't know what the problem is, but it won't show the gesture ON the screen....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居人 2024-12-06 19:11:10

虽然这是一个 3 年前的问题,但我仍然会添加一个答案。

该手势出现在 GestureOverlayView 之外,因为它是在 onCreate 中设置的。必须在计算布局尺寸后设置它。

ViewTreeObserver 可用于了解何时测量了布局。

ViewTreeObserver vto = gestureoverlay.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        gestureoverlay.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        gestureoverlay.setGesture(mOldGesture);
    } 
});

Although this is a 3 year old question, I'll still add an answer.

The gesture appears outside the GestureOverlayView because it's been set in onCreate. It must be set after the layout measurements have been computed.

ViewTreeObserver can be used to know when the layout has been measured.

ViewTreeObserver vto = gestureoverlay.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        gestureoverlay.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        gestureoverlay.setGesture(mOldGesture);
    } 
});
泛泛之交 2024-12-06 19:11:10

尽管这是一个 5 年前的问题,我仍然会添加一个答案,因为与此相比,批准的答案效率较低:

Use view.post() toexecute code after the view has was returned 。

gestureoverlay.post(new Runnable() {
   @Override
   public void run() {
      gestureoverlay.setGesture(mOldGesture);
   }
});

Altough this is a 5 year old question, I'll still add an answer, because the approved answer is inefficient in comparison to this:

Use view.post() to execute code after the view has been loaded.

gestureoverlay.post(new Runnable() {
   @Override
   public void run() {
      gestureoverlay.setGesture(mOldGesture);
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文