package com.gyarmy.testcolor; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.view.Menu; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; public class MainActivity extends Activity implements OnSeekBarChangeListener { private SeekBar seek_red; private SeekBar seek_green; private SeekBar seek_blue; private TextView text_content; private ImageView iv_mm; private Bitmap img_src; private float redMe = 1f; private float greenMe = 1f; private float blueMe = 1f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seek_red = (SeekBar)findViewById(R.id.seek_red); seek_green = (SeekBar)findViewById(R.id.seek_green); seek_blue = (SeekBar)findViewById(R.id.seek_blue); text_content = (TextView)findViewById(R.id.text_content); iv_mm = (ImageView) findViewById(R.id.iv_mm); //String path = Environment.getExternalStorageDirectory().toString(); String path = "/storage/emulated/0/Pictures/Screenshots/pic.png"; img_src = BitmapFactory.decodeFile(path); iv_mm.setImageBitmap(img_src); seek_red.setOnSeekBarChangeListener(this); seek_green.setOnSeekBarChangeListener(this); seek_blue.setOnSeekBarChangeListener(this); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub text_content.setText("progress:"+progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub //改变图片的颜色 //Bitmap img_dst = ((BitmapDrawable)(iv_mm.getDrawable())).getBitmap(); if(seekBar == seek_red){ redMe = seek_red.getProgress(); }else if(seekBar == seek_green){ greenMe = seek_green.getProgress(); }else if(seekBar == seek_blue){ blueMe = seek_blue.getProgress(); } Bitmap img_dst = Bitmap.createBitmap(img_src.getWidth(), img_src.getHeight(), img_src.getConfig()); Canvas cas = new Canvas(img_dst); Paint paint = new Paint(); Matrix matrix = new Matrix(); float[] cm = new float[]{ 1*redMe/100,0,0,0,0, 0,1*greenMe/100,0,0,0, 0,0,1*blueMe/100,0,0, 0,0,0,1,0 }; paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(cm))); //matrix.setRotate(40); cas.drawBitmap(img_src, matrix, paint); iv_mm.setImageBitmap(img_dst); } }
0则评论给“颜色设置”