点击按钮颜色变深.通过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; }

  

 

转载于:https://www.cnblogs.com/wikiki/p/5341043.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值