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
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~
-
- [2.2.6 AbsoluteLayout]
[2.3.1 TextView Detailed Explanation]
[2.3.2 EditText Detailed Explanation]
[2.3.3 Button and ImageButton]
[2.3.4 ImageView]
[2.3.5 RadioButton & Checkbox]
[2.3.6 ToggleButton and Switch]
[2.3.7 ProgressBar]
[2.3.8 SeekBar]
[2.3.9 RatingBar]
[2.4.1 ScrollView]
[2.4.2 Date & Time Components (Part 1)]
[2.4.3 Date & Time Components (Part 2)]
[2.4.4 Adapter Basics]
[2.4.5 ListView Simple and Practical Usage]
[2.4.6 BaseAdapter Optimization]
[2.4.7 ListView Focus Issues]
[2.4.8 ListView Checkbox Misalignment Issue Resolution]
[2.4.9 ListView Data Update Issues]
[2.5.0 Building a Reusable Custom BaseAdapter]
[2.5.1 ListView Item Multiple Layouts Implementation]
[2.5.2 GridView Basic Usage]
[2.5.3 Spinner Basic Usage]
[2.5.4 AutoCompleteTextView Basic Usage]
[2.5.5 ExpandableListView Basic Usage]
[2.5.6 ViewFlipper Basic Usage]
[2.5.7 Toast Basic Usage]
[2.5.8 Notification Detailed Explanation]
[2.5.9 AlertDialog Detailed Explanation]
[2.6.0 Other Common Dialogs Basic Usage]
[2.6.1 PopupWindow Basic Usage]
[2.6.2 Menu]
[2.6.3 ViewPager Simple Usage]
[2.6.4 DrawerLayout (Official Side-Slide Menu) Simple Usage]
[3.1.1 Event Handling Mechanism Based on Listening]
[3.2 Event Handling Mechanism Based on Callback]
[3.3浅Analysis of Handler Message Passing Mechanism]
[3.4 TouchListener vs OnTouchEvent + Multi-Touch]
[3.5 Listening to EditText Content Changes]
[3.6 Responding to System Setting Events (Configuration Class)]
[3.7 AsyncTask Asynchronous Tasks]
[3.8 Gestures]
[4.1.1 Activity Basics]
[4.1.2 Activity Introduction]
[4.1.3 Activity In-Depth]
[4.2.1 Service Introduction]
[4.2.2 Service Advanced]
[4.2.3 Service Mastery]
[4.3.1 BroadcastReceiver Trial]
[4.3.2 In-Depth Analysis of BroadcastReceiver]
[4.4.1 ContentProvider Exploration]
[4.4.2 Further Exploration of ContentProvider - Document Provider]
[4.5.1 Basic Use of Intent]
[4.5.2 Intent for Passing Complex Data]
[5.1 Fragment Overview]
[5.2.1 Fragment Detailed Explanation - Bottom Navigation Bar Implementation (Method 1)]
[5.2.2 Fragment Detailed Explanation - Bottom Navigation Bar Implementation (Method 2)]
[5.2.3 Fragment Detailed Explanation - Bottom Navigation Bar Implementation (Method 3)]
[5.2.4 Fragment Detailed Explanation - Bottom Navigation Bar + ViewPager Page Switching]
[5.2.5 Fragment Detailed Explanation - Simple Implementation of News (Shopping) App List Fragment]
[6.1 Data Storage and Access - File Storage Read and Write]
[6.2 Data Storage and Access - SharedPreferences Saving User Preferences]
[6.3.1 Data Storage and Access - First Encounter with SQLite Database]
[6.3.2 Data Storage and Access - Another Encounter with SQLite Database]
[7.1.1 Android Network Programming and Http Protocol Learning]
[7.1.2 Android Http Request and Response Headers Learning]
[7.1.3 Android HTTP Request Method: HttpURLConnection]
[7.1.4 Android HTTP Request Method: HttpClient]
[7.2.1 Android XML Data Parsing]
[7.2.2 Android JSON Data Parsing]
[7.3.1 Android File Upload]
[7.3.2 Android File Download (1)]
[7.3.3 Android File Download (2)]
[7.4 Android Calling WebService]
[7.5.1 WebView Basic Usage]
[7.5.2 WebView and JavaScript Interaction Basics]
[7.5.3 Notes on WebView After Android 4.4]
8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (I)
8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (II)
8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (III)
8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (IV)
8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (V)
8.3.10 Paint API - ColorFilter (Color Filter) (2-3)
8.3.14 Paint Several Enum/Constant Values and ShadowLayer Shadow Effect
8.3.17 Detailed Explanation of Canvas API (Part 2) - Collection of Clipping Methods
8.3.18 Detailed Explanation of Canvas API (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - First Encounter
8.4.4 Android Animation Collection - Property Animation - Revisited
[10.2 SmsManager (SMS Manager)](android-tutorial-smsmanager.html