Android圖片色彩變換實(shí)現(xiàn)方法
最近在做圖片相關(guān)的應(yīng)用,所以就各方積累到一些常用的操作,一般來(lái)說(shuō)會(huì)有多種方式來(lái)實(shí)現(xiàn)這一功能,比如
1.采用色度變換
2.采用ColorMatrix顏色矩陣
3.采用對(duì)像素點(diǎn)的直接操作
等等,今天就復(fù)習(xí)一下第一種方式吧,雖然比較單一,得到的結(jié)果類(lèi)型也比較少。
相比較于常見(jiàn)的圖片風(fēng)格變換,一般我們就是換個(gè)色彩度,飽和度,亮度等等,這里也恰恰是這個(gè)方式
編碼思路:
•抽象出圖片操作工具類(lèi)
•創(chuàng)建一個(gè)用于操作的Bitmap對(duì)象
•使用畫(huà)布Canvas,畫(huà)筆Paint
•調(diào)色處理,參數(shù)控制
•畫(huà)出Bitmap并返回
•被相關(guān)方法調(diào)用,得到結(jié)果
下面直接上代碼吧
首先是布局
<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>
接下來(lái)是工具操作類(lèi)的相關(guān)方法
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就會(huì)僅僅調(diào)用一次,圖像將不能被編輯
return bitmap;
}
然后是使用類(lèi)
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
}
}
然后運(yùn)行程序,你就可以通過(guò)對(duì)滑動(dòng)條的調(diào)節(jié)來(lái)對(duì)圖像做相關(guān)的處理變換了。
注意:
在工具類(lèi)的方法中最后要對(duì)傳進(jìn)去的參數(shù)做處理,而不是我們自己聲明的bitmap,否則我們將得不到我們實(shí)時(shí)的圖片效果。因?yàn)槲覀兊腷itmap僅僅是作為一個(gè)操作的對(duì)象模型,真正需要操作的是我們的bp參數(shù)。
總結(jié):在處理圖像有許多的方法,尤其是對(duì)圖像用像素點(diǎn)的方式效果最多,可以呈現(xiàn)多種多樣的效果。如老照片,浮雕,底片等等;而采用顏色矩陣也是一種好經(jīng)典的操作方法。這些很值得我們學(xué)習(xí),這樣我們就可以是的我們的應(yīng)用呈現(xiàn)出更加絢麗的色彩及效果咯!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Base64位編碼與String字符串的相互轉(zhuǎn)換,Base64與Bitmap的相互轉(zhuǎn)換實(shí)例代碼
這篇文章主要介紹了Java Base64位編碼與String字符串的相互轉(zhuǎn)換,Base64與Bitmap的相互轉(zhuǎn)換實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android中AsyncTask異步任務(wù)使用詳細(xì)實(shí)例(一)
AsyncTask是Android提供的輕量級(jí)的異步類(lèi),可以直接繼承AsyncTask,在類(lèi)中實(shí)現(xiàn)異步操作,并提供接口反饋當(dāng)前異步執(zhí)行的程度(可以通過(guò)接口實(shí)現(xiàn)UI進(jìn)度更新),最后反饋執(zhí)行的結(jié)果給UI主線程,通過(guò)本文給大家介紹Android中AsyncTask異步任務(wù)使用詳細(xì)實(shí)例(一),需要的朋友參考下2016-02-02
ionic App 解決android端在真機(jī)上tab處于頂部的問(wèn)題
這篇文章主要介紹了ionic App 解決android端在真機(jī)上tab處于頂部的問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-06-06
解決NDK開(kāi)發(fā)中Eclipse報(bào)錯(cuò)Unresolved inclusion jni.h的最終解決方法(已測(cè))
這篇文章主要介紹了解決NDK開(kāi)發(fā)中Eclipse報(bào)錯(cuò)Unresolved inclusion jni.h的最終方法,需要的朋友可以參考下2016-12-12
Android Notification使用方法總結(jié)
這篇文章主要介紹了Android Notification使用方法總結(jié)的相關(guān)資料,這里提供了四種使用方法,需要的朋友可以參考下2017-09-09
Android Button的基本用法詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android Button的基本用法詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android通過(guò)XListView實(shí)現(xiàn)上拉加載下拉刷新功能
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)XListView實(shí)現(xiàn)上拉加載下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

