• Android之图片颜色调节


    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
                     
                }
            });
             
             
        }
     
         
     
    }
    

      

    执行效果图如下:

    不努力,还要青春干什么?
  • 相关阅读:
    OpenGL编程 基础篇(二)Sierpinski垫片
    PAT1045. Favorite Color Stripe
    OpenGL编程 基础篇(一)
    百练1145:Tree Summing
    百练1745:Divisibility
    百练1321:棋盘问题
    Scrapy架构及其组件之间的交互
    MongoDB快速入门
    Redis快速入门
    Memcache所有方法及参数详解
  • 原文地址:https://www.cnblogs.com/caidupingblogs/p/4932367.html
Copyright © 2020-2023  润新知