点击图片变暗效果 省去一张点击效果的图片


// 点击图片变暗
public static void changeLight(ImageView imageView, int brightness) {
ColorMatrix cMatrix = new ColorMatrix();
cMatrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0,
brightness,// 改变亮度
0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 });
imageView.setColorFilter(new ColorMatrixColorFilter(cMatrix));
}
上面代码是点击图片变暗,这种情况下就不用让美工每次做2张图片了,同时也会美化GridView的点击效果,你只需要

wallpaper.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

}
});
wallpaper.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
Util.changeLight(wallpaper, 0);
Bundle bundle = new Bundle();
Intent it = new Intent(MobileWallpaper.this,MobileSetWallpaper.class);
bundle.putInt("position", position);
bundle.putInt("bg", images[position]);

it.putExtras(bundle);
startActivity(it);

//onclick
break;
case MotionEvent.ACTION_DOWN:

Util.changeLight(wallpaper, -50);
break;

case MotionEvent.ACTION_CANCEL:

Util.changeLight(wallpaper, 0);
break;
}
return false;
}
});

二。进行图片双指缩小,放大时,往往是没有限制,你只需要调用下面的center方法,其中horizontal,vertical为true,则图片无论移动,缩放都会居中显示

/**
* 横向、纵向居中
*/
protected void center(Bitmap bitmap,boolean horizontal, boolean vertical) {

Matrix m = new Matrix();
m.set(matrix);
RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
m.mapRect(rect);

float height = rect.height();
float width = rect.width();

float deltaX = 0, deltaY = 0;

if (vertical) {
// 图片小于屏幕大小,则居中显示。大于屏幕,上方留空则往上移,下方留空则往下移
int screenHeight = dm.heightPixels;
if (height < screenHeight) {
deltaY = (screenHeight - height) / 2 - rect.top;
} else if (rect.top > 0) {
deltaY = -rect.top;
} else if (rect.bottom < screenHeight) {
deltaY = image_bg.getHeight() - rect.bottom;
}
}

if (horizontal) {
int screenWidth = dm.widthPixels;
if (width < screenWidth) {
deltaX = (screenWidth - width) / 2 - rect.left;
} else if (rect.left > 0) {
deltaX = -rect.left;
} else if (rect.right < screenWidth) {
deltaX = screenWidth - rect.right;
}
}
matrix.postTranslate(deltaX, deltaY);
}

/**
* 两点的距离
*/
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}

/**
* 两点的中点
*/
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}

/**
* 限制最大最小缩放比例,自动居中
*/
private void CheckView() {
float p[] = new float[9];
matrix.getValues(p);
if (mode == ZOOM) {
if (p[0] < minScaleR) {
matrix.setScale(minScaleR, minScaleR);
}
if (p[0] > MAX_SCALE) {
matrix.set(savedMatrix);
}
}
center(bitmap);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值