Paint API - ColorFilter (color filter) (2-3)


Introduction to this section:

In the previous section we explained the ColorFilter (color filter) in Paint API in Android The first subclass: ColorMatrixColorFilter (color matrix color filter), I believe it has broadened everyone's horizons in Android image processing. In this section, we will study its second subclass: LightingColorFilter (lighting color filter), let’s start with the previous one Official API document: LightingColorFilter, there are not many things in the document, the key is here:

1.png

It roughly means: a color filter that can be used to simulate simple lighting Effect, the constructor method has two parameters, one Used to multiply the RPG value of the original image, and one is added to the result obtained previously! In fact, the calculation method is nothing more than: (RGB value * mul + Add) % 255, so as to get the new RPG value. The % here is the remainder. In addition, Alpha does not change during the whole process. Be part of the change! Let’s write an example below to verify the verification!


1. Code example:

Running renderings:

2.gif

Implementation code:

First is a simple layout: activity_main.xml:

 <RelativeLayout 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"
    android:padding="5dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/img_meizi"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:src="@mipmap/img_meizi" />

    <EditText
        android:id="@+id/edit_mul"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/img_meizi"
        android:text="0" />

    <EditText
        android:id="@+id/edit_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_mul"
        android:text="0" />


    <Button
        android:id="@+id/btn_change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/img_meizi"
        android:layout_below="@id/img_meizi"
        android:text="变化" /></RelativeLayout>

Then is our MainActivty.java, It’s also very simple:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView img_meizi;
    private EditText edit_mul;
    private EditText edit_add;
    private Button btn_change;
    private Bitmap mBitmap;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_meizi);
        bindViews();
    }

    private void bindViews() {
        img_meizi = (ImageView) findViewById(R.id.img_meizi);
        edit_mul = (EditText) findViewById(R.id.edit_mul);
        edit_add = (EditText) findViewById(R.id.edit_add);
        btn_change = (Button) findViewById(R.id.btn_change);

        btn_change.setOnClickListener(this);

    }


    private Bitmap ProcessImage(Bitmap bp,int mul,int add){
        Bitmap bitmap = Bitmap.createBitmap(bp.getWidth(),bp.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColorFilter(new LightingColorFilter(mul,add));
        canvas.drawBitmap(bp,0,0,paint);
        return bitmap;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_change:
                int mul = Integer.parseInt(edit_mul.getText().toString());
                int add = Integer.parseInt(edit_add.getText().toString());
                img_meizi.setImageBitmap(ProcessImage(mBitmap,mul,add));
                break;
        }
    }
}

Okay, the demonstration of using LightingColorFilter is completed~


3. Download the code of this section

LightingColorFilterDemo.zip


Summary of this section:

Well, this section demonstrates a basic usage of LightingColorFilter, which is used to simulate simple lighting effects. To achieve simple image processing effects, okay, that’s it for this section, thank you~