Open In App

How to Implement TNImage Library in Android?

Last Updated : 29 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android OS was developed by Android Inc. which Google bought in 2005. In this article, we are going to show the implementation of the TNImage library. Here we are going to put an image on the main screen and when we touch the image then we can move the image from one position to another on the main screen. This feature can be used when we have many images on the main screen then we can move images from one place to another and can set the position of images again. Let’s see the implementation of this feature.

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

Add it in your root ‘build.gradle’ at the end of repositories:

allprojects {

 repositories {

  …

  maven { url ‘https://jitpack.io’ }

 }

}

Include the following dependency.

compile ‘com.github.AmeerHamzaaa:TNImageView-Android:0.1.2’

Step 3: Working with the activity_main.xml file

Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".RotateCoin">
  
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="Click Here"/>
  
    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/ic_coin"
        android:id="@+id/coin"/>
  
</LinearLayout>


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 

Java




package com.anni.shareimage;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
  
import java.util.Random;
  
public class RotateCoin extends AppCompatActivity {
  
    public static final Random RANDOM = new Random();
    private ImageView coin;
    private Button btn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotate_coin);
        coin = (ImageView) findViewById(R.id.coin);
        btn = (Button) findViewById(R.id.btn);
            
        // click on the button to flip
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                flipCoin();
            }
        });
    }
  
    private void flipCoin() {
        Animation fadeOut = new AlphaAnimation(1, 0);
        fadeOut.setInterpolator(new AccelerateInterpolator());
            
        // set the duration
        fadeOut.setDuration(1000);
        fadeOut.setFillAfter(true);
        fadeOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
  
            }
            @Override
            public void onAnimationEnd(Animation animation) {
                
                // add the images when rotating
                coin.setImageResource(RANDOM.nextFloat() > 0.5f ? R.drawable.ic_coin : R.drawable.ic_coins);
                Animation fadeIn = new AlphaAnimation(0, 1);
                fadeIn.setInterpolator(new DecelerateInterpolator());
                    
                // set the duration
                fadeIn.setDuration(3000);
                fadeIn.setFillAfter(true);
                coin.startAnimation(fadeIn);
            }
  
            @Override
            public void onAnimationRepeat(Animation animation) {
  
            }
        });
  
        coin.startAnimation(fadeOut);
    }
  
}


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads