亲宝软件园·资讯

展开

Android图片的颜色处理功能 Android编程实现图片的颜色处理功能示例

飘走的我 人气:0
想了解Android编程实现图片的颜色处理功能示例的相关内容吗,飘走的我在本文为您仔细讲解Android图片的颜色处理功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,图片,颜色处理,下面大家一起来学习吧。

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

先看效果图:

图片的颜色处理的基本步骤:

1.先拿到一张原图
2.拿到一张和原图一样的纸
3.把纸固定在画板上
4.颜色的取值
5.进度条的拖动与监听

代码编写:

布局:

<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:orientation="vertical">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="青--红" />
  <SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/red_seekbar"/>
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="青--绿" />
  <SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/green_seekbar"/>
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="青--蓝" />
  <SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/blue_seekbar"/>
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/im"
    android:src="@drawable/img_small_1"/>
</LinearLayout>

activity:

public class MainActivity extends Activity implements OnSeekBarChangeListener{
  private SeekBar red_sb,green_sb,blue_sb;
  private ImageView imageView;
  private Canvas canvas;
  private Paint paint;
  private Bitmap baseBitmap,copyBitmap;
  private float red_vector,green_vector,blue_vector;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    red_sb=(SeekBar) findViewById(R.id.red_seekbar);
    green_sb=(SeekBar) findViewById(R.id.green_seekbar);
    blue_sb=(SeekBar) findViewById(R.id.blue_seekbar);
    imageView=(ImageView) findViewById(R.id.im);
    red_sb.setOnSeekBarChangeListener(this);
    green_sb.setOnSeekBarChangeListener(this);
    blue_sb.setOnSeekBarChangeListener(this);
  }
  @Override
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
  }
  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
  }
  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    int progress=seekBar.getProgress();
    float count=progress/50f;//使拖动条的取值为0f-2f,满足我们的取值要求
    switch (seekBar.getId()) {
    case R.id.red_seekbar:
      this.red_vector=count;
      break;
    case R.id.green_seekbar:
      this.green_vector=count;
      break;
    case R.id.blue_seekbar:
      this.blue_vector=count;
      break;
    default:
      break;
    }
    //主题代码
    baseBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img_small_1);
    copyBitmap=Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());
    canvas=new Canvas(copyBitmap);
    Matrix matrix=new Matrix();
    paint=new Paint();
    //vector:取值范围(0-2)
    float[] colors=new float[]{
        red_vector,0,0,0,0,
        0,green_vector,0,0,0,
        0,0,blue_vector,0,0,
        0,0,0,1,0};
    paint.setColorFilter(new ColorMatrixColorFilter(colors));
    canvas.drawBitmap(baseBitmap, matrix, paint);
    imageView.setImageBitmap(copyBitmap);
  }
}

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

加载全部内容

相关教程
猜你喜欢
用户评论