我在adapter
RecyclerView
中有此代码:
holder.image.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int[] loc = new int[2];
holder.image.getLocationInWindow(loc);
((FeedFragment) fragment).blurBackground(getBitmapFromView(holder.image), loc[0], loc[1]);
Toast.makeText(context.getApplicationContext(), "Long press started", Toast.LENGTH_SHORT).show();
isLongPressed = true;
return true;
}
});
holder.image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
// We're only interested in when the button is released.
if (event.getAction() == MotionEvent.ACTION_UP) {
// We're only interested in anything if our speak button is currently pressed.
if (isLongPressed) {
// ((FeedFragment) fragment).removeBlur();
((FeedFragment) fragment).showAllGalleries(String.valueOf(photo.getPk()));
Toast.makeText(context.getApplicationContext(), "Long press ended", Toast.LENGTH_SHORT).show();
// Do something when the button is released.
isLongPressed = false;
}
}
return false;
}
});
所以我想做的是:一旦长按开始,我想调用一种方法来模糊屏幕,除了长时间点击发生的图像,这种模糊(改变背景的alpha)发生在400毫秒。
问题是,blurBackground()
方法被调用两次。因此,当我删除onActivityResult
中的模糊时,我只删除最后添加的图层,而由于方法被调用两次而存在另一层模糊。我希望blurBackground()
方法只能被调用一次。
这种行为的原因是什么?如何修复?
以下是其他方法:
public void blurBackground(Bitmap bitmapFromView, int left, int top) {
LogUtil.i(TAG, "blurBackground called");
int statusBarHeight = (int) Math.ceil(25 * getActivity().getResources().getDisplayMetrics().density);
RelativeLayout overlay = new RelativeLayout(getActivity());
ID_OVERLAY = ViewIDGenerator.generateViewId();
LogUtil.i(TAG, "ID = " + ID_OVERLAY);
overlay.setId(ID_OVERLAY);
overlay.setBackgroundColor(Color.parseColor("#000000"));
RelativeLayout.LayoutParams overlayParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
overlay.setLayoutParams(overlayParams);
ImageView snapshot = new ImageView(getActivity());
snapshot.setImageBitmap(bitmapFromView);
RelativeLayout.LayoutParams snapshotParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
snapshotParams.setMargins(left, top-statusBarHeight, 0, 0);
snapshot.setLayoutParams(snapshotParams);
overlay.addView(snapshot);
container.addView(overlay);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 0.7f);
alphaAnimation.setDuration(400);
alphaAnimation.setFillAfter(true);
overlay.startAnimation(alphaAnimation);
}
public void removeBlur() {
LogUtil.i(TAG, "removeBlur called");
LogUtil.i(TAG, "ID = " + ID_OVERLAY);
View namebar = container.findViewById(ID_OVERLAY);
((ViewGroup) namebar.getParent()).removeView(namebar);
}
答案 0 :(得分:1)
如果您唯一的问题是blurBackground被调用两次,您可以举起一个标志!
Boolean isBlurred = false;
然后在blurBackground
public void blurBackground(Bitmap bitmapFromView, int left, int top) {
if (!isBlurred)
{
LogUtil.i(TAG, "blurBackground called");
...
isBlurred = true;
}
}
答案 1 :(得分:0)
为onTouch方法返回true,这样您就可以告诉您已处理该事件。
public boolean onTouch(View v, MotionEvent event) {
doSomething();
return true;
}