Tuesday, September 27, 2011

Grid, Adaptor, left to right into top to bottom

Say we have list of object as follows,
list =
.size() = 14
and need to show it in grid control that scroll vertically as follows,
// assume we want three columns.
int columns = 3;
// first calculate number of rows.
int rows = (columns-1+list.size())/columns;

Or into grid control that scroll horizontally as follows,
//assume we want three rows.
int rows = 3;
// first calculate number of rows.
int columns = (rows-1+list.size())/rows;

Mostly we use adaptor to fetch the item to show in each cell.
In this case adaptor can choose the index equal to position, 

int index = position;
return list.get(index); 

But say some cases we may have to change to order in display. for example. top to bottom as shown below:
But if the control fetch base on position from left to right as show below:
then, we need to add proper calculation inside the adaptor:

int index =position/columns+position%columns*rows;
return list.get(index); 

Thursday, September 15, 2011

Android: Aligning Custom Views at Base Line

Have you ever try to align a non-symmetric custom view at a custom base line as follows?


I tried it using base line attribute of Relative Layout.
First adjust the size of view:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//Height should be always width * 7 / 12
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()*7/12);
}


Then Override the getBaseLine method:
@Override
public int getBaseline() {
//base line is from bottom to width/12 (radius of the blue circle)
return getMeasuredHeight()-getMeasuredWidth()/12;
}


Following onDraw method would explain the reason for the above calculation:

@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setAntiAlias(true);
//Draw the half circle
paint.setColor(Color.GRAY);
float width = getWidth();
float height = getHeight();
canvas.save();
canvas.translate(width/2, height-width/12);
canvas.drawArc(new RectF(-width/3, -width/3, +width/3, width/3), 0, -180, true, paint);
//Draw small circles
paint.setColor(Color.DKGRAY);
RectF rectF = new RectF(-width/2+width/24, -width/12+width/24, -width/3-width/24,                +width/12-width/24);
canvas.drawOval(rectF, paint);
for(int i = 0 ; i < 15 ;i++){
canvas.rotate(12f);
canvas.drawOval(rectF, paint);
}
//Draw blue circle
paint.setColor(Color.BLUE);
canvas.rotate(-180+30);
canvas.drawOval(new RectF(-width/2, -width/12, -width/3, +width/12), paint);
//Draw red arrow
paint.setColor(Color.RED);
Path path = new Path();
path.moveTo(-width*5/12, 0);
path.lineTo(10, -width/12);
path.lineTo(0, 0);
path.lineTo(10, +width/12);
path.close();
canvas.drawPath(path, paint);
canvas.restore();
}


Finally, create a layout xml as following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<demo.ui.testing.DemoUITesting
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:layout_alignBaseline="@+id/radioButton1"
android:layout_toRightOf="@+id/radioButton1">
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/radioButton1"
android:layout_centerVertical="true">
</RelativeLayout>

Enjoy!.

Wednesday, September 14, 2011

Android: convert Immutable Bitmap into Mutable

Android provide Bitmap class to manipulate images. We can load, draw, edit or save. But incase of editing bitmap should be mutable.

For example:
Canvas canvas = new Canvas(mBitmap);
Will throw IllegalArgumentException: image is immuable ...

An image file can be loaded in to bitmap using BitmapFactory as follows:

mBitmap = BitmapFactory.decodeFile(path);

But this will be an immutable bitmap. This will not be able to edited.

Anyway can load image using BitmapFactory.decodeFile(path,options);
Here BitmapFactory.Options options = new BitmapFactory.Options();

But from API Level 11 only options.inMutable available to load the file into a mutable bitmap.

So, if we are building application with API level less than 11, then we have to find some other alternatives.

One alternative is creating another bitmap by copying the source bitmap.
mBitmap = mBitmap.copy(ARGB_8888 ,true);

But the will throw OutOfMemoryException if the source file is big. Actually incase if we want to edit an original file, then we will face this issue. We should be able to load at-least image into memory, but most we can not allocate another copy into memory.

So, we have to save the decoded bytes into some where and clear existing bitmap, then create a new mutable bitmap and load back the  saved bytes into bitmap again. Even to copy bytes we cannot create another ByteBuffer inside the memory. In that case need to use MappedByteBuffer that will allocate bytes inside a disk file.

Following code would explain clearly:

//this is the file going to use temporally to save the bytes. 

File file = new File("/mnt/sdcard/sample/temp.txt");
file.getParentFile().mkdirs();

//Open an RandomAccessFile
/*Make sure you have added uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
into AndroidManifest.xml file*/
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); 

// get the width and height of the source bitmap.
int width = bitmap.getWidth();
int height = bitmap.getHeight();

//Copy the byte to the file
//Assume source bitmap loaded using options.inPreferredConfig = Config.ARGB_8888;
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, width*height*4);
bitmap.copyPixelsToBuffer(map);
//recycle the source bitmap, this will be no longer used.
bitmap.recycle();
//Create a new bitmap to load the bitmap again.
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
map.position(0);
//load it back from temporary 
bitmap.copyPixelsFromBuffer(map);
//close the temporary file and channel , then delete that also
channel.close();
randomAccessFile.close();

Hope this wil help you.

I have uploaded the sample source code here:
http://dl.dropbox.com/u/7717254/DemoPaint.zip
In which application, user can load a image and draw and save back. In case of saving back to original image size.


Friday, June 10, 2011

எப்படியும் ஒருநாள் எம் முற்றம் மிதிப்போம்தானே!




இருவது வருஷம் ஆச்சு...
இருநூறு வசந்தம் போச்சு...

குண்டுபோட்டு கலைத்தனர் - வந்து
கூரை குலைத்து போயினர்
கல்லை மட்டு விட்டதனால் - வெறும்
கட்டிடமாய் நிண்டு கொண்டு - என்னிடம்
இன்று நீ என்ன தான் கேட்கிறாய்?
இத்தனை நாளாய் நாம் எங்கிருந்தோம் என்றா?

அங்க...
ஆக்கிரமித்தவன் நின்டிருப்பான்,
அவன் கூலிக்காரன் வந்திருப்பான்
வேறுசிலர் கண்டிருப்பாய் - அவர்கள்
வேவுபாக்க நுழைந்திருப்பர் - நமக்காய்
போராட துணிந்தவர்கள்,
படைகள் பல அமைத்தவர்கள்
உயர் பாதுகாப்பிலும்
ஊடுருவ துணிந்தவங்கள்
இவர் எல்லாம் இருந்தும் நானோ...
போராளி ஆகவில்லை
போர்க்களம் ஒன்றும் போகவில்லை

ஊர் ஊராய் ஓடி,
உணவு, உடுப்பு...
அது கிடக்க - அல்லது
அது கிடைக்க,
படிப்பு படிப்பு என்று அலைந்து,
பல்கலைக்கழகம் சென்று, பட்டமும் பெற்று,
பட்டமளிப்பு விழாவுக்கு, பட்டாளத்துடன் போய்
படம் எடுத்து, பெரிசாக்கி - அதுக்கும் சட்டம் போட்டு - கடைசீல
அதை மாட்ட சுவரில்லை
அன்றரிந்தேன் உன்னிலையை.
நீ மட்டும் இல்லை - நானும்
வெறும் கட்டிடம் தானென்று.

அன்று...
நிமிர்ந்து நீ நிக்க, - உன்
நினைவாக எடுத்த படம்
கடைசிமட்டு இருந்ததுதான் - அதுவும்
கடைசி போரில் கைவிட்டு போனதல்லே

உயிர் பயத்தில் நான் இங்கு
உயர் பாதுகாப்பில் நீ அங்கு - அன்று
நிழலுக்காய் நட்டமரம் வேர்விட்டு - இன்று உன்னை
நிலைகுலைக்க போகுதாம்
அந்நிய நாட்டவர் எல்லாம்
அவனோட சேந்து, நாடகம் போட்டு,
அத்தனையும் அளித்துவிட்டு
நடைபிணமாய் எம்மை எல்லாம்
நாட்டை விட்டே துரத்திவிட்டு
எங்கள் சொந்த வீட்டை பாக்க
எமக்கே அனுமதிகுடுக்க
அண்ணாவும், தங்கையும் அத்தானும் - உன்னை
ஆசையா பாக்கவந்து
அத்தனையும் சொன்னார்கள்.
உனக்கு மட்டுமல்ல, உலகத்துக்கும் உதேநிலைதான்

இத்தனை கடந்தும்
இன்றைக்கும் சொல்கிறேன் நான்
எப்படியும் ஒருநாள் எம் முற்றம் மிதிப்போம்தானே!


Saturday, February 12, 2011

Part 6: Thoughts of “How we do?” - Easy to Understand or Efficient in Execution


Task is this, we need to hang advertisement cards on the handrail of this train. But we have two types of advertisement cards say red and blue and the pattern should be like red blue red blue red, red blue red blue red.
Anyway, you are not suppose do this, workers will be given to you, you have to give him instructions and ask him to do.
You have two options to do this:
Option 1:
First give the red advertisement cards to the worker and hang on the start, middle and end of each handrails, then give the blue advertisement cards and ask to hang in between every red advertisement cards.
Option 2:
Ask the worker to arrange the advertisement cards as RBRBRRBRBR... then get one by one from the bundle and hang 5 in each handrail.

Lets check these options; in the 2nd option, we do some prearrangement, that means we have added another layer of abstraction and complexity. Need to analyze and understand that layer and test completely before use. In first option worker has to go along train two times, but in 2nd option if he goes once it is enough, the execution is efficient. But in first option we can start the task immediately or need very less time to analyze and understand. In 2nd option, need to spend some more time before start the task, to analyze and understand , is not easy to understand.

This is a simple example, but you may face lot more complex sample in your life. In business, sometimes, the start time of task is more critical than efficient of execution. for example, in option 1, you may be able to add another worker to hang blue cards. If you spend more time in analyzing then there are chances to loose the business. But sure, you cannot execute the business for long with an inefficient execution, you should analyze and make execution efficient. It is your choice to take according to your situation. But keep in your mind you have these options to choose from.

For programers, here is an example in Android, say you want to resize and crop an image.

Option 1 would be:


intermediate = Bitmap.createScaledBitmap(source, dstWidth, dstHeight, filter);

target = Bitmap.createBitmap(intermidiate, x, y, width, height);


Option 2 would be:


Canvas canvas = new Canvas(target);

Matrix matrix = new Matrix();

matrix.preScale(sx, sy);

matrix.postTranslate(dx, dy);

canvas.drawBitmap(source, matrix, paint);


Sure, there are lot more examples, you can find yourself and enjoy.



Friday, November 26, 2010

எப்படியும் ஒருநாள் திரும்பி போறதுதானே

ஆறு பரப்பு காணிவித்து...
ஐந்து பரப்பு காணி வாங்கி...

அப்பப்பா வந்து ...
அடிக்கல் வைத்து...
அத்திவாரம் போட்டு ...

கருத்தகொளும்பான் மரத்திடியில் ...
கலரிந்து, அடுக்கி, தண்ணிவிட்டு ...

சுத்திச் சுவர் எழுப்பி - அதில
கதவு வச்சு ...
யன்னல் வச்சு - அதுக்கு
மழை பட கூடாது என்று - சன்செட்
முற்றத்தில போட்டிக்கோ ...

வெளிச்சுவர் இருக்க ...
உட்சுவர் பூசி...
ஊருக்கெல்லாம் சொல்லி - குடிபூரல் ....

வைரவர் கோயிலிருந்து - படம்
வழி நடையா கொண்டுவந்து ...
பெரிய மாமா தேங்காய் உடைக்க ...
பெரிய அறையில சாமிப்படம்...

தும்பிக்கை சரிபார்த்து - பிள்ளையார் படம் ...
துணைவி வள்ளி, மனைவி தெய்வானை, முருகன் ...
மூன்று பெரும் நிக்கிற ஒரு படம்...
முக்கியமா அவையால் இருக்கவேணும்!

இலச்சுமி படத்துக்கு ...
இலச்ச கணக்குல கண்டிஷன் - வீட்டில
தங்கம் குவியவேண்டும் என்று - காசெல்லாம்
தட்டில விழவேண்டும் - சிதறக்கூடாது ...

அத்திவாரம் போட்டு ...
ஆறு மாதத்தில் அடி வைத்தாலும் ...
ஆடு மாடு கட்ட என்று ...
அருகில ஒரு கொட்டில் ...

எல்லாரும் இப்படித்தான் வீடுகட்டுறது...
எல்லாம் கட்டி என்ன செய்தது - கடைசீல...
எவனோ திறத்த ஓடிவந்ததுதான்...
"எப்படியும் ஒருநாள் திரும்பி போறதுதானே"
எத்தனை காலம் போட்டு இப்படி சொல்லிச் சொல்லி...

இருபது வருஷம் தாண்டி...
இப்ப விடுகினமாம் ...
..................................
இன்னமும் எழுதவேணும் ...

Tuesday, November 23, 2010

Android: When to persist data or release resources if that is expensive?

android: When to release resources?

In android, life-cycle of each application is mostly controled by its Activities. Each Activity has its own left-cycle, you can get detailed information from this link.

Mostly, these Activities interact with user while those are in foreground. Android will put the activity to background in following cases:

1. If the activity starts another activity

2. or in any interrupts, like as, got a incoming call or pressing home button etc.

And if finish() called or back button pressed, it will stop the activity.

Here the problem is with the activities which are brought to background. In one point of time android will stop and remove these activities from the activity stack. There is no guarantee as android will call any call back method before it stops those activities.

Only guaranteed call back is onPause, which will be called while an activity go to background. So, It is recommended to persist state or necessary data and release all allocate resources (Files may have opened in Native or allocated memory etc.)

If you look at the following diagram, you will understand all onPuase calls.





But, in some case it is more expensive to persist the state of application data and releasing resources in every onPause calls. Actually we don’t need to do this if user moving around inside our application. In the above diagram, in each onPause call if it with green pop-up, then we don’t need to persist or release. In other cases we need.

Additionally android provide isFinishing() API to check whether the activity is finishing or not. But that is not clearly provide an idea when to do the persistence and not. We need another flag to indicate while an activity pauses due an new child activity started from that activity.

If i put this into a table:

Main Activity

Child Activity

Start Activity

don’t persist

don’t persist

call finish() or back pressed

persist

don’t persist

Home button pressed, got an incoming call

persist

persist

Let check the following class to track for staring child activity state.

import android.app.Activity;

import android.content.Intent;

public class TscaActicity extends Activity {

private boolean mStartingChildActivity;

public void setStartingChildActivity(boolean startingChildActivity) {

mStartingChildActivity = startingChildActivity;

}

public boolean isStartingChildActivity() {

return mStartingChildActivity;

}

@Override

protected void onResume() {

setStartingChildActivity(false);

super.onResume();

}

@Override

public void startActivityForResult(Intent intent, int requestCode) {

setStartingChildActivity(true);

super.startActivityForResult(intent, requestCode);

}

/... other start * call should flow the same patter ../

}

Let put both isFinishing() and isStartingChildActivity() together in a table:

Main

Main

Child

Child

isFinishing()

isStarting

ChildActivity()

isFinishing()

isStarting

ChildActivity()

Start Activity

false

true

false

true

call finish() or back pressed

true

false

true

false

Home button pressed, got an incoming call

false

false

false

false

So finally, we need to write a Main Activity and Child Activity as follows:

import com.muvee.studio.activity.TscaActicity;

public class MainActivity extends TscaActicity {

@Override

protected void onPause() {

if(!isStartingChildActivity()){

//persist

//release

}

super.onPause();

}

}

import com.muvee.studio.activity.TscaActicity;

public class ChildActivity extends TscaActicity {

@Override

protected void onPause() {

if(!isFinishing() && !isStartingChildActivity()){

//persist

//release

}

super.onPause();

}

}