Android image color processing,

Source: Internet
Author: User

Android image color processing,

You can move the mouse to adjust the seekbar and adjust the color of the image.

The project layout is as follows:

<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"    android:orientation="vertical"    tools:context=".MainActivity" >    <SeekBar        android:id="@+id/sb1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="255" />    <SeekBar        android:id="@+id/sb2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="255" />    <SeekBar        android:id="@+id/sb3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="255" />    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

The effect is as follows:

The logic code is as follows:

Package com. wuyudong. resize; import android. OS. bundle; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. colorMatrix; import android. graphics. colorMatrixColorFilter; import android. graphics. matrix; import android. graphics. paint; import android. view. menu; import android. widget. imageView; import android. widget. seekBar; import android. widget. seekBar. onSeekBarChangeListener; public class MainActivity extends Activity {private ImageView iv1; private SeekBar sb1, sb2, sb3; private Bitmap alterBitmap; private Canvas canvas; private Paint paint; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); iv1 = (ImageView) findViewById (R. id. iv); // set the final bitmap bitmap1 = BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher); // creates a bitmap alterBitmap = Bitmap. createBitmap (bitmap1.getWidth (), bitmap1.getHeight (), bitmap1.getConfig (); // use alterBitmap as the template to create a canvas = new Canvas (alterBitmap ); // create a paint brush and set the property Paint = new paint (); paint. setColor (Color. BLACK); // create a Matrix final matrix Matrix = new Matrix (); // specify the color matrix final ColorMatrix cm = new ColorMatrix ();/*** Set this colormatrix to identity: * [1 0 0 0 0 0-red vector * 0 1 0 0 0-green vector * 0 0 1 0 0-blue vector * 0 0 0 1 0]-alpha vector */ paint. setColorFilter (new ColorMatrixColorFilter (cm); paint. setAntiAlias (true); // sets the canvas. drawBitmap (bitmap1, matrix, paint); iv1.setImageBitmap (alterBitmap); sb1 = (SeekBar) findViewById (R. id. sb1); sb2 = (SeekBar) findViewById (R. id. sb2); sb3 = (SeekBar) findViewById (R. id. sb3); sb1.setOnSeekBarChangeListener (new OnSeekBarChangeListener () {@ Override public void onStopTrackingTouch (SeekBar seekBar) {int progress = seekBar. getProgress (); cm. set (new float [] {progress/128.0f, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0,}); paint. setColorFilter (new ColorMatrixColorFilter (cm); canvas. drawBitmap (bitmap1, matrix, paint); transform (alterBitmap) ;}@ Override public void onStartTrackingTouch (SeekBar seekBar) {}@ Override public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {}}); sb2.setOnSeekBarChangeListener (new OnSeekBarChangeListener () {@ Override public void onStopTrackingTouch (SeekBar seekBar) {int progress = seekBar. getProgress (); cm. set (new float [] {progress/128.0f, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, progress/128.0f, 0, 0, 0, 0, 0, 1, 0,}); paint. setColorFilter (new ColorMatrixColorFilter (cm); canvas. drawBitmap (bitmap1, matrix, paint); transform (alterBitmap) ;}@ Override public void onStartTrackingTouch (SeekBar seekBar) {}@ Override public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {}}); sb3.setOnSeekBarChangeListener (new OnSeekBarChangeListener () {@ Override public void onStopTrackingTouch (SeekBar seekBar) {int progress = seekBar. getProgress (); cm. set (new float [] {1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, progress/128.0f, 0, 0, 0, 0, 0, 0, 1, 0,}); paint. setColorFilter (new ColorMatrixColorFilter (cm); canvas. drawBitmap (bitmap1, matrix, paint); transform (alterBitmap) ;}@ Override public void onStartTrackingTouch (SeekBar seekBar) {}@ Override public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser ){}});}}

Running effect:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.