Easy Tutorial
❮ C The Structure Of The Parameter Verilog Competition Hazard ❯

8.3.10 Paint API - ColorFilter (Color Filter) (2-3)

Category Android Basic Tutorial

Introduction to This Section:

>

In the previous section, we discussed the first subclass of the Android Paint API, ColorMatrixColorFilter (Color Matrix Color Filter), which we believe broadened everyone's horizons in Android image processing. In this section, we will explore its second subclass: LightingColorFilter (Lighting Color Filter). First, let's take a look at the official API documentation: LightingColorFilter. There isn't much in the documentation, but the key points are here:

The general idea is: a color filter that can be used to simulate simple lighting effects. The constructor has two parameters, one for multiplying the original image's RGB values, and one for adding to the result obtained before! In fact, the calculation method is nothing more than: (RGB value * mul + Add) % 255, to get the new RGB value, where % is the modulus operation, and in the whole process, Alpha does not participate in the change! Next, let's write an example to verify it!


1. Code Example:

Running Effect Picture :

Implementation Code :

First, 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="Change" />

</RelativeLayout>

Then our MainActivity.java, 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;
        }
    }
}

Well, the demonstration of the use of LightingColorFilter is over~


3. Download of This Section's Code

LightingColorFilterDemo.zip


Summary of This Section:

>

Well, this section demonstrated a basic usage of LightingColorFilter to simulate a simple lighting effect and achieve a simple image processing effect. Okay, that's it for this section, thank you~

-

❮ C The Structure Of The Parameter Verilog Competition Hazard ❯