日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android图片色彩变幻

發布時間:2023/12/20 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android图片色彩变幻 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在做圖片相關的應用,所以就各方積累到一些常用的操作,一般來說會有多種方式來實現這一功能,比如

  • 采用色度變換
  • 采用ColorMatrix顏色矩陣
  • 采用對像素點的直接操作
    等等,今天就復習一下第一種方式吧,雖然比較單一,得到的結果類型也比較少。

  • 相比較于常見的圖片風格變換,一般我們就是換個色彩度,飽和度,亮度等等,這里也恰恰是這個方式
    編碼思路:

    • 抽象出圖片操作工具類
    • 創建一個用于操作的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;@Overrideprotected 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);}@Overridepublic 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));}@Overridepublic void onStartTrackingTouch(SeekBar arg0) {// TODO Auto-generated method stub}@Overridepublic void onStopTrackingTouch(SeekBar arg0) {// TODO Auto-generated method stub}}

    然后運行程序,你就可以通過對滑動條的調節來對圖像做相關的處理變換了。


    注意:
    在工具類的方法中最后要對傳進去的參數做處理,而不是我們自己聲明的bitmap,否則我們將得不到我們實時的圖片效果。因為我們的bitmap僅僅是作為一個操作的對象模型,真正需要操作的是我們的bp參數。


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

    總結

    以上是生活随笔為你收集整理的Android图片色彩变幻的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。