本文作为动画引文,希望大家能够阅读下一篇:
导语
本文主要是写出一些简单的修图功能,用到关键技术是颜色矩阵 ColorMatrix类。老规矩,就先来看美丽的效果图吧!
咳咳。。。。虽然有些粗糙,但不妨碍了解技术。接下来,看代码如何实现。
实现效果图
代码实现
(1)XML代码参考:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aiyang.animationdemo.MainActivity"
android:padding="50dp"
android:orientation="vertical">
<TextView
android:id="@+id/hello"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@color/colorAccent"
android:gravity="center"
android:textColor="#fff"
android:text="切换" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/qq"/>
</LinearLayout>
qq图片附上,欢迎加群。
MainActivity代码参考:
public class MainActivity extends AppCompatActivity {
TextView textView;
int num = 0;//修图
ImageView image;
Bitmap mBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.hello);
image = findViewById(R.id.image);
mBitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();//Drawble转bitmap
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (num<5){
num++;
switch (num){
case 0:
btnGray();
break;
case 1:
btnBlack();
break;
case 2:
btnRemeiber();
break;
case 3:
btnWhite();
break;
case 4:
btnBaohe();
break;
}
setImageMatrix(mBitmap,image);
}else{
num = 0 ;
}
}
});
}
private float[] mColorMatrix=new float[20]; //设置20个颜色数值
/**
* 设置图片进行矩阵转换
* @param bitmap
*/
private void setImageMatrix(Bitmap bitmap, ImageView mImageView) {
//创建一个 Bitmap
Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
//设置颜色矩阵
ColorMatrix colorMatrix=new ColorMatrix();
colorMatrix.set(mColorMatrix);
//将Bitmap放在画板中,用画笔根据颜色过滤器重新绘制
Canvas canvas = new Canvas(bmp);
Paint paint=new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(bitmap,0,0, paint);
//重新展示
mImageView.setImageBitmap(bmp);
}
//灰色
public void btnGray(){
mColorMatrix[0]=0.33F;
mColorMatrix[1]=0.59F;
mColorMatrix[2]=0.11F;
mColorMatrix[3]=0;
mColorMatrix[4]=0;
mColorMatrix[5]=0.33F;
mColorMatrix[6]=0.59F;
mColorMatrix[7]=0.11F;
mColorMatrix[8]=0;
mColorMatrix[9]=0;
mColorMatrix[10]=0.33F;
mColorMatrix[11]=0.59F;
mColorMatrix[12]=0.11F;
mColorMatrix[13]=0;
mColorMatrix[14]=0;
mColorMatrix[15]=0;
mColorMatrix[16]=0;
mColorMatrix[17]=0;
mColorMatrix[18]=1;
mColorMatrix[19]=0;
}
//反差
public void btnBlack(){
mColorMatrix[0]=-1;
mColorMatrix[1]=0;
mColorMatrix[2]=0;
mColorMatrix[3]=1;
mColorMatrix[4]=1;
mColorMatrix[5]=0;
mColorMatrix[6]=-1;
mColorMatrix[7]=0;
mColorMatrix[8]=1;
mColorMatrix[9]=1;
mColorMatrix[10]=0;
mColorMatrix[11]=0;
mColorMatrix[12]=-1;
mColorMatrix[13]=1;
mColorMatrix[14]=1;
mColorMatrix[15]=0;
mColorMatrix[16]=0;
mColorMatrix[17]=0;
mColorMatrix[18]=-1;
mColorMatrix[19]=0;
}
//怀旧
public void btnRemeiber(){
mColorMatrix[0]=0.393F;
mColorMatrix[1]=0.769F;
mColorMatrix[2]=0.189F;
mColorMatrix[3]=0;
mColorMatrix[4]=0;
mColorMatrix[5]=0.349F;
mColorMatrix[6]=0.686F;
mColorMatrix[7]=0.168F;
mColorMatrix[8]=0;
mColorMatrix[9]=0;
mColorMatrix[10]=0.272F;
mColorMatrix[11]=0.534F;
mColorMatrix[12]=0.131F;
mColorMatrix[13]=0;
mColorMatrix[14]=0;
mColorMatrix[15]=0;
mColorMatrix[16]=0;
mColorMatrix[17]=0;
mColorMatrix[18]=1;
mColorMatrix[19]=0;
}
//去色
public void btnWhite(){
mColorMatrix[0]=1.5F;
mColorMatrix[1]=1.5F;
mColorMatrix[2]=1.5F;
mColorMatrix[3]=0;
mColorMatrix[4]=-1;
mColorMatrix[5]=1.5F;
mColorMatrix[6]=1.5F;
mColorMatrix[7]=1.5F;
mColorMatrix[8]=0;
mColorMatrix[9]=-1;
mColorMatrix[10]=1.5F;
mColorMatrix[11]=1.5F;
mColorMatrix[12]=1.5F;
mColorMatrix[13]=0;
mColorMatrix[14]=-1;
mColorMatrix[15]=0;
mColorMatrix[16]=0;
mColorMatrix[17]=0;
mColorMatrix[18]=1;
mColorMatrix[19]=0;
}
//饱和
public void btnBaohe(){
mColorMatrix[0]=1.438F;
mColorMatrix[1]=-0.122F;
mColorMatrix[2]=-0.016F;
mColorMatrix[3]=0;
mColorMatrix[4]=-0.03F;
mColorMatrix[5]=-0.062F;
mColorMatrix[6]=1.378F;
mColorMatrix[7]=-0.016F;
mColorMatrix[8]=0;
mColorMatrix[9]=0.05F;
mColorMatrix[10]=-0.062F;
mColorMatrix[11]=-0.122F;
mColorMatrix[12]=1.483F;
mColorMatrix[13]=0;
mColorMatrix[14]=-0.02F;
mColorMatrix[15]=0;
mColorMatrix[16]=0;
mColorMatrix[17]=0;
mColorMatrix[18]=1;
mColorMatrix[19]=0;
}
}