温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么对图片的颜色进行处理

发布时间:2021-06-28 18:08:36 来源:亿速云 阅读:206 作者:Leah 栏目:移动开发

这篇文章将为大家详细讲解有关Android中怎么对图片的颜色进行处理,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

布局:

<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中怎么对图片的颜色进行处理就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI