Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Photo的顏色調節,Photo顏色調節

Photo的顏色調節,Photo顏色調節

編輯:關於android開發

Photo的顏色調節,Photo顏色調節


package net.hnjdzy.imagecolor;

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 iv;
    private SeekBar sb1,sb2,sb3; 
    private Canvas canvas;
    private Paint paint;
    private ColorMatrix cm;
    private Matrix matrix;
    private Bitmap b1,b2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        iv = (ImageView) findViewById(R.id.imageView1);
        sb1 = (SeekBar) findViewById(R.id.seekBar1);
        sb2 = (SeekBar) findViewById(R.id.seekBar2);
        sb3 = (SeekBar) findViewById(R.id.seekBar3);  
        b1 = BitmapFactory.decodeResource(getResources(), R.drawable.fq);
        b2 = Bitmap.createBitmap(b1.getWidth(), b1.getHeight(), b1.getConfig());
        canvas = new Canvas(b2);
        paint = new Paint();
        paint.setColor(Color.BLACK);
        matrix = new Matrix();
        paint.setAntiAlias(true);
        
        //顏色矩陣
        cm = new ColorMatrix();
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        
        canvas.drawBitmap(b1, matrix, paint);
        iv.setImageBitmap(b2);
        //改變RGB中R的值
        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(b1, matrix, paint);
                iv.setImageBitmap(b2);
                
            }
            
            @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
                
            }
        });
        //改變RGB中G的值
        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(b1, matrix, paint);
                iv.setImageBitmap(b2);
                
            }
            
            @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
                
            }
        });
        //改變RGB中B的值
         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(b1, matrix, paint);
                iv.setImageBitmap(b2);
                
            }
            
            @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
                
            }
        });
        
        
    }

    

}

 

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