Skip to content

Instantly share code, notes, and snippets.

View nalitzis's full-sized avatar
🎯
Focusing

Adolfo Bulfoni nalitzis

🎯
Focusing
  • Amsterdam
View GitHub Profile
@nalitzis
nalitzis / subsample.java
Created June 7, 2014 20:46
Subsampling
private void subSampleImage(int powerOf2) {
if(powerOf2 < 1 || powerOf2 > 8) {
Log.e(TAG, "trying to apply upscale or excessive downscale: "+powerOf2);
return;
}
final Resources res = getActivity().getResources();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = powerOf2;
final Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.brasil, options);
private void readBitmapInfo() {
final Resources res = getActivity().getResources();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, R.drawable.brasil, options);
final float imageHeight = options.outHeight;
final float imageWidth = options.outWidth;
final String imageMimeType = options.outMimeType;
Log.d(TAG, "w,h, type:"+imageWidth+", "+imageHeight+", "+imageMimeType);
Log.d(TAG, "estimated memory required in MB: "+imageWidth * imageHeight * BYTES_PER_PX/MemUtils.BYTES_IN_MB);
@nalitzis
nalitzis / availableram.java
Last active January 13, 2016 07:32
Available RAM
public static final float BYTES_IN_MB = 1024.0f * 1024.0f;
public static float megabytesFree() {
final Runtime rt = Runtime.getRuntime();
final float bytesUsed = rt.totalMemory();
final float mbUsed = bytesUsed/BYTES_IN_MB;
final float mbFree = megabytesAvailable() - mbUsed;
return mbFree;
}
public class TestSample extends AndroidTestCase{
private int mFinalValue;
private static final int EXPECTED_VALUE = 10;
private final HandlerThread mHandlerThread;
private SampleClass mInstance;
public void testDoAsync(){
mInstance = new SampleClass();
public class TestSample extends AndroidTestCase{
private int mFinalValue;
private static final int EXPECTED_VALUE = 10;
public void testDoAsync(){
SampleClass s = new SampleClass();
Listener l = new ListenerImpl();
Semaphore semaphore = new Semaphore(0);
s.doAsync();
semaphore.acquire();
public class TestSample extends AndroidTestCase{
public void testDoAsync(){
SampleClass s = new SampleClass();
Listener l = new ListenerImpl();
s.doAsync();
}
private class ListenerImpl implements Listener{
public void onValueChanged(int i){
public interface SampleClass {
void doAsync(Listener l);
public interface Listener{
void onValueChanged(int i);
}
}
@nalitzis
nalitzis / resize.java
Last active December 19, 2015 16:59
resize dynamically a big view and observe it through view tree observer
@Override
protected void onCreate(Bundle savedInstanceState) {
//... init code ...
ViewTreeObserver vto = mRootView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListenerImpl());
}
private void resizeBigView(){
@nalitzis
nalitzis / MeasureButton.java
Created July 12, 2013 20:27
get view size through View.MeasureSpec
private Pair<Integer,Integer> measureButton(){
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
mButton.measure(widthMeasureSpec, heightMeasureSpec);
int h = mButton.getMeasuredHeight();
int w = mButton.getMeasuredWidth();
Log.d(TAG, "height of button is :"+h+", width is :"+w);
return new Pair<Integer, Integer>(w,h);
}
boolean signal = false;
synchMethod(){
// do something...
callAsycnMethod(callback);
synchronized(this){
while(!signal){
try{