Android 仿美图秀秀颜色混合

来源:互联网 发布:施华蔻黑胶和银胶 知乎 编辑:程序博客网 时间:2024/06/08 04:05
<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"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="青----------------红" />    <SeekBar        android:id="@+id/skb_red"        android:progress="50"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="紫----------------绿" />    <SeekBar        android:id="@+id/skb_green"        android:progress="50"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="黄----------------蓝" />    <SeekBar        android:id="@+id/skb_blue"        android:progress="50"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>
public class MainActivity extends Activity implements OnSeekBarChangeListener {    private ImageView iv;    private SeekBar skbRed;    private SeekBar skbGreen;    private SeekBar skbBlue;    private float redPerent = 1;    private float greenPerent = 1;    private float bluePerent = 1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv = (ImageView) findViewById(R.id.iv);        skbRed = (SeekBar) findViewById(R.id.skb_red);        skbGreen = (SeekBar) findViewById(R.id.skb_green);        skbBlue = (SeekBar) findViewById(R.id.skb_blue);        skbRed.setOnSeekBarChangeListener(this);        skbGreen.setOnSeekBarChangeListener(this);        skbBlue.setOnSeekBarChangeListener(this);        // 给iv加载图片        Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/img_small_1.jpg");        iv.setImageBitmap(bitmap);    }    @Override    public void onProgressChanged(SeekBar seekBar, int progress,            boolean fromUser) {        // TODO Auto-generated method stub    }    @Override    public void onStartTrackingTouch(SeekBar seekBar) {        // TODO Auto-generated method stub    }    @Override    public void onStopTrackingTouch(SeekBar seekBar) {        // 释放的时候        int progress = seekBar.getProgress();// 0-100        float perent = progress / 50f;        Bitmap srcBitmap = BitmapFactory                .decodeFile("/mnt/sdcard/img_small_1.jpg");        // 1. 创建拷贝        Bitmap copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth(),                srcBitmap.getHeight(), srcBitmap.getConfig());        // 2. 将拷贝挂载到画布上        Canvas canvas = new Canvas(copyBitmap);        // 3. 需要画笔        Paint paint = new Paint();        if (seekBar == skbRed) {            redPerent = perent;        } else if (seekBar == skbGreen) {            greenPerent = perent;        } else if (seekBar == skbBlue) {            bluePerent = perent;        }        ColorMatrix cm = new ColorMatrix();        cm.set(new float[] { 1 * redPerent, 0, 0, 0, 0,// R-->红色-->(0-2)                0, 1 * greenPerent, 0, 0, 0,// G-->绿色                0, 0, 1 * bluePerent, 0, 0,// B-->蓝色                0, 0, 0, 1, 0 // A -->透明度        });        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);        paint.setColorFilter(filter);// 颜色过滤器        // 4. 对图像进行处理的参数配置        Matrix matrix = new Matrix();        // 5. 将原始图片按照自己的配置方式画到画布上        canvas.drawBitmap(srcBitmap, matrix, paint);        iv.setImageBitmap(copyBitmap);    }}

参考:Android图片处理