简单的图片处理器(设置色相,亮度,饱和度)
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上,就实现了对图片的调整。。。
<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上,就实现了对图片的调整。。。
相关文章推荐
- as3.0 用代码设置“调整颜色”滤镜--------即:亮度、对比度、饱和度、色相的调整
- as3.0 用代码设置“调整颜色”滤镜--------即:亮度、对比度、饱和度、色相的调整
- Flex 亮度、对比度、饱和度和色相的设置
- as3.0 用代码设置“调整颜色”滤镜--------即:亮度、对比度、饱和度、色相的调整
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- cocos2d中更改CCSprite的饱和度、色相、亮度等属性
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- AS3 使用TweenMax来改变显示对象亮度、对比度、饱和度、色相
- android开发 修改图片色调饱和度、色相、亮度处理
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- 【其它】颜色的知识--亮度、色相、饱和度、对比度
- 颜色的原理,三基色原理以及HLS(色相、亮度、饱和度)原理
- cocos2dx 更改sprite色相 饱和度 亮度
- Android 图片特效(一):色相、饱和度与亮度
- [ActionScript 3.0] 亮度、对比度、饱和度、色相的调整
- 调整影片的亮度、对比度、饱和度、色相
- PS图层混合算法之五(饱和度,色相,颜色,亮度)
- android图像处理系列之三--图片色调饱和度、色相、亮度处理