}
三原色调整界面
可以大致分析一下,如果要调整一张图片,要经过这三个步骤:
-
获取一张图片
-
对图片进行一系列修改
-
返回一张图片
因此,不妨设计一个工具类,来进行图像相关的处理,这样可以提高代码的复用性。
public class ImageUtils {
public static Bitmap handleImageEffect(Bitmap bitmap,float hue,float saturation,float lum){
//由于不能直接在原图上修改,所以创建一个图片,设定宽度高度与原图相同。为32位ARGB图片
Bitmap currentBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//创建一个和原图相同大小的画布
Canvas canvas = new Canvas(currentBitmap);
//创建笔刷并设置抗锯齿
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//色相ColorMatrix
ColorMatrix hueMatrix = new ColorMatrix();
hueMatrix.setRotate(0,hue);
hueMatrix.setRotate(1,hue);
hueMatrix.setRotate(2,hue);
//饱和度ColorMatrix
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(saturation);
//亮度ColorMatrix
ColorMatrix lumMatrix = new ColorMatrix();
lumMatrix.setScale(lum,lum,lum,1);
//将三种效果融合起来
ColorMatrix imageMatrix = new ColorMatrix();
imageMatrix.postConcat(hueMatrix);
imageMatrix.postConcat(saturationMatrix);
imageMatrix.postConcat(lumMatrix);
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
canvas.drawBitmap(bitmap,0,0,paint);
return currentBitmap;
}
}
这里,用到了Canvas类,canvas类是一个画布类,后面进行的操作都会在画布上进行,而不是在原图上。建立了三个ColorMatrix,进行了不同方面的操作,并用postConcat方法将这些Matrix进行融合。通过setColorFilter方法来修改paint的相关属性,然后在canvas上将图片绘制出来。最后将修改后的图片返回出来。
回到我们的界面。设计是用三个Seekbar来分别控制色相、饱和度、亮度三个属性。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”>
<ImageView
android:id=“@+id/image_view”
android:layout_width=“300dp”
android:layout_height=“300dp”
android:scaleType=“centerCrop”
android:layout_marginTop=“24dp”
android:layout_marginBottom=“24dp”
android:layout_centerHorizontal=“true”/>
<SeekBar
android:id=“@+id/seekbar_hue”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_margin=“10dp”
android:layout_below=“@id/image_view”/>
<SeekBar
android:id=“@+id/seekbar_saturation”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_margin=“10dp”
android:layout_below=“@id/seekbar_hue”/>
<SeekBar
android:id=“@+id/seekbar_lum”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_margin=“10dp”
android:layout_below=“@id/seekbar_saturation”/>
然后编辑PrimaryColorActivity。这里我们定义了一个最大值及中间值,可以让它从中间值开始变化,然后通过一系列方法来分别获取色相、饱和度、亮度的值。
public cl