欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android 图片的颜色处理实例代码

时间:2021-01-23 10:16:21|栏目:Android代码|点击:

仿造美图秀秀移动鼠标调整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"
  tools:context=".MainActivity" >

  <SeekBar
    android:id="@+id/sb1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="255" />

  <SeekBar
    android:id="@+id/sb2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="255" />

  <SeekBar
    android:id="@+id/sb3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="255" />

  <ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

效果如下:

逻辑部分代码如下:

package com.wuyudong.resize;

import android.os.Bundle;
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.view.Menu;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

  private ImageView iv1;
  private SeekBar sb1, sb2, sb3;
  
  private Bitmap alterBitmap;
  private Canvas canvas;
  private Paint paint;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    iv1 = (ImageView) findViewById(R.id.iv);

    // 设置第一个bitmap的图标
    final Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
        R.drawable.ic_launcher);

    // 新建一个bitmap
    alterBitmap = Bitmap.createBitmap(bitmap1.getWidth(),
        bitmap1.getHeight(), bitmap1.getConfig());

    // 以alterBitmap为模板新建画布
    canvas = new Canvas(alterBitmap);
    // 新建画笔并设置属性
    paint = new Paint();
    paint.setColor(Color.BLACK);

    // 新建矩阵
    final Matrix matrix = new Matrix();

    // 指定颜色矩阵
    final ColorMatrix cm = new ColorMatrix();
    
    /**
     * Set this colormatrix to identity:
     * [ 1 0 0 0 0  - red vector
     *  0 1 0 0 0  - green vector
     *  0 0 1 0 0  - blue vector
     *  0 0 0 1 0 ] - alpha vector
     */
    paint.setColorFilter(new ColorMatrixColorFilter(cm));

    paint.setAntiAlias(true);

    // 设置画布
    canvas.drawBitmap(bitmap1, matrix, paint);
    iv1.setImageBitmap(alterBitmap);

    sb1 = (SeekBar) findViewById(R.id.sb1);
    sb2 = (SeekBar) findViewById(R.id.sb2);
    sb3 = (SeekBar) findViewById(R.id.sb3);

    sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = seekBar.getProgress();
        cm.set(new float[]{
            progress/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(bitmap1, matrix, paint);
        iv1.setImageBitmap(alterBitmap);
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

      }

      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {

      }
    });
    sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
        int progress = seekBar.getProgress();
        cm.set(new float[]{
            progress/128.0f, 0, 0, 0, 0,
            0, 1, 0, 0, 0,
            0, 0, progress/128.0f, 0, 0,
            0, 0, 0, 1, 0,
        });
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bitmap1, matrix, paint);
        iv1.setImageBitmap(alterBitmap);
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

      }

      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {

      }
    });
    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/128.0f, 0, 0,
            0, 0, 0, 1, 0,
        });
        paint.setColorFilter(new ColorMatrixColorFilter(cm));
        canvas.drawBitmap(bitmap1, matrix, paint);
        iv1.setImageBitmap(alterBitmap);
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {

      }

      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {

      }
    });
  }

}

运行效果:

出处:http://www.cnblogs.com/wuyudong/

上一篇:android实现微信联合登录开发示例

栏    目:Android代码

下一篇:Android动态人脸检测的示例代码(脸数可调)

本文标题:Android 图片的颜色处理实例代码

本文地址:http://www.codeinn.net/misctech/49925.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有