如今,许多社交媒体应用程序提供了许多过滤器,我们可以使用这些过滤器来使应用程序中的图像更美丽,更有吸引力。这种功能通常在Instagram,Snapchat和许多社交媒体应用程序中看到。在本文中,我们将介绍Android中类似于Instagram的过滤器部分的实现。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,在其中显示ImageView,并在下面提供不同类型的图像过滤选项。单击每个图像滤镜后,该滤镜将应用于我们的原始图像。下面给出了一个示例GIF,以了解我们将在本文中做些什么。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:添加依赖项和JitPack存储库
导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。
implementation ‘com.github.mukeshsolanki:photofilter:1.0.2’
将JitPack存储库添加到您的构建文件中。将其添加到allprojects {}部分中存储库末尾的根build.gradle中。
allprojects {
repositories {
…
maven { url “https://jitpack.io” }
}
}
添加此依赖项后,同步您的项目,现在我们将继续执行它。
步骤3:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
Java
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.mukesh.image_processing.ImageProcessor;
public class MainActivity extends AppCompatActivity {
// creating a bitmap for our original
// image and all image filters.
Bitmap bitmap;
// creating a variable for image view.
ImageView oneIV, twoIV, threeIV, fourIV, fiveIV, sixIV, sevenIV, eightIV, nineIV, tenIV, originalIV;
Bitmap oneBitMap, twoBitMap, threeBitmap, fourBitMap, fiveBitMap, sixBitMap, sevenBitMap, eightBitMap, nineBitMap, tenBitMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// creating a variable for our image processor.
ImageProcessor processor = new ImageProcessor();
// initializing bitmap with our image resource.
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
// initializing image views for our filters
// and original image on which we will
// be applying our filters.
oneIV = findViewById(R.id.idIVOne);
twoIV = findViewById(R.id.idIVTwo);
threeIV = findViewById(R.id.idIVThree);
fourIV = findViewById(R.id.idIVFour);
fiveIV = findViewById(R.id.idIVFive);
sixIV = findViewById(R.id.idIVSix);
sevenIV = findViewById(R.id.idIVSeven);
eightIV = findViewById(R.id.idIVEight);
nineIV = findViewById(R.id.idIVNine);
tenIV = findViewById(R.id.idIVTen);
originalIV = findViewById(R.id.idIVOriginalImage);
// below line is use to add tint effect to our original
// image bitmap and storing that in one bitmap.
oneBitMap = processor.tintImage(bitmap, 90);
// after storing it to one bitmap
// we are setting it to imageview.
oneIV.setImageBitmap(oneBitMap);
// below line is use to apply gaussian blur effect
// to our original image bitmap.
twoBitMap = processor.applyGaussianBlur(bitmap);
twoIV.setImageBitmap(twoBitMap);
// below line is use to add sepia toing effect
// to our original image bitmap.
threeBitmap = processor.createSepiaToningEffect(bitmap, 1, 2, 1, 5);
threeIV.setImageBitmap(threeBitmap);
// below line is use to apply saturation
// filter to our original image bitmap.
fourBitMap = processor.applySaturationFilter(bitmap, 3);
fourIV.setImageBitmap(fourBitMap);
// below line is use to apply snow effect
// to our original image bitmap.
fiveBitMap = processor.applySnowEffect(bitmap);
fiveIV.setImageBitmap(fiveBitMap);
// below line is use to add gray scale
// to our image view.
sixBitMap = processor.doGreyScale(bitmap);
sixIV.setImageBitmap(sixBitMap);
// below line is use to add engrave effect
// to our image view.
sevenBitMap = processor.engrave(bitmap);
sevenIV.setImageBitmap(sevenBitMap);
// below line is use to create a contrast
// effect to our image view.
eightBitMap = processor.createContrast(bitmap, 1.5);
eightIV.setImageBitmap(eightBitMap);
// below line is use to add shadow effect
// to our original bitmap.
nineBitMap = processor.createShadow(bitmap);
nineIV.setImageBitmap(nineBitMap);
// below line is use to add flea
// effect to our image view.
tenBitMap = processor.applyFleaEffect(bitmap);
tenIV.setImageBitmap(tenBitMap);
// below line is use to call on click
// listener for our all image filters.
initializeOnCLickListerns();
}
private void initializeOnCLickListerns() {
oneIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on clicking on each filter we are
// setting that filter to our original image.
originalIV.setImageBitmap(oneBitMap);
}
});
twoIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(twoBitMap);
}
});
threeIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(threeBitmap);
}
});
fourIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(fourBitMap);
}
});
fiveIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(fiveBitMap);
}
});
sixIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(sixBitMap);
}
});
sevenIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(sevenBitMap);
}
});
eightIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(eightBitMap);
}
});
nineIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(nineBitMap);
}
});
tenIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(tenBitMap);
}
});
}
}
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.mukesh.image_processing.ImageProcessor;
public class MainActivity extends AppCompatActivity {
// creating a bitmap for our original
// image and all image filters.
Bitmap bitmap;
// creating a variable for image view.
ImageView oneIV, twoIV, threeIV, fourIV, fiveIV, sixIV, sevenIV, eightIV, nineIV, tenIV, originalIV;
Bitmap oneBitMap, twoBitMap, threeBitmap, fourBitMap, fiveBitMap, sixBitMap, sevenBitMap, eightBitMap, nineBitMap, tenBitMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// creating a variable for our image processor.
ImageProcessor processor = new ImageProcessor();
// initializing bitmap with our image resource.
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
// initializing image views for our filters
// and original image on which we will
// be applying our filters.
oneIV = findViewById(R.id.idIVOne);
twoIV = findViewById(R.id.idIVTwo);
threeIV = findViewById(R.id.idIVThree);
fourIV = findViewById(R.id.idIVFour);
fiveIV = findViewById(R.id.idIVFive);
sixIV = findViewById(R.id.idIVSix);
sevenIV = findViewById(R.id.idIVSeven);
eightIV = findViewById(R.id.idIVEight);
nineIV = findViewById(R.id.idIVNine);
tenIV = findViewById(R.id.idIVTen);
originalIV = findViewById(R.id.idIVOriginalImage);
// below line is use to add tint effect to our original
// image bitmap and storing that in one bitmap.
oneBitMap = processor.tintImage(bitmap, 90);
// after storing it to one bitmap
// we are setting it to imageview.
oneIV.setImageBitmap(oneBitMap);
// below line is use to apply gaussian blur effect
// to our original image bitmap.
twoBitMap = processor.applyGaussianBlur(bitmap);
twoIV.setImageBitmap(twoBitMap);
// below line is use to add sepia toing effect
// to our original image bitmap.
threeBitmap = processor.createSepiaToningEffect(bitmap, 1, 2, 1, 5);
threeIV.setImageBitmap(threeBitmap);
// below line is use to apply saturation
// filter to our original image bitmap.
fourBitMap = processor.applySaturationFilter(bitmap, 3);
fourIV.setImageBitmap(fourBitMap);
// below line is use to apply snow effect
// to our original image bitmap.
fiveBitMap = processor.applySnowEffect(bitmap);
fiveIV.setImageBitmap(fiveBitMap);
// below line is use to add gray scale
// to our image view.
sixBitMap = processor.doGreyScale(bitmap);
sixIV.setImageBitmap(sixBitMap);
// below line is use to add engrave effect
// to our image view.
sevenBitMap = processor.engrave(bitmap);
sevenIV.setImageBitmap(sevenBitMap);
// below line is use to create a contrast
// effect to our image view.
eightBitMap = processor.createContrast(bitmap, 1.5);
eightIV.setImageBitmap(eightBitMap);
// below line is use to add shadow effect
// to our original bitmap.
nineBitMap = processor.createShadow(bitmap);
nineIV.setImageBitmap(nineBitMap);
// below line is use to add flea
// effect to our image view.
tenBitMap = processor.applyFleaEffect(bitmap);
tenIV.setImageBitmap(tenBitMap);
// below line is use to call on click
// listener for our all image filters.
initializeOnCLickListerns();
}
private void initializeOnCLickListerns() {
oneIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on clicking on each filter we are
// setting that filter to our original image.
originalIV.setImageBitmap(oneBitMap);
}
});
twoIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(twoBitMap);
}
});
threeIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(threeBitmap);
}
});
fourIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(fourBitMap);
}
});
fiveIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(fiveBitMap);
}
});
sixIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(sixBitMap);
}
});
sevenIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(sevenBitMap);
}
});
eightIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(eightBitMap);
}
});
nineIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(nineBitMap);
}
});
tenIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
originalIV.setImageBitmap(tenBitMap);
}
});
}
}
该项目中使用的图像将添加到drawable文件夹中。要浏览图像,请导航至应用程序> res> drawable,然后您将在该文件夹中找到该图像。现在运行该应用程序,然后查看以下代码的输出:
输出:
Note: The app will take some time to load though we are using bitmap and it takes time to decode the image to the bitmap.