Computer >> Máy Tính >  >> Lập trình >> Android

Cách sử dụng Gravity View cho Android

Trước khi đi vào ví dụ, chúng ta nên biết chế độ xem trọng lực trong android là gì. Chế độ xem trọng lực cho phép chúng tôi sử dụng các cảm biến chuyển động của thiết bị Android và cho phép người dùng cuối khám phá sản phẩm bằng cách xoay thiết bị của họ.

Ví dụ này trình bày Cách sử dụng Gravity View cho Android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Mở build.gradle và thêm phụ thuộc vào thư viện hỗ trợ thiết kế.

apply plugin: 'com.android.application'
android {
   packagingOptions {
      exclude 'META-INF/proguard/androidx-annotations.pro'
   }
   packagingOptions {
      exclude 'META-INF/DEPENDENCIES'
      exclude 'META-INF/LICENSE'
      exclude 'META-INF/LICENSE.txt'
      exclude 'META-INF/license.txt'
      exclude 'META-INF/NOTICE'
      exclude 'META-INF/NOTICE.txt'
      exclude 'META-INF/notice.txt'
      exclude 'META-INF/ASL2.0'
   }
   compileSdkVersion 28
   defaultConfig {
      applicationId "com.example.andy.myapplication"
      minSdkVersion 15
      targetSdkVersion 28
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
      release {
         minifyEnabled false
         proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
   }
}
dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])
   implementation 'com.android.support:appcompat-v7:28.0.0'
   implementation 'com.android.support.constraint:constraint-layout:1.1.3'
   implementation 'co.gofynd.library:gravity-view:1.0'
   testImplementation 'junit:junit:4.12'
   androidTestImplementation 'com.android.support.test:runner:1.0.2'
   androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Bước 3 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   android:id = "@+id/activity_main"
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content">
   <HorizontalScrollView
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:scrollbars = "none">
      <ImageView
         android:id = "@+id/imageView"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
   </HorizontalScrollView>
</RelativeLayout>

Trong đoạn mã trên, chúng ta đã sử dụng chế độ xem cuộn ngang để cuộn hình ảnh và chế độ xem hình ảnh để hiển thị hình ảnh trên màn hình.

Bước 4 - Thêm mã sau vào src / MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import co.gofynd.gravityview.GravityView;
public class MainActivity extends AppCompatActivity {
   GravityView gravityView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      ImageView imageView = findViewById(R.id.imageView);
      gravityView = GravityView.getInstance(this);
      boolean is_supported = gravityView.deviceSupported();
      if(is_supported) {
         gravityView
         .setImage(imageView,R.drawable.back)
         .center();
      }
   }
   @Override
   protected void onResume() {
      super.onResume();
      gravityView.registerListener();
   }
   @Override
   protected void onStop() {
      super.onStop();
      gravityView.unRegisterListener();
   }
}

Trong đoạn mã trên, chúng tôi đã lấy một hình ảnh từ drawable làm mặt sau, vì vậy hãy thêm một hình ảnh có thể cuộn vào drawable.

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ studio android, 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 Run Cách sử dụng Gravity View cho Android biểu tượng từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Cách sử dụng Gravity View cho Android

Trong màn hình trên được hiển thị chính giữa hình ảnh, bây giờ hãy di chuyển cạnh trái và cạnh phải sang thiết bị di động. Nó sẽ hiển thị đầu ra như hình dưới đây -

Cách sử dụng Gravity View cho Android

Cách sử dụng Gravity View cho Android