Last active
January 3, 2018 10:02
-
-
Save SIMMORSAL/d73695c4a785b82923a0b46ef7fba584 to your computer and use it in GitHub Desktop.
testing phase of Resizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.res.Resources; | |
import android.os.Handler; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.WindowManager; | |
public class Resizer { | |
public static int DONT_CHANGE = -999; | |
private Handler handler = new Handler(); | |
private ViewGroup.LayoutParams layoutParams; | |
private View view; | |
private int[] widthSizes; | |
private int[] heightSizes; | |
private int resizeSpeed = 16; | |
public Resizer(Context context){ | |
// getting the screen's refresh rate and setting size transition based on the speed | |
// for smooth animation | |
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
if (windowManager != null) { | |
resizeSpeed = (int) (1000 / windowManager.getDefaultDisplay().getRefreshRate()); | |
} | |
} | |
public Resizer resizeView(View view, float widthDP, float heightDP, int duration){ | |
this.view = view; | |
layoutParams = view.getLayoutParams(); | |
int viewWidth = view.getWidth(); | |
int viewHeight = view.getHeight(); | |
int width; | |
int height; | |
if (widthDP == DONT_CHANGE) | |
width = viewWidth; // I WILL CHANGE THESE LATER - THIS IS TESTING PHASE | |
else | |
width = dpToPx(widthDP); | |
if (heightDP == DONT_CHANGE) | |
height = viewHeight; | |
else | |
height = dpToPx(heightDP); | |
int stepCount = duration / resizeSpeed; | |
widthSizes = getSizesArray(viewWidth, width, stepCount); | |
heightSizes = getSizesArray(viewHeight, height, stepCount); | |
handler.post(runnableResizeView); | |
return this; | |
} | |
private int setSize = 0; | |
private Runnable runnableResizeView = new Runnable() { | |
@Override | |
public void run() { | |
if (setSize < widthSizes.length){ | |
layoutParams.width = widthSizes[setSize]; | |
layoutParams.height = heightSizes[setSize]; | |
view.setLayoutParams(layoutParams); | |
Log.i("Size #" + setSize, widthSizes[setSize]+""); | |
setSize++; | |
handler.postDelayed(this, resizeSpeed); | |
} | |
else if (onResizerFinishListener != null) | |
onResizerFinishListener.onFinish(); | |
} | |
}; | |
private int[] getSizesArray(int startSize, int endSize, int stepCount) { | |
int[] sizes = new int[stepCount + 1]; | |
float stepSize = (endSize - startSize) / (float) stepCount; | |
// making array | |
sizes[0] = startSize; | |
for (int i = 1; i < stepCount; i++){ | |
sizes[i] = startSize + (int) (stepSize * i); | |
} | |
sizes[sizes.length-1] = endSize; | |
return sizes; | |
} | |
private static int dpToPx(float dp) { | |
return (int) (dp * Resources.getSystem().getDisplayMetrics().density); | |
} | |
OnResizerFinishListener onResizerFinishListener; | |
public void setOnResizerFinishListener(OnResizerFinishListener l){ | |
onResizerFinishListener = l; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment