Native Mobile App Development With Java

Zoom In /Out With Finger Touch Android java Example

07-Oct-2024

Zoom In /Out With Finger Touch Android java Example

1/  activity_main.xml file




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CDDC39"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>



<TextView
android:layout_width="match_parent"
android:layout_marginTop="40dp"
android:layout_height="wrap_content"
android:text="The name of my nation is Bangladesh. Its location in eastern South Asia borders the Bay of Bengal in the south, India, Myanmar, and simply India to the east and west. Its total area, including waterways, is 147,570 square kilometers. It won its freedom following a bloody liberation conflict. Three million men and women have sacrificed their lives for independence. The newly formed country faced many hurdles and harsh times after gaining independence. It does, however, rise well. Today, Bangladesh ranks among the best countries in South Asia to live in. In our country, there are many breathtaking locations to visit. Bangladesh is a small country. But it has a sizable population. About 120 million individuals call this little nation home. The population density in this area is high. Approximately 800 people call a square kilometer home. Bangladesh is primarily agricultural, with farmers making up most of the population. It is still a charming and serene country, though. Bangladeshis are extraordinarily kind to one another."
android:textStyle="bold|italic"
android:layout_margin="10dp"
android:textColor="@color/black"
android:id="@+id/zoomText"
android:textSize="30sp"
android:background="#CDDC39"
android:gravity="center"/>

</ScrollView>

</LinearLayout>
</RelativeLayout>



2/ MainActivity.java file −




package com.example.zoominzoomout;

import android.os.Bundle;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
TextView zoomText;
final static float MOVE_THRESHOLD = 200;
float ratio = 1.0f;
int baseDistance;
float baseRatio;

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

zoomText = findViewById(R.id.zoomText);
zoomText.setTextSize(ratio * 20);
zoomText.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getPointerCount() == 2) {
int action = event.getAction();
int mainAction = action & MotionEvent.ACTION_MASK;

if (mainAction == MotionEvent.ACTION_POINTER_DOWN) {
baseDistance = getDistance(event);
baseRatio = ratio;
} else {
float scale = (getDistance(event) - baseDistance) / MOVE_THRESHOLD;
float factor = (float) Math.pow(2, scale);
ratio = Math.min(1024.0f, Math.max(0.1f, baseRatio * factor));
zoomText.setTextSize(ratio * 20);
}
}
return true;
}

private int getDistance(MotionEvent event) {
int dx = (int) (event.getX(0) - event.getX(1));
int dy = (int) (event.getY(0) - event.getY(1));
return (int) Math.sqrt(dx * dx + dy * dy);
}
}

OUTPUT.



Comments