Android photo color change

activity_main.xml:

<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=".MainActivity" 
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <SeekBar 
        android:id="@+id/sb1"
        android:max="255"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
     <SeekBar 
        android:id="@+id/sb2"
         android:max="255"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
      <SeekBar 
        android:id="@+id/sb3"
         android:max="255"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
      
       <SeekBar 
        android:id="@+id/sb4"
         android:max="255"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>
see the MainActivity.java:

package com.example.photocolorchange;


import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
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,sb4;
	private Bitmap alertBitmap;
	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);
		
		final Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.shuishou);
		 alertBitmap = Bitmap.createBitmap(bitmap1.getWidth(),bitmap1.getHeight(),bitmap1.getConfig());
		 paint = new Paint();
		 canvas = new Canvas(alertBitmap);
		final Matrix matrix = new Matrix();
		final ColorMatrix cm = new ColorMatrix();
		paint.setColorFilter(new ColorMatrixColorFilter(cm));
		
		
		
		paint.setAntiAlias(true);
		canvas.drawBitmap(bitmap1, matrix, paint);
		
		iv1.setImageBitmap(alertBitmap);
		
		sb1 = (SeekBar) findViewById(R.id.sb1);
		sb2 = (SeekBar) findViewById(R.id.sb2);
		sb3 = (SeekBar) findViewById(R.id.sb3);
		sb4 = (SeekBar) findViewById(R.id.sb4);
		//RED
		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,1,0,0,0,
						0,0,1,0,0,
						0,0,0,1,0,
				});
				paint.setColorFilter(new ColorMatrixColorFilter(cm));
				canvas.drawBitmap(bitmap1, matrix, paint);
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				
			}
		});
		//GREEN
		sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int progress = seekBar.getProgress();
				cm.set(new float[]{
						1,0,0,0,0,
						0,progress/128.0f,0,0,0,
						0,0,1,0,0,
						0,0,0,1,0,
				});
				paint.setColorFilter(new ColorMatrixColorFilter(cm));
				canvas.drawBitmap(bitmap1, matrix, paint);
				iv1.setImageBitmap(alertBitmap);
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				
			}
		});
	//BLUE
	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,0,progress/128.0f,0,0,
					0,0,0,1,0,
			});
			paint.setColorFilter(new ColorMatrixColorFilter(cm));
			canvas.drawBitmap(bitmap1, matrix, paint);
			iv1.setImageBitmap(alertBitmap);
			
		}
		
		
		
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			
		}
	});
	
	//sensitometry
	sb4.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,progress/128.0f,0,0,0,
					0,0,progress/128.0f,0,0,
					0,0,0,1,0,
			});
//			cm.setSaturation(progress/128.0f);  //saturability
			paint.setColorFilter(new ColorMatrixColorFilter(cm));
			canvas.drawBitmap(bitmap1, matrix, paint);
			iv1.setImageBitmap(alertBitmap);
			
		}
		
		
		
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			
		}
	});
		
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

final result:

Android photo color  change

Android photo color change,布布扣,bubuko.com

Android photo color change

上一篇:前台返回json数据的常用方式+常用的AJAX请求后台数据方式


下一篇:Int32.Parse, Convert.ToInt32,Int32.TryParse三者的区别