iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android编程之图片颜色处理方法
  • 262
分享到

Android编程之图片颜色处理方法

程之方法图片Android 2022-06-06 09:06:49 262人浏览 薄情痞子
摘要

本文实例讲述了Android编程之图片颜色处理方法。分享给大家供大家参考,具体如下: 你想做到跟美图秀秀一样可以处理自己的照片,美化自己的照片吗?其实你也可以自己做一个这样的软

本文实例讲述了Android编程之图片颜色处理方法。分享给大家供大家参考,具体如下:

你想做到跟美图秀秀一样可以处理自己的照片,美化自己的照片吗?其实你也可以自己做一个这样的软件,废话不多说了,直接上图,上代码了!

效果图如下:

没处理前:

处理之后:

MainActivity.java的代码如下:


package net.loonggg.test; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.canvas; 
import android.graphics.Color; 
import android.graphics.ColORMatrix; 
import android.graphics.ColorMatrixColorFilter; 
import android.graphics.Matrix; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.widget.ImageView; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
public class MainActivity extends Activity { 
  private SeekBar sb1, sb2, sb3, sb4, sb5; 
  private ImageView iv; 
  private Bitmap bitmap, updateBitmap; 
  private Canvas canvas; 
  private Paint paint; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    iv = (ImageView) findViewById(R.id.iv); 
    sb1 = (SeekBar) findViewById(R.id.sb1); 
    sb2 = (SeekBar) findViewById(R.id.sb2); 
    sb3 = (SeekBar) findViewById(R.id.sb3); 
    sb4 = (SeekBar) findViewById(R.id.sb4); 
    sb5 = (SeekBar) findViewById(R.id.sb5); 
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b); 
    updateBitmap = Bitmap.createBitmap(bitmap.getWidth(), 
        bitmap.getHeight(), bitmap.getConfig()); 
    canvas = new Canvas(updateBitmap); 
    paint = new Paint(); 
    final ColorMatrix cm = new ColorMatrix(); 
    paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
    paint.setColor(Color.BLACK); 
    paint.setAntiAlias(true); 
    final Matrix matrix = new Matrix(); 
    canvas.drawBitmap(bitmap, matrix, paint); 
    iv.setImageBitmap(updateBitmap); 
     
    sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
        int progress = seekBar.getProgress(); 
        cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 红色值 
            0, 1, 0, 0, 0,// 绿色值 
            0, 0, 1, 0, 0,// 蓝色值 
            0, 0, 0, 1, 0 // 透明度 
        }); 
        paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
        canvas.drawBitmap(bitmap, matrix, paint); 
        iv.setImageBitmap(updateBitmap); 
      } 
      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 
      } 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, 
          boolean fromUser) { 
      } 
    }); 
     
    sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
        int progress = seekBar.getProgress(); 
        cm.set(new float[] { 1, 0, 0, 0, 0,// 红色值 
            0, progress / 128f, 0, 0, 0,// 绿色值 
            0, 0, 1, 0, 0,// 蓝色值 
            0, 0, 0, 1, 0 // 透明度 
        }); 
        paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
        canvas.drawBitmap(bitmap, matrix, paint); 
        iv.setImageBitmap(updateBitmap); 
      } 
      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 
      } 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, 
          boolean fromUser) { 
      } 
    }); 
     
    sb3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
        int progress = seekBar.getProgress(); 
        cm.set(new float[] { 1, 0, 0, 0, 0,// 红色值 
            0, 1, 0, 0, 0,// 绿色值 
            0, 0, progress / 128f, 0, 0,// 蓝色值 
            0, 0, 0, 1, 0 // 透明度 
        }); 
        paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
        canvas.drawBitmap(bitmap, matrix, paint); 
        iv.setImageBitmap(updateBitmap); 
      } 
      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 
      } 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, 
          boolean fromUser) { 
      } 
    }); 
     
    sb4.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
        int progress = seekBar.getProgress(); 
        cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 红色值 
            0, progress / 128f, 0, 0, 0,// 绿色值 
            0, 0, progress / 128f, 0, 0,// 蓝色值 
            0, 0, 0, 1, 0 // 透明度 
        }); 
        paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
        canvas.drawBitmap(bitmap, matrix, paint); 
        iv.setImageBitmap(updateBitmap); 
      } 
      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 
      } 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, 
          boolean fromUser) { 
      } 
    }); 
     
    sb5.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 
      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
        int progress = seekBar.getProgress(); 
        cm.set(new float[] { 1, 0, 0, 0, 0,// 红色值 
            0, 1, 0, 0, 0,// 绿色值 
            0, 0, 1, 0, 0,// 蓝色值 
            0, 0, 0, progress / 128f, 0 // 透明度 
        }); 
        paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
        canvas.drawBitmap(bitmap, matrix, paint); 
        iv.setImageBitmap(updateBitmap); 
      } 
      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 
      } 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, 
          boolean fromUser) { 
      } 
    }); 
  } 
}

布局文件代码如下:


<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:background="#CDCDCD" 
  android:orientation="vertical" 
  tools:context=".MainActivity" > 
  <ImageView 
    android:id="@+id/iv" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" 
    android:padding="10dp" > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="红色:" 
      android:textColor="#FF0000" 
      android:textSize="24sp" /> 
    <SeekBar 
      android:id="@+id/sb1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:max="256" 
      android:progress="128" /> 
  </LinearLayout> 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" 
    android:padding="10dp" > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="绿色:" 
      android:textColor="#00FF00" 
      android:textSize="24sp" /> 
    <SeekBar 
      android:id="@+id/sb2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:max="256" 
      android:progress="128" /> 
  </LinearLayout> 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" 
    android:padding="10dp" > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="蓝色:" 
      android:textColor="#0000FF" 
      android:textSize="24sp" /> 
    <SeekBar 
      android:id="@+id/sb3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:max="256" 
      android:progress="128" /> 
  </LinearLayout> 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" 
    android:padding="10dp" > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="饱和度:" 
      android:textColor="#000000" 
      android:textSize="16.5sp" /> 
    <SeekBar 
      android:id="@+id/sb4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:max="256" 
      android:progress="128" /> 
  </LinearLayout> 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" 
    android:padding="10dp" > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="透明度:" 
      android:textColor="#000000" 
      android:textSize="16.5sp" /> 
    <SeekBar 
      android:id="@+id/sb5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:max="256" 
      android:progress="128" /> 
  </LinearLayout> 
</LinearLayout>

到这里就完了,看明白了吗?

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:Android图片处理实例介绍(图)android图片处理之让图片变成圆形Android中3种图片压缩处理方法Android模糊处理实现图片毛玻璃效果android图片处理之让图片一直匀速旋转android图片圆角、图片去色处理示例Android 图片特效处理的方法实例Android实现拍照及图片裁剪(6.0以上权限处理及7.0以上文件管理)Android编程中图片特效处理方法小结Android图片处理教程之全景查看效果实现


--结束END--

本文标题: Android编程之图片颜色处理方法

本文链接: https://www.lsjlt.com/news/26459.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作