Friday, 18 October 2013

Quick pop-up with animation like Google Chrome .

Quick pop-up same as Google Chrome which appear when you first time start Chrome.
Here in this pop-up use animation which will be used to create a floating fill to pop-up.

How to use ?
QuickPopup quickPopup = new QuickPopup(MainActivity.this, "Hi :)");
quickPopup.show(view);

QuickPopup.java

public class QuickPopup {

protected WindowManager mWindowManager;
protected Context mContext;
protected Drawable mBackgroundDrawable = null;
protected PopupWindow mWindow;
private TextView txtShow;
private ImageView imgUp, imgDown;
protected View mView;
protected ShowPopUpListener showpopupListener;

public QuickPopup(Context context, String text, int viewResource) {
mContext = context;
mWindow = new PopupWindow(context);
mWindowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
initCompo(viewResource);
}

public QuickPopup(Context context) {
this(context, "", R.layout.popup);
}

public QuickPopup(Context context, String text) {
this(context);
setText(text);
}

private void initCompo(int viewResource) {
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

setContentView(layoutInflater.inflate(viewResource, null));

txtShow = (TextView) mView.findViewById(R.id.text);
imgUp = (ImageView) mView.findViewById(R.id.arrow_up);
imgDown = (ImageView) mView.findViewById(R.id.arrow_down);

txtShow.setMovementMethod(ScrollingMovementMethod.getInstance());
txtShow.setSelected(true);
}

public void show(View anchor) {
preShow();

int[] location = new int[2];

anchor.getLocationOnScreen(location);

Rect anchorRect = new Rect(location[0], location[1], location[0]
+ anchor.getWidth(), location[1] + anchor.getHeight());

mView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

int rootHeight = mView.getMeasuredHeight();
int rootWidth = mView.getMeasuredWidth();

final int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
final int screenHeight = mWindowManager.getDefaultDisplay().getHeight();

int yPos = anchorRect.top - rootHeight;

boolean onTop = true;

if (anchorRect.top < screenHeight / 2) {
yPos = anchorRect.bottom;
onTop = false;
}

int whichArrow, requestedX;

whichArrow = ((onTop) ? R.id.arrow_down : R.id.arrow_up);
requestedX = anchorRect.centerX();

View arrow = whichArrow == R.id.arrow_up ? imgUp : imgDown;
View hideArrow = whichArrow == R.id.arrow_up ? imgDown : imgUp;

final int arrowWidth = arrow.getMeasuredWidth();

arrow.setVisibility(View.VISIBLE);

ViewGroup.MarginLayoutParams param = (ViewGroup.MarginLayoutParams) arrow
.getLayoutParams();

hideArrow.setVisibility(View.INVISIBLE);

int xPos = 0;

if (anchorRect.left + rootWidth > screenWidth) {
xPos = (screenWidth - rootWidth);// right click
} else if (anchorRect.left - (rootWidth / 2) < 0) {
xPos = anchorRect.left;// left clcik
} else {
xPos = (anchorRect.centerX() - (rootWidth / 2)); // inbetween
}

param.leftMargin = (requestedX - xPos) - (arrowWidth / 2);

if (onTop) {
txtShow.setMaxHeight(anchorRect.top - anchorRect.height());

} else {
txtShow.setMaxHeight(screenHeight - yPos);
}

mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);

mView.setAnimation(AnimationUtils.loadAnimation(mContext,
R.anim.float_anim));

}

protected void preShow() {
if (mView == null)
throw new IllegalStateException("view undefined");

if (showpopupListener != null) {
showpopupListener.onPreShow();
showpopupListener.onShow();
}

if (mBackgroundDrawable == null)
mWindow.setBackgroundDrawable(new BitmapDrawable());
else
mWindow.setBackgroundDrawable(mBackgroundDrawable);

mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
mWindow.setTouchable(true);
mWindow.setFocusable(true);
mWindow.setOutsideTouchable(true);

mWindow.setContentView(mView);
}

public void setBackgroundDrawable(Drawable background) {
mBackgroundDrawable = background;
}

public void setContentView(View root) {
mView = root;

mWindow.setContentView(root);
}

public void setContentView(int layoutResID) {
LayoutInflater inflator = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

setContentView(inflator.inflate(layoutResID, null));
}

public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
mWindow.setOnDismissListener(listener);
}

public void dismiss() {
mWindow.dismiss();
if (showpopupListener != null) {
showpopupListener.onDismiss();
}
}

public void setText(String text) {
txtShow.setText(text);
}

public void setShowListener(ShowPopUpListener showpopupListener) {
this.showpopupListener = showpopupListener;
}
}


Interface

public interface ShowPopUpListener {
void onPreShow();

void onDismiss();

void onShow();
}

res/anim/float_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:interpolator="@android:anim/bounce_interpolator"
        android:duration="1000"
        android:fillAfter="true"
        android:fromYDelta="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:toYDelta="5%" />

</set>



2 comments: