Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android photo color change

Android photo color change

編輯:關於Android編程

activity_main.xml:



    

    
     
      
      
       

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:


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved