您的位置:首页 > 移动开发 > Android开发

简单的图片处理器(设置色相,亮度,饱和度)

2015-04-07 15:21 232 查看
首先前台布局就是选择的一张图片,还有三个设置三种颜色参数的seekbar

<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"

    android:orientation="vertical" >

    <LinearLayout android:layout_height="0dp"

        android:layout_width="fill_parent"

        android:layout_weight="1">

        <ImageView

            android:id="@+id/imageView1"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            android:src="@drawable/touxiang" />

        

    </LinearLayout>

    

     <LinearLayout android:layout_height="50dp"

        android:layout_width="fill_parent">

         <SeekBar

             android:id="@+id/rotateseekBar"

             android:layout_width="match_parent"

             android:layout_marginLeft="20dp"

             android:layout_marginRight="20dp"

             android:layout_height="wrap_content"

             android:layout_weight="1"
android:layout_gravity="center_vertical"/>

         

     </LinearLayout>

    

      <LinearLayout android:layout_height="50dp"

        android:layout_width="fill_parent">

         <SeekBar

             android:id="@+id/saturationseekBar"

             android:layout_width="match_parent"

             android:layout_marginLeft="20dp"

             android:layout_marginRight="20dp"

             android:layout_height="wrap_content"

             android:layout_weight="1"
android:layout_gravity="center_vertical"/>

         

     </LinearLayout>

    

      

       <LinearLayout android:layout_height="50dp"

        android:layout_width="fill_parent">

         <SeekBar

             android:id="@+id/lumseekBar"

             android:layout_width="match_parent"

             android:layout_marginLeft="20dp"

             android:layout_marginRight="20dp"

             android:layout_height="wrap_content"

             android:layout_weight="1"
android:layout_gravity="center_vertical"/>

         

     </LinearLayout>

    

     

</LinearLayout>

接下来在主布局的后台监听seekbar的changed事件,对图片进行调整处理

package com.example.imageswitch;

import ImageUtil.BitmapSwitch;

import android.R.integer;

import android.os.Bundle;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.view.Menu;

import android.widget.ImageView;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends BitmapSwitch implements OnSeekBarChangeListener {

private ImageView contentImageView;
private SeekBar rotateBar, satutationBar, lumBar;
private static int MAX_VALUE = 255;
private static int MID_VALUE = 127;
//保存当前seekbar设置的值
private float rvalue,svalue,lvalue;
private Bitmap currentBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}

private void initView() {
contentImageView = (ImageView) findViewById(R.id.imageView1);
rotateBar = (SeekBar) findViewById(R.id.rotateseekBar);
satutationBar = (SeekBar) findViewById(R.id.saturationseekBar);
lumBar = (SeekBar) findViewById(R.id.lumseekBar);
currentBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.touxiang);

rotateBar.setOnSeekBarChangeListener(this);
satutationBar.setOnSeekBarChangeListener(this);
lumBar.setOnSeekBarChangeListener(this);
// 设置当前各种属性的值,和各种属性最大值
rotateBar.setMax(MAX_VALUE);
rotateBar.setProgress(MID_VALUE);
satutationBar.setMax(MAX_VALUE);
satutationBar.setProgress(MID_VALUE);
lumBar.setMax(MAX_VALUE);
lumBar.setProgress(MID_VALUE);
contentImageView.setImageBitmap(currentBitmap);
}

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

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// Bitmap bitmap=
switch (seekBar.getId()) {
case R.id.rotateseekBar:
rvalue=(float) ((progress-MID_VALUE)*1.0F/MID_VALUE*180);//将seekbar获取的值转化
break;

case R.id.saturationseekBar:
svalue=(float) ((progress)*1.0F/MID_VALUE);
break;

case R.id.lumseekBar:
lvalue=progress*1.0F/MID_VALUE;
break;
}
//currentBitmap=handBitmap(currentBitmap, rvalue, svalue, lvalue);
contentImageView.setImageBitmap(handBitmap(currentBitmap, rvalue, svalue, lvalue));
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

}

最主要的是实现根据这些参数调整bitmap的工具类

public Bitmap handBitmap(Bitmap bitmap,float rotate,float saturation,float lum){
Bitmap newBitmap=bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas=new Canvas(newBitmap);
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
//0,1,2分别设置R,G,B三种色值
ColorMatrix ratateColorMatrix=new ColorMatrix();
ratateColorMatrix.setRotate(0, rotate);
ratateColorMatrix.setRotate(1, rotate);
ratateColorMatrix.setRotate(2, rotate);

ColorMatrix saturationColorMatrix=new ColorMatrix();
saturationColorMatrix.setSaturation(saturation);

//设置亮度--四个参数是R,G,B,透明度(1表示全透明)
ColorMatrix lumColorMatrix=new ColorMatrix();
lumColorMatrix.setScale(lum, lum, lum, 1);

//将三种效果融合
ColorMatrix allColorMatrix=new ColorMatrix();
allColorMatrix.postConcat(ratateColorMatrix);
allColorMatrix.postConcat(saturationColorMatrix);
allColorMatrix.postConcat(lumColorMatrix);

paint.setColorFilter(new ColorMatrixColorFilter(allColorMatrix));
canvas.drawBitmap(bitmap, 0, 0, paint);

return newBitmap;

}

结果返回一个新的bitmap,再将这个bitmap设置到imageview上,就实现了对图片的调整。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐