当前位置:首页 > Android > 正文内容

Android图片颜色处理

jsc9年前 (2016-09-23)Android6153

你想做到跟美图秀秀一样可以处理自己的照片,美化自己的照片吗?其实你也可以自己做一个这样的软件,废话不多说了,直接上图,上代码了!

效果图如下:

没处理前:

1.png

处理之后:

2.png

MainActivity.java的代码如下:

package net.loonggg.test;
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.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
	private SeekBar sb1, sb2, sb3, sb4, sb5;
	private ImageView iv;
	private Bitmap bitmap, updateBitmap;
	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);
		sb1 = (SeekBar) findViewById(R.id.sb1);
		sb2 = (SeekBar) findViewById(R.id.sb2);
		sb3 = (SeekBar) findViewById(R.id.sb3);
		sb4 = (SeekBar) findViewById(R.id.sb4);
		sb5 = (SeekBar) findViewById(R.id.sb5);
		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b);
		updateBitmap = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), bitmap.getConfig());
		canvas = new Canvas(updateBitmap);
		paint = new Paint();
		final ColorMatrix cm = new ColorMatrix();
		paint.setColorFilter(new ColorMatrixColorFilter(cm));
		paint.setColor(Color.BLACK);
		paint.setAntiAlias(true);
		final Matrix matrix = new Matrix();
		canvas.drawBitmap(bitmap, matrix, paint);
		iv.setImageBitmap(updateBitmap);
		/**
		 * RGB三原色 红色值的设置
		 */
		sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int progress = seekBar.getProgress();
				cm.set(new float[] { progress / 128f, 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(bitmap, matrix, paint);
				iv.setImageBitmap(updateBitmap);
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}
		});
		/**
		 * RGB三原色 绿色值的设置
		 */
		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 / 128f, 0, 0, 0,// 绿色值
						0, 0, 1, 0, 0,// 蓝色值
						0, 0, 0, 1, 0 // 透明度
				});
				paint.setColorFilter(new ColorMatrixColorFilter(cm));
				canvas.drawBitmap(bitmap, matrix, paint);
				iv.setImageBitmap(updateBitmap);
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}
		});
		/**
		 * RGB三原色 蓝色值的设置
		 */
		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 / 128f, 0, 0,// 蓝色值
						0, 0, 0, 1, 0 // 透明度
				});
				paint.setColorFilter(new ColorMatrixColorFilter(cm));
				canvas.drawBitmap(bitmap, matrix, paint);
				iv.setImageBitmap(updateBitmap);
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}
		});
		/**
		 * RGB三原色 三个值都改变为设置饱和度(亮度)
		 */
		sb4.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int progress = seekBar.getProgress();
				cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 红色值
						0, progress / 128f, 0, 0, 0,// 绿色值
						0, 0, progress / 128f, 0, 0,// 蓝色值
						0, 0, 0, 1, 0 // 透明度
				});
				paint.setColorFilter(new ColorMatrixColorFilter(cm));
				canvas.drawBitmap(bitmap, matrix, paint);
				iv.setImageBitmap(updateBitmap);
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}
		});
		/**
		 * RGB三原色 设置透明度
		 */
		sb5.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, 1, 0, 0,// 蓝色值
						0, 0, 0, progress / 128f, 0 // 透明度
				});
				paint.setColorFilter(new ColorMatrixColorFilter(cm));
				canvas.drawBitmap(bitmap, matrix, paint);
				iv.setImageBitmap(updateBitmap);
			}
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
			}
		});
	}
}

布局代码如下:

<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:background="#CDCDCD"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色:"
android:textColor="#FF0000"
android:textSize="24sp" />
<SeekBar
android:id="@+id/sb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色:"
android:textColor="#00FF00"
android:textSize="24sp" />
<SeekBar
android:id="@+id/sb2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色:"
android:textColor="#0000FF"
android:textSize="24sp" />
<SeekBar
android:id="@+id/sb3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="饱和度:"
android:textColor="#000000"
android:textSize="16.5sp" />
<SeekBar
android:id="@+id/sb4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度:"
android:textColor="#000000"
android:textSize="16.5sp" />
<SeekBar
android:id="@+id/sb5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="256"
android:progress="128" />
</LinearLayout>
</LinearLayout>

到这里就完了,看明白了吗?

扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/141.html

分享给朋友:

“Android图片颜色处理” 的相关文章

Android开发中Bundle用法 包裹数据

SDK里是这样描述:A mapping from String values to various Parcelable types。  它帮助我将数据打包传入intent里面,为使用这些数据提供了便利。protected void onListItemClick (L...

Android中使用PULL方式解析XML文件

Android中使用PULL方式解析XML文件

 Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素 并触发相应事件。跟SAX不同的是, Pull解析器产生的事件是一个数字,而非方法,因此可以使用一个s...

Android权限问题整理

Android权限系统非常庞大,我们在Android系统中做任何操作都需要首先获取Android系统权限,本文记录了所有的Android权限问题,整理一下分享给大家。访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES读取或写入登记check-in数...

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

注:(图中每一个条目和图标都是由代码动态生成) 代码动态布局,并需要为每一个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon" ...

Android应用加入微信分享

Android应用加入微信分享

一、申请你的AppIDhttp://open.weixin.qq.com/  友情提示:推荐使用eclipse打包软件最后一步的MD5值去申请AppID二、官网下载libammsdk.jar包http://open.weixin.qq.com/download/?lang=zh_...

Android 学习之 开源项目PullToRefresh的使用

Android 学习之 开源项目PullToRefresh的使用

首先 下载 Android-PullToRefresh-master 下载地址  https://github.com/chrisbanes/Android-PullToRefresh 下载之...