Android圖片色彩變換實現(xiàn)方法
最近在做圖片相關的應用,所以就各方積累到一些常用的操作,一般來說會有多種方式來實現(xiàn)這一功能,比如
1.采用色度變換
2.采用ColorMatrix顏色矩陣
3.采用對像素點的直接操作
等等,今天就復習一下第一種方式吧,雖然比較單一,得到的結果類型也比較少。
相比較于常見的圖片風格變換,一般我們就是換個色彩度,飽和度,亮度等等,這里也恰恰是這個方式
編碼思路:
•抽象出圖片操作工具類
•創(chuàng)建一個用于操作的Bitmap對象
•使用畫布Canvas,畫筆Paint
•調(diào)色處理,參數(shù)控制
•畫出Bitmap并返回
•被相關方法調(diào)用,得到結果
下面直接上代碼吧
首先是布局
<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就會僅僅調(diào)用一次,圖像將不能被編輯 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 } }
然后運行程序,你就可以通過對滑動條的調(diào)節(jié)來對圖像做相關的處理變換了。
注意:
在工具類的方法中最后要對傳進去的參數(shù)做處理,而不是我們自己聲明的bitmap,否則我們將得不到我們實時的圖片效果。因為我們的bitmap僅僅是作為一個操作的對象模型,真正需要操作的是我們的bp參數(shù)。
總結:在處理圖像有許多的方法,尤其是對圖像用像素點的方式效果最多,可以呈現(xiàn)多種多樣的效果。如老照片,浮雕,底片等等;而采用顏色矩陣也是一種好經(jīng)典的操作方法。這些很值得我們學習,這樣我們就可以是的我們的應用呈現(xiàn)出更加絢麗的色彩及效果咯!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android使用fastjson庫解析json字符串實戰(zhàn)
fastjson是一個Java語言編寫的高性能功能完善的JSON庫,它采用一種“假定有序快速匹配”的算法,把JSON?Parse的性能提升到極致,是目前Java語言中最快的JSON庫,Fastjson接口簡單易用,已經(jīng)被廣泛使用在緩存序列化、協(xié)議交互、Web輸出、Android客戶端等多種應用場景2023-11-115個Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法
本文主要介紹了5個Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02Android App開發(fā)中創(chuàng)建Fragment組件的教程
這篇文章主要介紹了Android App開發(fā)中創(chuàng)建Fragment的教程,Fragment是用以更靈活地構建多屏幕界面的可UI組件,需要的朋友可以參考下2016-05-05Android中的Intent Filter匹配規(guī)則簡介
這篇文章主要為大家詳細介紹了Android中的Intent Filter匹配規(guī)則,感興趣的小伙伴們可以參考一下2016-04-04Android使用setCustomTitle()方法自定義對話框標題
Android有自帶的對話框標題,但是不太美觀,如果要給彈出的對話框設置一個自定義的標題,使用AlertDialog.Builder的setCustomTitle()方法非常方便,接下來通過本文給大家介紹Android使用setCustomTitle()方法自定義對話框標題,感興趣的朋友一起學習吧2016-02-02Android中FTP上傳、下載的功能實現(xiàn)(含進度)
本篇文章主要介紹了Android中FTP上傳、下載(含進度),具有一定的參考價值,有需要的可以了解一下。2016-11-11