绘图机制ColorMatrix

我们可以通过Android系统提供的API,来进行ColorMatrix的修改,也可以精确的修改矩阵的值来实现颜色的效果。
这里是第二种:
public class PictureActivity extends AppCompatActivity {

ViewGroup grid = null;
EditText mEdit [] = new EditText[20];
Bitmap bitmap;
ImageView mImage;
Button changeButton;
Button restoreButton;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

    setContentView(R.layout.picture_change);
    init();//初始化控件

    grid.post(new Runnable() {  ////我们无法再onCreat()方法中获得视图的宽高值,所以通过
                                //View的post方法,在视图创建完毕后获得宽高值。
        @Override这里写图片描述
        public void run() {

            add();//添加控件,动态加载ExitText
            Log.d("Pic",Thread.currentThread().getName()+"****");

            setDefaultValue();//设置初始的值

            changeButton.setOnClickListener(buttonListener);
           restoreButton.setOnClickListener(buttonListener);
        }
    });

}

//设置EditText默认值
private void setDefaultValue() {

    //for (int i = 0; i < grid.getChildCount(); i++) {
    for (int i = 0; i < mEdit.length; i++) {

        if (i % 6 == 0) {

            mEdit[i].setText(1+"");

        } else {

            mEdit[i].setText(0+"");
        }

    }

}

//添加控件
private void add() {

    for (int i = 0; i < 20; i++) {

        EditText edit = new EditText(PictureActivity.this);

        mEdit[i] = edit;

        grid.addView(edit,grid.getWidth() / 5, grid.getHeight() / 4);

    }

}

private void init() {

    changeButton = (Button)findViewById(R.id.changeButton);
    restoreButton = (Button)findViewById(R.id.restoreButton);

    grid = (ViewGroup) findViewById(R.id.grid);
    bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ttt);//从资源中得到bitmap类型的实例

    mImage = (ImageView)findViewById(R.id.mImageView);
    mImage.setImageBitmap(bitmap);

}

*

`
private void changePic(){*

Bitmap bitm = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);

   Canvas canvas = new Canvas(bitm);

    float[] array = getArray();  //当前矩阵值
   ColorMatrix colorMatrix = new ColorMatrix();
   colorMatrix.set(array);//获取输入的值将其传递给colorMatrix对象

   Paint paint = new Paint();

   paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

   canvas.drawBitmap(bitmap,0,0,paint);
   mImage.setImageBitmap(bitm);
}

public float[] getArray() {

    float[] array = new float[mEdit.length];

    for (int i = 0; i < mEdit.length; i++){

        array[i] = Float.valueOf(mEdit[i].getText().toString());

    }
    return array;
}

private View.OnClickListener buttonListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        switch (v.getId()){
           case R.id.changeButton:
                changePic();

                break;
            case  R.id.restoreButton:

                //恢复原图
                restore();
                break;
        }
    }
};

private void restore() {

    init();
    setDefaultValue();
    changePic();

}

}

要加载的layout文件如下

你可能感兴趣的:(android,绘图)