package medusa.theone.waterdroplistview.utils;
|
|
import android.graphics.Bitmap;
|
import android.graphics.Canvas;
|
import android.graphics.PixelFormat;
|
import android.graphics.drawable.Drawable;
|
import android.view.View;
|
import android.view.ViewGroup;
|
|
/**
|
* Created by xiayong on 2015/6/27.
|
*/
|
public class Utils {
|
|
/**
|
* Map a value within a given range to another range.
|
* @param value the value to map
|
* @param fromLow the low end of the range the value is within
|
* @param fromHigh the high end of the range the value is within
|
* @param toLow the low end of the range to map to
|
* @param toHigh the high end of the range to map to
|
* @return the mapped value
|
*/
|
public static double mapValueFromRangeToRange(
|
double value,
|
double fromLow,
|
double fromHigh,
|
double toLow,
|
double toHigh) {
|
double fromRangeSize = fromHigh - fromLow;
|
double toRangeSize = toHigh - toLow;
|
double valueScale = (value - fromLow) / fromRangeSize;
|
return toLow + (valueScale * toRangeSize);
|
}
|
|
/**
|
* set margins of the specific view
|
* @param target
|
* @param l
|
* @param t
|
* @param r
|
* @param b
|
*/
|
public static void setMargin(View target, int l, int t, int r, int b){
|
if (target.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
|
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) target.getLayoutParams();
|
p.setMargins(l, t, r, b);
|
target.requestLayout();
|
}
|
}
|
|
/**
|
* convert drawable to bitmap
|
* @param drawable
|
* @return
|
*/
|
public static Bitmap drawableToBitmap(Drawable drawable) {
|
int width = drawable.getIntrinsicWidth();
|
int height = drawable.getIntrinsicHeight();
|
Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
|
Canvas canvas = new Canvas(bitmap);
|
drawable.setBounds(0, 0, width, height);
|
drawable.draw(canvas);
|
return bitmap;
|
|
}
|
}
|