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)文章
Android使用fastjson庫(kù)解析json字符串實(shí)戰(zhàn)
fastjson是一個(gè)Java語(yǔ)言編寫(xiě)的高性能功能完善的JSON庫(kù),它采用一種“假定有序快速匹配”的算法,把JSON?Parse的性能提升到極致,是目前Java語(yǔ)言中最快的JSON庫(kù),Fastjson接口簡(jiǎn)單易用,已經(jīng)被廣泛使用在緩存序列化、協(xié)議交互、Web輸出、Android客戶(hù)端等多種應(yīng)用場(chǎng)景2023-11-115個(gè)Android開(kāi)發(fā)中比較常見(jiàn)的內(nèi)存泄漏問(wèn)題及解決辦法
本文主要介紹了5個(gè)Android開(kāi)發(fā)中比較常見(jiàn)的內(nèi)存泄漏問(wèn)題及解決辦法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02Android App開(kāi)發(fā)中創(chuàng)建Fragment組件的教程
這篇文章主要介紹了Android App開(kāi)發(fā)中創(chuàng)建Fragment的教程,Fragment是用以更靈活地構(gòu)建多屏幕界面的可UI組件,需要的朋友可以參考下2016-05-05Android中的Intent Filter匹配規(guī)則簡(jiǎn)介
這篇文章主要為大家詳細(xì)介紹了Android中的Intent Filter匹配規(guī)則,感興趣的小伙伴們可以參考一下2016-04-04Android中實(shí)現(xiàn)EditText圓角的方法
Android中實(shí)現(xiàn)EditText圓角的方法,需要的朋友可以參考一下2013-03-03Android使用setCustomTitle()方法自定義對(duì)話(huà)框標(biāo)題
Android有自帶的對(duì)話(huà)框標(biāo)題,但是不太美觀,如果要給彈出的對(duì)話(huà)框設(shè)置一個(gè)自定義的標(biāo)題,使用AlertDialog.Builder的setCustomTitle()方法非常方便,接下來(lái)通過(guò)本文給大家介紹Android使用setCustomTitle()方法自定義對(duì)話(huà)框標(biāo)題,感興趣的朋友一起學(xué)習(xí)吧2016-02-02Android中FTP上傳、下載的功能實(shí)現(xiàn)(含進(jìn)度)
本篇文章主要介紹了Android中FTP上傳、下載(含進(jìn)度),具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11