点击按钮颜色变深.通过ColorFilter ColorMatrix

 

 

private ImageButton imgeBtn;

	// 颜色矩阵
	public final float[] BT_SELECTED = new float[] { 1, 0, 0, 0, -50, 0, 1, 0,
			0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };
	public final float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0, 0, 1,
			0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		imgeBtn = (ImageButton) findViewById(R.id.imagebtn);
		imgeBtn.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub

				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					
					((ImageButton) v).setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));

				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					
					((ImageButton) v).setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
//					v.setBackgroundDrawable(v.getBackground());
					
					
				}

				return false;
			}
		});
	}

  

 ((Button) v).getCompoundDrawables()[1].setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));

 

android 使用ColorMatrix把图变成灰色


ColorMatrix类有一个内置的方法可用于改变饱和度。 ColorMatrix cm = new ColorMatrix(); cm.setSaturation(.5f); paint.setColorFilter(new ColorMatrixColorFilter(cm)); 传入一个大于1的数字将增加饱和度,而传入一个0~1之间的数字会减少饱和度。0值将产生一幅灰度图像。 所以获得灰色图像的代码如下: public static final Bitmap grey(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap faceIconGreyBitmap = Bitmap .createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(faceIconGreyBitmap); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter( colorMatrix); paint.setColorFilter(colorMatrixFilter); canvas.drawBitmap(bitmap, 0, 0, paint); return faceIconGreyBitmap; }

  

 

posted @ 2016-03-31 14:15  wikiki  阅读(437)  评论(0编辑  收藏  举报