Saturday, January 11, 2025
Google search engine
HomeLanguagesJavaGravityView in Android

GravityView in Android

In this article, we are going to show the GravityView in android. In this article, we are going to see the gravity effect on an image. As we move our phone we will see different parts of the image. Here we will be using Horizontal ScrollView so we will be moving our phone horizontally. In the below video you can visualize how this is happening

Step by Step Implementation

Step 1: Create a new Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.  

Step 2: Add this into build.gradle file

implementation 'co.gofynd.library:gravity-view:1.0'

Step 3: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
 
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </HorizontalScrollView>
     
</RelativeLayout>


Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file 

if(!gravityView.deviceSupported()){
            // show the error / imageView
            Toast.makeText(GravityViewActivity.this,"Not Supported",Toast.LENGTH_LONG).show();
        }
        else
        {
          // if device is supported then we will add the image
          // in imageview and will show the image using gravity view
          gravityView.setImage(imageView,R.drawable.image).center();
}

Below is the code for the MainActivity.java file.

Java




import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
 
import androidx.appcompat.app.AppCompatActivity;
 
import co.gofynd.gravityview.GravityView;
 
public class MainActivity extends AppCompatActivity {
     
    GravityView gravityView;
    ImageView imageView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        imageView = findViewById(R.id.imageView);
        gravityView = GravityView.getInstance(this);
        if (!gravityView.deviceSupported()) {
            // show the error
            Toast.makeText(MainActivity.this, "Not Supported", Toast.LENGTH_LONG).show();
        } else {
            // if device is supported then we will add the image
            // in imageview and will show the image using gravity view
            gravityView.setImage(imageView, R.drawable.image).center();
        }
    }
 
    @Override
    protected void onStop() {
        super.onStop();
        gravityView.unRegisterListener();
    }
 
    @Override
 
    protected void onResume() {
        super.onResume();
        gravityView.registerListener();
    }
}


Output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments