Android -- 图片处理, 画画板,缩放,旋转,平移,镜面,倒影,图片合成,颜色处理...

原文:http://www.cnblogs.com/xj626852095/p/3645055.html

1. 画画板 示例代码

public class MainActivity extends Activity {
    private ImageView iv;
    private Bitmap baseBitmap;
    private Canvas canvas;
    private Paint paint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        paint = new Paint();
        paint.setStrokeWidth(5);
        paint.setColor(Color.GREEN);
        
        // 创建一个可以被修改的bitmap
        baseBitmap = Bitmap.createBitmap(320, 359,
                Bitmap.Config.ARGB_8888);
        System.out.println("图宽度:"+iv.getWidth());
        System.out.println("图宽高:"+iv.getHeight());
        canvas = new Canvas(baseBitmap);
        canvas.drawColor(Color.WHITE);

        // 知道用户手指在屏幕上移动的轨迹
        iv.setOnTouchListener(new OnTouchListener() {
            //定义手指开始位置的坐标
            int startX;
            int startY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: // 手指第一次接触屏幕
                    System.out.println("手指按下");
    
                    startX = (int) event.getX();
                    startY = (int) event.getY();
                    break;
                case MotionEvent.ACTION_MOVE: // 手指在屏幕上滑动
                    System.out.println("手指移动");
                    int newX  = (int) event.getX();
                    int newY = (int) event.getY();
                    
                    canvas.drawLine(startX,startY, newX, newY, paint);
                    //重新更新画笔的开始位置.
                    startX = (int) event.getX();
                    startY = (int) event.getY();
                    iv.setImageBitmap(baseBitmap);
                    break;
                case MotionEvent.ACTION_UP:// 手指离开屏幕

                    break;
                }

                return true;
            }
        });

    }

    public void save(View view) {
        try {
            File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
            FileOutputStream stream = new FileOutputStream(file);
            baseBitmap.compress(CompressFormat.JPEG, 100, stream);
            stream.close();
            Toast.makeText(this, "保存图片成功", 1).show();
            
            //模拟一个消息 通知系统sd卡被重新挂载了.
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
            intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
            sendBroadcast(intent);
            
            
        } catch (Exception e) {
            Toast.makeText(this, "保存图片失败", 1).show();
            e.printStackTrace();
        }
        
        
        
    }

}

2. 图片缩放

public class MainActivity extends Activity {
    private ImageView iv_src;
    private ImageView iv_dest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_src = (ImageView) findViewById(R.id.iv_src);
        iv_src.setImageBitmap(BitmapFactory.decodeFile("/sdcard/tom.png"));
        iv_dest = (ImageView) findViewById(R.id.iv_dest);

    }

    public void click(View view) {
        Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/tom.png");
        Bitmap baseBitmap = Bitmap.createBitmap(bitmap.getWidth()*2,
                bitmap.getHeight()/2, bitmap.getConfig());
        Canvas canvas = new Canvas(baseBitmap);
        Matrix matrix = new Matrix();
    /*    matrix.setValues(new float[] {
                2, 0, 0,
                0, 0.5f, 0,
                0, 0, 1
            });*/
        matrix.setScale(2.0f, 0.5f);
        canvas.drawBitmap(bitmap, matrix, new Paint());
        iv_dest.setImageBitmap(baseBitmap);
    }
}

3. 图片旋转

Matrix matrix = new Matrix();

matrix.setRotate(15);

canvas.drawBitmap(bmp, matrix, paint);

消除锯齿

paint.setAntiAlias(true);  

指定圆心的旋转

matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2);

4. 图片平移

matrix.setTranslate(1.5f,-10);

5. 镜面效果

matrix.setScale(-1, 1);

matrix.postTranslate(bmp.getWidth(),0);

6. 倒影

matrix.setScale(1, -1);

matrix.postTranslate(0, bmp.getHeight());

7. 图片合成

public class MainActivity extends Activity {
    private ImageView iv;    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        
        //第一张图
          Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
        //第二张图
          Bitmap addbitMap = BitmapFactory.decodeResource(getResources(), R.drawable.a3);
        
        //可修改的位图 及 画板
          Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        Canvas canvas = new Canvas(alterBitmap);
        
        Paint point = new Paint();
        point.setColor(Color.BLACK);
        point.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_OVER));//合成模式
        
        //将第一张图和第二张图画到画板的可修改位图上
          canvas.drawBitmap(bitmap, new Matrix(), point);                
        canvas.drawBitmap(addbitMap, new Matrix(), point);
        
        iv.setImageBitmap(alterBitmap);                
    }
}

 8. 颜色处理,RGB颜色, 亮度, 饱和度

亮度处理

ColorMatrix cm = new ColorMatrix();
float contrast = 2;
cm.set(new float[] {
    contrast, 0, 0, 0, 0,
    0, contrast, 0, 0, 0,
    0, 0, contrast, 0, 0,
    0, 0, 0, 1, 0 });
paint.setColorFilter(new ColorMatrixColorFilter(cm));

RGB 及 饱和度 

public class MainActivity extends Activity {

    private ImageView iv1;
    private SeekBar sb1;
    private SeekBar sb2;
    private SeekBar sb3;
    private SeekBar sb4;
            
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        iv1 = (ImageView) findViewById(R.id.iv_1);
        sb1 = (SeekBar) findViewById(R.id.seekBar1);
        sb2 = (SeekBar) findViewById(R.id.seekBar2);
        sb3 = (SeekBar) findViewById(R.id.seekBar3);
        sb4 = (SeekBar) findViewById(R.id.seekBar4);
        sb1.setMax(255);
        sb2.setMax(255);
        sb3.setMax(255);
        sb4.setMax(255);
        
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.top);
        final Bitmap baseBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        
        final Canvas canvas = new Canvas(baseBitmap);
        final Matrix matrix = new Matrix();
        final Paint paint = new Paint();
        
        canvas.drawBitmap(bitmap, matrix, paint);
        iv1.setImageBitmap(baseBitmap);
        
        //调节R 红色
        sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                int ret = seekBar.getProgress();
                if(ret!=0)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.set(new float[]{
                        ret/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(baseBitmap, matrix, paint);
                    iv1.setImageBitmap(baseBitmap);
                }
            }
        });
        
        //调节G 绿色
        sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                int ret = seekBar.getProgress();
                if(ret!=0)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.set(new float[]{
                        1,0,0,0,0,    
                        0,ret/128.0f,0,0,0,    
                        0,0,1,0,0,    
                        0,0,0,1,0,    
                    });
                    paint.setColorFilter(new ColorMatrixColorFilter(cm));
                    canvas.drawBitmap(baseBitmap, matrix, paint);
                    iv1.setImageBitmap(baseBitmap);
                }
            }
        });
        
        //调节B 蓝色
        sb3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                
            }            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                int ret = seekBar.getProgress();
                if(ret!=0)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.set(new float[]{
                        1,0,0,0,0,    
                        0,1,0,0,0,    
                        0,0,ret/128.0f,0,0,    
                        0,0,0,1,0,    
                    });
                    paint.setColorFilter(new ColorMatrixColorFilter(cm));
                    canvas.drawBitmap(baseBitmap, matrix, paint);
                    iv1.setImageBitmap(baseBitmap);
                }
            }
        });
        
        //调节 饱和度
        sb4.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {                
            }            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {                
            }            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                int ret = seekBar.getProgress();
                if(ret!=0)
                {
                    ColorMatrix cm = new ColorMatrix();
                    cm.setSaturation(ret/128.0f);
                    paint.setColorFilter(new ColorMatrixColorFilter(cm));
                    canvas.drawBitmap(baseBitmap, matrix, paint);
                    iv1.setImageBitmap(baseBitmap);
                }
            }
        });
        
    }

}

转载于:https://www.cnblogs.com/an-ly/p/5333374.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值