Thứ Năm, 20 tháng 9, 2018

Android - Cử chỉ

Học lập trình Android cung cấp các loại sự kiện màn hình cảm ứng đặc biệt như pinch, nhấn đúp, cuộn, nhấn và giữ liên tục. Đây là tất cả được gọi là cử chỉ.

Android cung cấp lớp GestureDetector để nhận các sự kiện chuyển động và cho chúng tôi biết rằng các sự kiện này tương ứng với cử chỉ hay không.

Để sử dụng nó, bạn cần phải tạo một đối tượng của Gesture Detector và sau đó mở rộng một lớp khác với Gesture Detector. Simple On Gesture Listener để hoạt động như một trình lắng nghe và ghi đè một số phương thức. Cú pháp của nó được đưa ra dưới đây.
GestureDetector myG;
myG = new GestureDetector(this,new Gesture());
   
class Gesture extends GestureDetector.SimpleOnGestureListener{
   public boolean onSingleTapUp(MotionEvent ev) {
   }
   
   public void onLongPress(MotionEvent ev) {
   }
   
   public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
   }
   
   public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
   }
}

Xử lý cử chỉ chụm

Android cung cấp lớp ScaleGestureDetector để xử lý các cử chỉ như pinch vv Để sử dụng nó, bạn cần khởi tạo một đối tượng của lớp này. Cú pháp của nó như sau:
ScaleGestureDetector SGD;
SGD = new ScaleGestureDetector(this,new ScaleListener());
Tham số đầu tiên là ngữ cảnh và tham số thứ hai là trình nghe sự kiện. Chúng ta phải định nghĩa trình nghe sự kiện và ghi đè lên một hàm OnTouchEvent để làm cho nó hoạt động. Cú pháp của nó được đưa ra dưới đây
public boolean onTouchEvent(MotionEvent ev) {
   SGD.onTouchEvent(ev);
   return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
   @Override
   public boolean onScale(ScaleGestureDetector detector) {
      float scale = detector.getScaleFactor();
      return true;
   }
}
Ngoài cử chỉ chụm, còn có các phương pháp khác có sẵn để thông báo thêm về các sự kiện chạm. Chúng được liệt kê dưới đây

Sr.NoPhương pháp & mô tả
1getEventTime ()

Phương thức này nhận được thời gian sự kiện của sự kiện hiện tại đang được xử lý ..
2getFocusX ()

Phương thức này nhận tọa độ X của tiêu điểm cử chỉ hiện tại.
3getFocusY ()

Phương thức này nhận tọa độ Y của tiêu điểm cử chỉ hiện tại.
4getTimeDelta ()

Phương thức này trả về chênh lệch thời gian tính bằng mili giây giữa sự kiện mở rộng được chấp nhận trước đó và sự kiện mở rộng hiện tại.
5isInProgress ()

Phương thức này trả về true nếu cử chỉ tỷ lệ đang được tiến hành ..
6onTouchEvent (Sự kiện MotionEvent)

Phương thức này chấp nhận MotionEvents và gửi các sự kiện khi thích hợp.

Thí dụ

Đây là một ví dụ minh họa việc sử dụng lớp ScaleGestureDetector. Nó tạo ra một ứng dụng cơ bản cho phép bạn phóng to và thu nhỏ thông qua pinch.

Để thử nghiệm với ví dụ này, bạn có thể chạy ứng dụng này trên thiết bị thực hoặc trong trình mô phỏng có bật màn hình cảm ứng.

Các bướcSự miêu tả
1Bạn sẽ sử dụng Android studio để tạo ứng dụng Android theo gói com.example.sairamkrishna.myapplication.
2Sửa đổi tệp src / MainActivity.java để thêm mã cần thiết.
3Sửa đổi res / layout / activity_main để thêm các thành phần XML tương ứng
4Chạy ứng dụng và chọn thiết bị Android đang chạy và cài đặt ứng dụng trên đó và xác minh kết quả
Sau đây là nội dung của tệp hoạt động chính đã sửa đổi src / MainActivity.java .
package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;

import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

public class MainActivity extends Activity {
   private ImageView iv;
   private Matrix matrix = new Matrix();
   private float scale = 1f;
   private ScaleGestureDetector SGD;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      iv=(ImageView)findViewById(R.id.imageView);
      SGD = new ScaleGestureDetector(this,new ScaleListener());
   }

   public boolean onTouchEvent(MotionEvent ev) {
      SGD.onTouchEvent(ev);
      return true;
   }

   private class ScaleListener extends ScaleGestureDetector.
      SimpleOnScaleGestureListener {
      
      @Override
      public boolean onScale(ScaleGestureDetector detector) {
         scale *= detector.getScaleFactor();
         scale = Math.max(0.1f, Math.min(scale, 5.0f));
         matrix.setScale(scale, scale);
         iv.setImageMatrix(matrix);
         return true;
      }
   }
}


Sau đây là nội dung sửa đổi của xml res / layout / activity_main.xml .
Ở đây abc biểu thị logo của hướng dẫn
<RelativeLayout 
   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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity" >
   
   <TextView android:text="Gestures  
      Example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:scaleType="matrix"
      android:layout_below="@+id/textView"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentBottom="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
</RelativeLayout>
Sau đây là nội dung của res / values ​​/ string.xml .
<resources>
   <string name="app_name>My Application</string>
</resources>
Sau đây là nội dung của tệp AndroidManifest.xml .
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sairamkrishna.myapplicationMainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
   
   </application>
</manifest>
Hãy thử chạy ứng dụng của bạn. Tôi cho rằng bạn đã kết nối thiết bị Android Mobile thực tế với máy tính của mình. Để chạy ứng dụng từ Android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Đầu ra mẫu phải như thế này
Học lập trình Android
Bây giờ chỉ cần đặt hai ngón tay trên màn hình Android, và tách chúng ra một phần và bạn sẽ thấy rằng hình ảnh Android đang phóng to. Nó được thể hiện trong hình dưới đây
Học lập trình Android
Bây giờ một lần nữa đặt hai ngón tay trên màn hình Android, và cố gắng đóng chúng lại và bạn sẽ thấy rằng hình ảnh Android hiện đang co lại. Nó được thể hiện trong hình dưới đây

Không có nhận xét nào:

Đăng nhận xét

Lập trình Android - RenderScript

Trong chương này, chúng ta sẽ tìm hiểu về Android RenderScript. Thông thường các ứng dụng trên Android được thiết kế để tiêu thụ tài nguyên ...