35 MobileComputing Prac6
35 MobileComputing Prac6
Title of LAB Assignment: To perform the animation on an image and to apply various
filters on an image.
Practical No. 6
Aim: To perform the animation on an image and to apply various filters on an image.
Description:
Animations in Android serve to enhance user interaction and visual storytelling within an application. By
applying various animations to images, developers can create a more engaging user experience. Here
are the key concepts and techniques involved in image animations:
Types of Animations:
Implementation of Animations:
Image filtering is the process of altering the visual characteristics of an image. Filters can be applied to
modify colors, enhance details, or create artistic effects. In Android, image filtering can be performed
using various techniques and libraries.
Types of Filters:
Color Filters: Modify the color properties of an image, including hue, saturation, and brightness.
Common types of color filters include:
Grayscale Filter: Converts color images into shades of gray, removing color information
and emphasizing form and texture.
Sepia Filter: Adds a warm, brown tone to images, giving them an antique or nostalgic
feel.
Invert Filter: Reverses the colors in an image, producing a negative effect.
Blur Filters: Soften the details in an image, which can be useful for creating backgrounds or
focusing attention on specific UI elements.
Gaussian Blur: A widely used technique that blurs an image based on a Gaussian
function, creating a smooth transition between colors.
Box Blur: A simpler form of blurring that averages pixels within a defined square area.
Image Processing Libraries: Various libraries such as OpenGL ES, RenderScript, or Android’s built-
in bitmap processing can be used to apply filters efficiently, especially for high-performance
applications.
Implementation of Filters:
Photo Editing Applications: Users can apply filters to enhance their images, offering features like
cropping, rotating, and color adjustments.
Social Media Apps: Instant filtering allows users to modify images before sharing them,
contributing to user engagement and content creation.
Artistic Effects: Filters can transform standard images into artistic representations, appealing to
users looking for creativity.
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35
1. Move.
2. Rotate.
3. Expand.
1. Brightness.
2. Darkness.
3. Grayscale.
Code:
MainActivity.java
package com.example.prac6;
import android.animation.ObjectAnimator;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
brightnessSeekBar = findViewById(R.id.brightnessSeekBar);
darknessSeekBar = findViewById(R.id.darknessSeekBar);
grayscaleSeekBar = findViewById(R.id.grayscaleSeekBar);
Button moveBtn = findViewById(R.id.moveBtn);
Button rotateBtn = findViewById(R.id.rotateBtn);
Button expandBtn = findViewById(R.id.expandBtn);
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
darknessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
applyEffects();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
grayscaleSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
applyEffects();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
// Move animation
moveBtn.setOnClickListener(v -> {
ObjectAnimator moveAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f);
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35
moveAnimator.setDuration(1000);
moveAnimator.start();
});
// Rotate animation
rotateBtn.setOnClickListener(v -> {
ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 180f);
rotateAnimator.setDuration(1000);
rotateAnimator.start();
});
// Expand animation
expandBtn.setOnClickListener(v -> {
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f);
scaleXAnimator.setDuration(1000);
scaleYAnimator.setDuration(1000);
scaleXAnimator.start();
scaleYAnimator.start();
});
}
// Darkness effect
float darknessValue = (float) darknessSeekBar.getProgress() / 100;
float adjustedBrightness = brightnessValue - darknessValue / 100f;
matrix.setScale(adjustedBrightness, adjustedBrightness, adjustedBrightness, 1);
// Grayscale effect
float grayscaleValue = (float) grayscaleSeekBar.getProgress() / 100;
ColorMatrix grayMatrix = new ColorMatrix();
grayMatrix.setSaturation(1 - grayscaleValue);
// Combine matrices
matrix.postConcat(grayMatrix);
}
}
activity_main.xml
<ImageView
android:id="@+id/imageView"
android:layout_width="306dp"
android:layout_height="362dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="66dp"
android:layout_marginEnd="71dp"
android:src="@drawable/img" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_below="@id/imageView"
android:layout_marginTop="20dp">
<Button
android:id="@+id/moveBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Move"
android:layout_weight="1"
android:layout_marginRight="8dp" />
<Button
android:id="@+id/rotateBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Rotate"
android:layout_weight="1"
android:layout_marginRight="8dp" />
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35
<Button
android:id="@+id/expandBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Expand"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_marginTop="113dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Brightness"
android:textSize="16sp" />
<SeekBar
android:id="@+id/brightnessSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="200"
android:progress="100" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Darkness"
android:textSize="16sp" />
<SeekBar
android:id="@+id/darknessSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Grayscale"
android:textSize="16sp" />
<SeekBar
android:id="@+id/grayscaleSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
</LinearLayout>
</RelativeLayout>
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35
Output:
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35
Name: Devendra Suresh Mourya SYMCA/A Roll No: 35