Tuesday 20 December 2011

Image Translation,Rotation and Zooming in android


 private Drawable mIcon;

TouchExampleView view;

boolean zoom=true,rotation=false;




in onCreate()  {


 view = new TouchExampleView(this);
 setContentView( view  );


}



   public class TouchExampleView extends View {
        private static final int INVALID_POINTER_ID = -1;
        
       
        private float mPosX;
        private float mPosY;
        
        private float mLastTouchX;
        private float mLastTouchY;
        private int mActivePointerId = INVALID_POINTER_ID;
        
        private ScaleGestureDetector mScaleDetector;
        
        float gx=0,gy=0;
        
        int dg=0;
        float dg1=0;
     private List<Float> mScaleFactor = new ArrayList<Float>();
        
     
     private List<Float> gx1 = new ArrayList<Float>();
     
     private List<Float> gy1 = new ArrayList<Float>();
     
     float mScaleFactor1=1.f;
     
     int mScaleIndex=0;


     
        public TouchExampleView(Context context) {
            this(context, null, 0);
        }
        
        public TouchExampleView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
        
        public TouchExampleView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
           
            mIcon = this.getResources().getDrawable(R.drawable.red_glass);
            mIcon.setBounds(0, 0, mIcon.getIntrinsicWidth(), mIcon.getIntrinsicHeight());
            mIcon.setAlpha(225);  
         
            mScaleFactor.add(1.f); 
            gx1.add(2f);
            gy1.add(2f);
            mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
        }
        
       
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            // Let the ScaleGestureDetector inspect all events.
            mScaleDetector.onTouchEvent(ev);
            
            final int action = ev.getAction();
            switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
           
           
           
                final float x = ev.getX();
                final float y = ev.getY();
                
                Log.d("Touch","DOWN  "+"new x and y"+x+" "+y+"old x and Y"+mLastTouchX+"  "+mLastTouchY);
                
                mLastTouchX = x;
                mLastTouchY = y;
                mActivePointerId = ev.getPointerId(0);
                break;
            }
                
            case MotionEvent.ACTION_MOVE: {
           
           
                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
                final float x = ev.getX(pointerIndex);
                final float y = ev.getY(pointerIndex);


                float x1 = ev.getX(0) - ev.getX(1);
                float y1 = ev.getY(0) - ev.getY(1);
                 dg = (int)(Math.toDegrees(Math.atan2(y1, x1)));
                
                


                
                if(x<mLastTouchX+10 && mLastTouchX>x)
                {
                dg+=5;
                }
                
                
                // Only move if the ScaleGestureDetector isn't processing a gesture.
                if (!mScaleDetector.isInProgress()) {
                    final float dx = x - mLastTouchX;
                    final float dy = y - mLastTouchY;


                    
                 
                   
                    mPosX += dx;
                    mPosY += dy;
                   
                    invalidate();
                }


                mLastTouchX = x;
                mLastTouchY = y;


                break;
            }
                
            case MotionEvent.ACTION_UP: {
            dg=0;
                mActivePointerId = INVALID_POINTER_ID;
                break;
            }
                
            case MotionEvent.ACTION_CANCEL: {
                mActivePointerId = INVALID_POINTER_ID;
                break;
            }
            
            case MotionEvent.ACTION_POINTER_UP: {
           
           
            dg=0;
            Log.d("Touch","ACTION_POINTER_UP");
           
                final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                        >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                final int pointerId = ev.getPointerId(pointerIndex);
                if (pointerId == mActivePointerId) {
                    // This was our active pointer going up. Choose a new
                    // active pointer and adjust accordingly.
                    int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                    
                   if(newPointerIndex<0)
                  newPointerIndex=0;
                    mLastTouchX = ev.getX(newPointerIndex);
                    mLastTouchY = ev.getY(newPointerIndex);
                    mActivePointerId = ev.getPointerId(newPointerIndex);
                }dg=0;
                break;
            }
           
            case MotionEvent.ACTION_POINTER_1_DOWN:Log.d("Touch","ACTION_POINTER_DOWN  1");break;
            case MotionEvent.ACTION_POINTER_2_DOWN:Log.d("Touch","ACTION_POINTER_DOWN        2");break;
            case MotionEvent.ACTION_POINTER_3_DOWN:Log.d("Touch","ACTION_POINTER_DOWN    3");break;
            
            case MotionEvent.ACTION_POINTER_INDEX_SHIFT:Log.d("Touch","ACTION_POINTER_INDEX_SHIFT ");break;
            
            }
            
            return true;
        }


        @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
       
        Log.d("Touch","event"+event+"keycode"+keyCode);
       
        if(keyCode==KeyEvent.KEYCODE_DPAD_UP)
        {
        dg+=10;
        }
       
    return super.onKeyDown(keyCode, event);
    }


    @Override
        public void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            
            canvas.save();
               
            Log.d("HI","baseline onDraw"+seekbarposition+"     onDraw");
            
          
            
            //Image Moving....
            canvas.translate(mPosX, mPosY);
           
           
           
           float msf=mScaleFactor.get(mScaleIndex);
           
         
           
      //Image Rotation....
         canvas.rotate((float) (seekbarposition*3.6)); 
          
         Log.d("Roation Angle","Angle:"+dg1);
         
         
         //Image Scaling....
         canvas.scale(msf, msf);
    
          
           
            mIcon.draw(canvas);
            canvas.restore();
        }


   
    //Code for Image Scaling............
        private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
       
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                mScaleFactor1 *= detector.getScaleFactor();
               
                
               
                
              gx = detector.getFocusX();
    gy = detector.getFocusY();
   
   
    float centerX = view.getWidth()/ 2;
    float centerY = view.getHeight()/ 2;
   
   
    if(rotation){
   
    gx1.add(gx);
        gy1.add(gy);
        mRotateIndex++;
       
        if(mRotateIndex!=0)
        {
       
        float newX=centerX-gx;
        float newY=centerY-gy;
       
        dg1 = (float) (Math.toDegrees(Math.atan2(newY, newX))-90);
       
   
   
     }
   
    Log.d("Touch","onScale  gx...............     "+gx+"gyyyyyyyyyyyyyyyyy:    "+gy
   
    +"mscale factor"+mScaleFactor1);
   
               
                // Don't let the object get too small or too large.
               
    mScaleFactor1 = Math.max(0.1f, Math.min(mScaleFactor1, 1.0f));
               
    Rect r=mIcon.getBounds();


    Log.d("HI","Image height"+r.height());
   
   


               if(zoom){
    mScaleFactor.add(mScaleFactor1);
   
       mScaleIndex++;
               }
               
           
               
              
                invalidate();
               
    }
    return true;
            }


    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
    // TODO Auto-generated method stub
    super.onScaleEnd(detector);
   
   
    }
        }
        
      


    }



Thursday 15 December 2011


Points to Remember while Interacting with friends, colleagues & others.


1) The very first and foremost thing is never Argue! Because argument creates a very bad impression then let it be your friend, colloquies, or any elder person or family member. Just be cool & learn to listen to what the other person has to say even if it is not in your favor & if so then you should know to handle the situation smartly!

2) Have polite vocal quality and should avoid overreaction & over excitements just remember one thing "Be Cool at Your Temperament" & handle the situations.

3) Don't & never let anyone read your mind by looking at your face! But in the process Also Don't forget to be honest with yourself!

4) Always remember the personality difference between yourself & the people whom we interact. We can never accept that opposite person will have same views, thought process, or learning style & learn to respect these differences to have healthy relationships.

5) Learn to be reliable & faithful for opposite person. We should learn to keep things confidential if things need to be or should have a sound understanding of which things we should speak, when & in front of whom.

6) You should know when to accept your own faults. If it is your mistake then admit it irrespective of your ego especially when the opposite person is someone very special like your father or any near one like him.

7) As now to know to admit your own faults you should also know to forgive our near ones, remember we all are humans & humans have normally tendency to commit mistakes but take them positively & also learn to forgive others.

8) Based on above two points regarding accepting & committing the mistakes also learn to point out others mistakes tactfully & telling them in the sense that won’t hurt the opposite person.

9) Be encouraging to opposite person so that opposite person likes to talks with you & share his views & ideas. Never have a passive attitude as nobody likes to interact with people having such personality types.

10) The last but not the least final point is to try implementing the above points & see the results!



Sample Android Login Application


After developing wide range of Java applications including Web and Desktop based since about four and half years now I have started learning Android Since last one month and day by day I'm getting very keen in learning the Same.

Following is the Sample Android Login Application which I have developed and would like to share with everyone.
This Application is at its very basic stage, Irrespective of the database used.

It works as follows:
If User Name and Password Entered are similar it gives a Toast pop up saying that "Login Successful"
Else It will give a Toast pop up Saying that "Invalid Login"

I feel it will help the Android learning beginners:

This application is developed on Following platforms:
Ubuntu 10.10,
Eclipse 3.5 Galileo
Android 2.1 SDK

Following is the Screen short of the Application:



Following is the Screen short of the Application:

I have used Table Layout, Following is the Layout Code for the Same:


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
     
<TableRow>
<TextView 
android:text="User Name: " 
android:id="@+id/TextView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</TextView>

<EditText 
android:text="" 
android:id="@+id/txtUname" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">
</EditText>
</TableRow>


<TableRow>
<TextView 
android:text="Password: " 
android:id="@+id/TextView02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</TextView>


<EditText 
android:text="" 
android:id="@+id/txtPwd" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:password="true">
</EditText>
</TableRow>


<TableRow>
<Button
android:text="Cancel" 
android:id="@+id/btnCancel" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">
</Button>


<Button
android:text="Login" 
android:id="@+id/btnLogin" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content">
</Button>


</TableRow>


</TableLayout>



Following is the Activity Code for the Application:


package com.mayuri.login;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class SampleLogin extends Activity {

EditText txtUserName;
EditText txtPassword;
Button btnLogin;
Button btnCancel;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        txtUserName=(EditText)this.findViewById(R.id.txtUname);
        txtPassword=(EditText)this.findViewById(R.id.txtPwd);
        btnLogin=(Button)this.findViewById(R.id.btnLogin);
        btnLogin=(Button)this.findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
         Toast.makeText(SampleLogin.this, "Login Successful",Toast.LENGTH_LONG).show();
        } else{
         Toast.makeText(SampleLogin.this, "Invalid Login",Toast.LENGTH_LONG).show();
        }

}
});       
    }





Following are the Screenshots of the Applications, Depending on their behaviours i.e. "Login Success" and "Invalid Login".