Skip to content

Commit

Permalink
Fixes for orientation changes and landscape mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhavalwooplr committed Feb 21, 2017
1 parent 023fa80 commit c800c7e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 12 deletions.
115 changes: 105 additions & 10 deletions Spotlight-library/src/main/java/com/wooplr/spotlight/SpotlightView.java
Expand Up @@ -22,6 +22,7 @@
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
Expand Down Expand Up @@ -183,6 +184,8 @@ public class SpotlightView extends FrameLayout {

private Typeface mTypeface = null;

private int softwareBtnHeight;


public SpotlightView(Context context) {
super(context);
Expand Down Expand Up @@ -365,7 +368,7 @@ private void dismiss() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void startRevealAnimation(final Activity activity) {

float finalRadius = (float) Math.hypot(getWidth(), getHeight());
float finalRadius = (float) Math.hypot(getViewWidth(), getHeight());
Animator anim = ViewAnimationUtils.createCircularReveal(this, targetView.getPoint().x, targetView.getPoint().y, 0, finalRadius);
anim.setInterpolator(AnimationUtils.loadInterpolator(activity,
android.R.interpolator.fast_out_linear_in));
Expand Down Expand Up @@ -406,7 +409,7 @@ public void onAnimationRepeat(Animator animator) {
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void exitRevealAnimation() {
float finalRadius = (float) Math.hypot(getWidth(), getHeight());
float finalRadius = (float) Math.hypot(getViewWidth(), getHeight());
Animator anim = ViewAnimationUtils.createCircularReveal(this, targetView.getPoint().x, targetView.getPoint().y, finalRadius, 0);
anim.setInterpolator(AnimationUtils.loadInterpolator(getContext(),
android.R.interpolator.accelerate_decelerate));
Expand Down Expand Up @@ -561,8 +564,10 @@ private void addPathAnimation(Activity activity) {
View mView = new View(activity);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.width = getWidth();
params.height = getHeight();
params.width = getViewWidth();
params.height = getViewHeight();
// params.width = getMeasuredWidth();
// params.height = getMeasuredHeight();
addView(mView, params);

//Textviews
Expand Down Expand Up @@ -722,20 +727,29 @@ private List<AnimPoint> checkLinePoint() {
(targetView.getViewRight() - targetView.getViewWidth() / 2),
targetView.getViewTop() / 2
));

animPoints.add(new AnimPoint((targetView.getViewRight() - targetView.getViewWidth() / 2),
targetView.getViewTop() / 2,
screenWidth - gutter,
screenWidth - ((screenHeight > screenWidth) ? gutter : (gutter + softwareBtnHeight)),
targetView.getViewTop() / 2));


//TextViews
headingParams.rightMargin = gutter;
if(screenHeight > screenWidth)
headingParams.rightMargin = gutter;//portrait
else
headingParams.rightMargin = gutter + softwareBtnHeight;//landscape
headingParams.leftMargin = (targetView.getViewRight() - targetView.getViewWidth() / 2) + extramargin;
headingParams.bottomMargin = screenHeight - targetView.getViewTop() / 2 + spaceAboveLine;
headingParams.topMargin = extramargin;
headingParams.gravity = Gravity.BOTTOM | Gravity.RIGHT;
headingTv.setGravity(Gravity.LEFT);


subHeadingParams.rightMargin = gutter;
if(screenHeight > screenWidth)
subHeadingParams.rightMargin = gutter;//portrait
else
subHeadingParams.rightMargin = gutter + softwareBtnHeight;//landscape
subHeadingParams.leftMargin = (targetView.getViewRight() - targetView.getViewWidth() / 2) + extramargin;
subHeadingParams.topMargin = targetView.getViewTop() / 2 + spaceBelowLine;
subHeadingParams.bottomMargin = extramargin;
Expand Down Expand Up @@ -778,19 +792,25 @@ private List<AnimPoint> checkLinePoint() {

animPoints.add(new AnimPoint(targetView.getViewRight() - targetView.getViewWidth() / 2,
(screenHeight - targetView.getViewBottom()) / 2 + targetView.getViewBottom(),
screenWidth - gutter,
screenWidth - ((screenHeight > screenWidth) ? gutter : (gutter + softwareBtnHeight)),
(screenHeight - targetView.getViewBottom()) / 2 + targetView.getViewBottom()));

// //TextViews
if(screenHeight > screenWidth)
headingParams.rightMargin = gutter;//portrait
else
headingParams.rightMargin = gutter + softwareBtnHeight;//landscape
headingParams.leftMargin = targetView.getViewRight() - targetView.getViewWidth() / 2 + extramargin;
headingParams.rightMargin = gutter;
headingParams.bottomMargin = screenHeight - ((screenHeight - targetView.getViewBottom()) / 2 + targetView.getViewBottom()) + spaceAboveLine;
headingParams.topMargin = extramargin;
headingParams.gravity = Gravity.BOTTOM | Gravity.RIGHT;
headingTv.setGravity(Gravity.LEFT);

if(screenHeight > screenWidth)
subHeadingParams.rightMargin = gutter;//portrait
else
subHeadingParams.rightMargin = gutter + softwareBtnHeight;//landscape
subHeadingParams.leftMargin = targetView.getViewRight() - targetView.getViewWidth() / 2 + extramargin;
subHeadingParams.rightMargin = gutter;
subHeadingParams.bottomMargin = extramargin;
subHeadingParams.topMargin = ((screenHeight - targetView.getViewBottom()) / 2 + targetView.getViewBottom()) + spaceBelowLine;
subHeadingParams.gravity = Gravity.RIGHT;
Expand All @@ -808,13 +828,29 @@ private List<AnimPoint> checkLinePoint() {
* Remove the spotlight view
*/
private void removeSpotlightView() {

if (listener != null)
listener.onUserClicked(usageId);

if (getParent() != null)
((ViewGroup) getParent()).removeView(this);
}

/**
* Remove the spotlight view
* @param needOnUserClickedCallback true, if user wants a call back when this spotlight view is removed from parent.
*/
public void removeSpotlightView(boolean needOnUserClickedCallback) {
try{
if(needOnUserClickedCallback && listener != null)
listener.onUserClicked(usageId);

if (getParent() != null)
((ViewGroup) getParent()).removeView(this);
}catch(Exception e){
e.printStackTrace();
}
}

/**
* Setters
Expand Down Expand Up @@ -944,6 +980,10 @@ public void setLineEffect(PathEffect pathEffect) {
this.lineEffect = pathEffect;
}

private void setSoftwareBtnHeight(int px){
this.softwareBtnHeight = px;
}

public void setTypeface(Typeface typeface) {
this.mTypeface = typeface;
}
Expand Down Expand Up @@ -986,6 +1026,7 @@ public static class Builder {
public Builder(Activity activity) {
this.activity = activity;
spotlightView = new SpotlightView(activity);
spotlightView.setSoftwareBtnHeight(getSoftButtonsBarHeight(activity));
}

public Builder maskColor(int maskColor) {
Expand Down Expand Up @@ -1162,4 +1203,58 @@ public boolean dispatchKeyEvent(KeyEvent event) {
public void logger(String s) {
Log.d("Spotlight", s);
}

private int getViewHeight(){
if(getWidth() > getHeight()){
//Landscape
return getHeight();
}else{
//Portrait
return (getHeight() - softwareBtnHeight);
}
}

private int getViewWidth(){
if(getWidth() > getHeight()){
//Landscape
return (getWidth() - softwareBtnHeight);
}else{
//Portrait
return getWidth();
}
}

private static int getSoftButtonsBarHeight(Activity activity) {
try{
// getRealMetrics is only available with API 17 and +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

if(metrics.heightPixels > metrics.widthPixels)
{
//Portrait
int usableHeight = metrics.heightPixels;
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}else{
//Landscape
int usableHeight = metrics.widthPixels;
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.widthPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
}
}catch(Exception e){
e.printStackTrace();
}
return 0;
}
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -8,7 +8,8 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/example/spotlight/MainActivity.java
@@ -1,5 +1,6 @@
package com.example.spotlight;

import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -8,6 +9,7 @@
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -39,6 +41,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private static final String INTRO_CHANGE_POSITION = "change_position_intro";
private static final String INTRO_SEQUENCE = "sequence_intro";
private boolean isRevealEnabled = true;
private SpotlightView spotLight;

@BindView(R.id.switchAnimation)
TextView switchAnimation;
Expand Down Expand Up @@ -140,7 +143,7 @@ public void run() {
}

private void showIntro(View view, String usageId) {
new SpotlightView.Builder(this)
spotLight = new SpotlightView.Builder(this)
.introAnimationDuration(400)
.enableRevealAnimation(isRevealEnabled)
.performClick(true)
Expand All @@ -162,5 +165,15 @@ private void showIntro(View view, String usageId) {
.usageId(usageId) //UNIQUE ID
.show();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

if(spotLight.isShown()){
spotLight.removeSpotlightView(false);//Remove current spotlight view from parent
resetAndPlay.performClick();//Show it again in new orientation if required.
}
}
}

0 comments on commit c800c7e

Please sign in to comment.