国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android圖片色彩變換實現方法

2019-12-12 05:37:55
字體:
來源:轉載
供稿:網友

最近在做圖片相關的應用,所以就各方積累到一些常用的操作,一般來說會有多種方式來實現這一功能,比如
 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參數。

總結:在處理圖像有許多的方法,尤其是對圖像用像素點的方式效果最多,可以呈現多種多樣的效果。如老照片,浮雕,底片等等;而采用顏色矩陣也是一種好經典的操作方法。這些很值得我們學習,這樣我們就可以是的我們的應用呈現出更加絢麗的色彩及效果咯!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河西区| 盘山县| 富蕴县| 阿克陶县| 松潘县| 武隆县| 大名县| 吴江市| 昆明市| 阿克陶县| 原阳县| 吉林市| 客服| 米林县| 大足县| 达拉特旗| 南宫市| 余干县| 库尔勒市| 建阳市| 麦盖提县| 延津县| 龙岩市| 孟州市| 定日县| 永新县| 永兴县| 松江区| 竹溪县| 青浦区| 务川| 昌吉市| 海口市| 五华县| 宜阳县| 汽车| 安新县| 虎林市| 民县| 庄河市| 孝义市|