This is a difficult situation to describe, so please bear with me. I've been asked to make a 'coach marks' screen a little bit like this:
However, in my case I need to 'pull' some of the underlying images to the front and display them in a different color to make them stand out. I cannot determine what the 'frame' of these images will be before run time. My approach is to add a black, semi-opaque view (I call it a 'curtain') covering the entire screen, and then add the images that I need as subviews of the curtain with a different color. EDIT This part is easy and I am not asking for help with this (which is why the suggestion that this is a duplicate of the suggested question is incorrect).
I don't believe that I can use bringToFront()
or setTranslationZ()
because these methods will not bring the ImageView
all the way to the front of my curtain. So, what I am trying to to do is get the exact 'frame' of the image view (its x, y, width, and height) relative to the overall screen and then try to add the new image view using the same drawable with the same frame.
It's also important to know that the underlying 'view' is actually made up of multiple views (including a sliding drawer), which is why I need to get the coordinates relative to the screen. I hope this makes sense.
I am able to draw the new ImageView
as a subview of the curtain with the correct color, but the position and size of the image is way off. How can I draw this new ImageView
exactly on top of the previous one?
I'm also open to other strategies (since this one seems to not be working). Thank you for any help.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
ViewTreeObserver vto = mRootview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mRootview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
mDrawerLayout.openDrawer(Gravity.LEFT);
RelativeLayout curtain = addOpaqueCurtainToRootView();
int frame[] = getFrameOfExistingImageView();
addNewImageViewOnTopOfEverything(curtain, frame);
}
});
}
private RelativeLayout addOpaqueCurtainToRootView() {
RelativeLayout curtain = new RelativeLayout(context);
RelativeLayout.LayoutParams curtainParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
curtain.setLayoutParams(curtainParams);
curtain.setBackgroundColor(Color.argb(179, 0, 0, 0)); //70% opaque
mRootview.addView(curtain);
return curtain;
}
private int[] getFrameOfExistingImageView() {
// proving that I have a reference to the original image
mCurrentImage.setColorFilter(Color.rgb(255, 0, 0));
int array[] = new int[2];
mCurrentImage.getLocationOnScreen(array);
Log.d(TAG, "width: " + Integer.toString(array[0]));
Log.d(TAG, "height: " + Integer.toString(array[1]));
Log.d(TAG, "x: " + Float.toString(mCurrentImage.getX()));
Log.d(TAG, "y: " + Float.toString(mCurrentImage.getY()));
int frame[] = new int[4]; // x,y,width,height
frame[0] = (int)mCurrentImage.getX();
frame[1] = (int)mCurrentImage.getY();
frame[2] = array[0];
frame[3] = array[1];
return frame;
}
private void addNewImageViewOnTopOfEverything(RelativeLayout curtain, int[] frame) {
ImageView iv = new ImageView(context);
iv.setImageResource(R.drawable.imgView);
iv.setColorFilter(Color.rgb(255, 255, 255));
iv.setX(frame[0]);
iv.setY(frame[1]);
iv.setLayoutParams(new RelativeLayout.LayoutParams(frame[2], frame[3]));
curtain.addView(iv);
}