最近在做图片相关的应用,所以就各方积累到一些常用的操作,一般来说会有多种方式来实现这一功能,比如

  1. 采用色度变换
  2. 采用ColorMatrix颜色矩阵
  3. 采用对像素点的直接操作

    等等,今天就复习一下第一种方式吧,虽然比较单一,得到的结果类型也比较少。

相比较于常见的图片风格变换,一般我们就是换个色彩度,饱和度,亮度等等,这里也恰恰是这个方式

编码思路:

  • 抽象出图片操作工具类
  • 创建一个用于操作的Bitmap对象
  • 使用画布Canvas,画笔Paint
  • 调色处理,参数控制
  • 画出Bitmap并返回
  • 被相关方法调用,得到结果

下面直接上代码吧

首先是布局

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="320dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:text="色    度"
            android:textSize="18dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <SeekBar
            android:id="@+id/hueBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:text="饱和度"
            android:textSize="18dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <SeekBar
            android:id="@+id/saturationBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:text="亮    度"
            android:textSize="18dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <SeekBar
            android:id="@+id/lumBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            />
    </LinearLayout>

</LinearLayout>

接下来是工具操作类的相关方法

public static Bitmap handleImageLikePS(Bitmap bp,float hue,float saturation,float lum){

        Bitmap bitmap=Bitmap.createBitmap(bp.getWidth(), bp.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas=new Canvas(bitmap);
        Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);

        ColorMatrix hueMatrix=new ColorMatrix();
        hueMatrix.setRotate(0, hue);
        hueMatrix.setRotate(1, hue);
        hueMatrix.setRotate(2, hue);

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

        ColorMatrix lumMatrix=new ColorMatrix();
        lumMatrix.setScale(lum,lum,lum,1);

        ColorMatrix imageMatrix=new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bp, 0, 0, paint);//此处如果换成bitmap就会仅仅调用一次,图像将不能被编辑

        return bitmap;
    }

然后是使用类

package com.example.colormatrixdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{

    private Bitmap bitmap;
    private ImageView imageview;
    private SeekBar hueBar,saturationBar,lumBar;

    private float mHue,mSaturation ,mLum;
    private static int MAXVALUE=255,MIDVALUE=127;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.masuo);
        imageview=(ImageView) findViewById(R.id.imageview);
        hueBar=(SeekBar) findViewById(R.id.hueBar);
        saturationBar=(SeekBar) findViewById(R.id.saturationBar);
        lumBar=(SeekBar) findViewById(R.id.lumBar);

        hueBar.setOnSeekBarChangeListener(this);
        saturationBar.setOnSeekBarChangeListener(this);
        lumBar.setOnSeekBarChangeListener(this);

        hueBar.setMax(MAXVALUE);
        hueBar.setProgress(MIDVALUE);
        saturationBar.setMax(MAXVALUE);
        saturationBar.setProgress(MIDVALUE);
        lumBar.setMax(MAXVALUE);
        lumBar.setProgress(MIDVALUE);

        imageview.setImageBitmap(bitmap);
    }

    @Override
    public void onProgressChanged(SeekBar seekbar, int progress, boolean arg2) {
        switch(seekbar.getId()){
        case R.id.hueBar:
            mHue=(progress-MIDVALUE)*1.0F/MIDVALUE*180;
            break;
        case R.id.saturationBar:
            mSaturation=progress*1.0F/MIDVALUE;
            break;
        case R.id.lumBar:
            mLum=progress*1.0F/MIDVALUE;
            break;
        }
        imageview.setImageBitmap(ImageTools.handleImageLikePS(bitmap, mHue, mSaturation, mLum));
    }

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

    }

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

    }

}

然后运行程序,你就可以通过对滑动条的调节来对图像做相关的处理变换了。


注意:

在工具类的方法中最后要对传进去的参数做处理,而不是我们自己声明的bitmap,否则我们将得不到我们实时的图片效果。因为我们的bitmap仅仅是作为一个操作的对象模型,真正需要操作的是我们的bp参数。


总结:在处理图像有许多的方法,尤其是对图像用像素点的方式效果最多,可以呈现多种多样的效果。如老照片,浮雕,底片等等;而采用颜色矩阵也是一种好经典的操作方法。这些很值得我们学习,这样我们就可以是的我们的应用呈现出更加绚丽的色彩及效果咯!

Android图片色彩变幻的更多相关文章

  1. android 图片占用内存与什么有关

    android 图片占用内存与什么有关 原文链接:http://blog.csdn.net/zjl5211314/article/details/7041813 在开发手机应用的时候,内存是有限的,那 ...

  2. Android 图片加载库Glide 实战(二),占位符,缓存,转换自签名高级实战

    http://blog.csdn.net/sk719887916/article/details/40073747 请尊重原创 : skay <Android 图片加载库Glide 实战(一), ...

  3. Android 图片内存优化与图片压缩

    1. 对图片本身进行操作 尽量不要使用 setImageBitmap.setImageResource. BitmapFactory.decodeResource 来设置一张大图,因为这些方法在完成 ...

  4. Android图片缓存之Lru算法

    前言: 上篇我们总结了Bitmap的处理,同时对比了各种处理的效率以及对内存占用大小.我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发 ...

  5. C#图片色彩的纠正-上

    WPF(C#)图片色彩的纠正-上 WPF(C#)图片色彩的纠正-下 前言 对图片进行色彩的纠正,其实与WPF是没有什么关系的,为什么标题又是“WPF(C#)图片色彩的纠正”呢,因为这些图片色彩的纠正功 ...

  6. Android图片处理

    相信做Android开发的小伙伴对于Android图片压缩.裁剪一定有很深的印象,今天我将带领大家一起学习一下这个看着高深莫测的知识,以便再以后的学习.工作中可以帮助到大家. 首先我们看一下这个问题出 ...

  7. Android图片缓存之Glide进阶

    前言: 前面学习了Glide的简单使用(Android图片缓存之初识Glide),今天来学习一下Glide稍微复杂一点的使用. 图片缓存相关博客地址: Android图片缓存之Bitmap详解 And ...

  8. Android图片缓存之初识Glide

    前言: 前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架.技术本身就要不断的更迭,从最初的自己使用SoftReference实现自己的图片缓存,到后来做电商项目自己的实 ...

  9. Android图片缓存之Bitmap详解

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...

随机推荐

  1. # electron-vue 尝试做个网易云音乐

    当跑起来electron第一刻 我发现这个浏览器头是不是有点丑 是不是可以隐藏起来呢,答案当然是可以的 src/main/index.js mainWindow = new BrowserWindow ...

  2. find 命令查找文件,文件夹

    查找文件 find / -name httpd.conf 查找文件夹 find / -name "*1526*" -type d, 其中双引号里的东西表示文件夹名字包含" ...

  3. 如何去掉修改Joomla、joomlart及其模版版权、标志、图标的方法

    Joomla是遵循GNU通用公共授权(GPL)的自由软件,我们虽然不推荐将Joomla的所有版权删除,但有些必要的信息还是需要修改的,下面以JoomlArt.com 的JA_teline_iii_v2 ...

  4. 全网代理公开ip爬取(隐藏元素混淆+端口加密)

    简述 本次要爬取的网站是全网代理,貌似还是代理ip类网站中比较有名的几个之一,其官网地址: http://www.goubanjia.com/. 对于这个网站的爬取是属于比较悲剧的,因为很久之前就写好 ...

  5. C++框架_之Qt的窗口部件系统的详解-上

    C++框架_之Qt的窗口部件系统的详解-上 第一部分概述 第一次建立helloworld程序时,曾看到Qt Creator提供的默认基类只有QMainWindow.QWidget和QDialog三种. ...

  6. Java内存泄漏分析系列之一:使用jstack定位线程堆栈信息

    原文地址:http://www.javatang.com 前一段时间上线的系统升级之后,出现了严重的高CPU的问题,于是开始了一系列的优化处理之中,现在将这个过程做成一个系列的文章. 基本概念 在对J ...

  7. Android开发技巧——Camera拍照功能

    本篇是我对开发项目的拍照功能过程中,对Camera拍照使用的总结.由于camera2是在api level 21(5.0.1)才引入的,而Camera到6.0仍可使用,所以暂未考虑camera2. 文 ...

  8. Objective-C方法与函数的区别

    Objective-C方法与函数的区别 方法是唯对象所有 函数是不依赖于对象存在的 方法 函数 - (void)test; void test(); 方法是以减号 - 开头 - 类型要用()括起来 - ...

  9. Objective-C方法的实现

    Objective-C的方法被两种数据类型描述:一个是选择子(SEL),它用来描述方法的名称;另一个是实现(IMP),它用来描述方法被调用时实际执行的代码(它们基本上只是C函数的指针). 类似于SEL ...

  10. DoesNotExist at /admin/

    DoesNotExist at /admin/ User has no account. Request Method: GET Request URL: http://127.0.0.1:8000/ ...