Sunday 30 September 2012

Android-Rotate an image

Rotateimage.java


package com.example.gallery;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.Matrix;

import android.view.Display;
import android.widget.ImageView;

public class Rotateimage extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
              // or just load a resource from the res/drawable directory:
              Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flo);
               
              // find the width and height of the screen:
              Display d = getWindowManager().getDefaultDisplay();
              int x = d.getWidth();
              int y = d.getHeight();
               
              // get a reference to the ImageView component that will display the image:
              ImageView img1 = (ImageView)findViewById(R.id.imageView1);
               
              // scale it to fit the screen, x and y swapped because my image is wider than it is tall
              Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, y, x, true);
               
              // create a matrix object
              Matrix matrix = new Matrix();
             
              matrix.postRotate(45, 90, 180);
               
              // create a new bitmap from the original using the matrix to transform the result
              Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
               
              // display the rotated bitmap
              img1.setImageBitmap(rotatedBitmap);
}
}




main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/flo"
        
        />

</RelativeLayout> 






Screenshot.






Saturday 22 September 2012

Android-Fetch images from sdcard and display in gridview


Using this code you can fetch images from sdcard and display it in a gridview
Sdcard.java
package com.example.sdcard;



import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class Sdcard extends Activity {

    //  Cursor used to access the results from querying for images on the SD card.

    private Cursor cursor;
   
     // Column index for the Thumbnails Image IDs.
 
    private int columnIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdcard);

        // Set up an array of the Thumbnail Image ID column we want
        String[] projection = {MediaStore.Images.Thumbnails._ID};
        // Create the cursor pointing to the SDCard
        cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,
                MediaStore.Images.Thumbnails.IMAGE_ID);
        // Get the column index of the Thumbnails Image ID
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

        GridView sdcardImages = (GridView) findViewById(R.id.gridView1);
        sdcardImages.setAdapter(new ImageAdapter(this));


    }




// Image adapter to link images to the gridview 


    private class ImageAdapter extends BaseAdapter {

        private Context context;

        public ImageAdapter(Context localContext) {
            context = localContext;
        }

        public int getCount() {
            return cursor.getCount();
        }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView picturesView;
            if (convertView == null) {
                picturesView = new ImageView(context);
                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // Set the content of the image based on the provided URI
                picturesView.setImageURI(Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
                picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
                picturesView.setPadding(10, 10, 10, 10);
                picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
            }
            else {
                picturesView = (ImageView)convertView;
            }
            return picturesView;
        }
    }
}


Here is the main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
   
        android:columnWidth="90dp"
         android:stretchMode="columnWidth"
         android:gravity="center"
        android:numColumns="2" >
    </GridView>

</RelativeLayout>


Friday 21 September 2012

Reflection for image in Android

Using this code you can make a a reflection for your picture in imageview

imgreflection.java

package com.example.imgshadw;

import android.os.Bundle;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;

public class Imgreflection extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_imshdw);
       Bitmap originalImage = BitmapFactory.decodeResource(getResources(),
               R.drawable.waterfall);
    


   //Create an Image view and add our bitmap with reflection to it
      ImageView imageView = new ImageView(this);
      imageView.setImageBitmap(getRefelection(originalImage));

      //Add the image to a linear layout and display it
      LinearLayout linLayout = new LinearLayout(this);
      linLayout.addView(imageView,   new LinearLayout.LayoutParams(   LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT                  )            );
        
       // set LinearLayout as ContentView
       setContentView(linLayout);
   }

   public Bitmap getRefelection(Bitmap image)
   {
        //The gap we want between the reflection and the original image
       final int reflectionGap = 4;
    
       //Get you bit map from drawable folder
       Bitmap originalImage = image ;
    

       int width = originalImage.getWidth();
       int height = originalImage.getHeight();
    

       //This will not scale but will flip on the Y axis
       Matrix matrix = new Matrix();
       matrix.preScale(1, -1);
    
       //Create a Bitmap with the flip matix applied to it.
       //We only want the bottom half of the image
       Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);
    
        
       //Create a new bitmap with same width but taller to fit reflection
       Bitmap bitmapWithReflection = Bitmap.createBitmap(width
         , (height + height/2), Config.ARGB_8888);

      //Create a new Canvas with the bitmap that's big enough for
      //the image plus gap plus reflection
      Canvas canvas = new Canvas(bitmapWithReflection);
      //Draw in the original image
      canvas.drawBitmap(originalImage, 0, 0, null);
      //Draw in the gap
      Paint deafaultPaint = new Paint();
      canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
      //Draw in the reflection
      canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);

      //Create a shader that is a linear gradient that covers the reflection
      Paint paint = new Paint();
      LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
        bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
        TileMode.CLAMP);
      //Set the paint to use this shader (linear gradient)
      paint.setShader(shader);
      //Set the Transfer mode to be porter duff and destination in
      paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
      //Draw a rectangle using the paint with our linear gradient
      canvas.drawRect(0, height, width,
        bitmapWithReflection.getHeight() + reflectionGap, paint);
      return bitmapWithReflection;
   }
}

create your main. xml with an imageview in it.



Screenshot for the Application








Horizontal page swipe using view pager

First create an activity viewpager.java

Viewpager.java

package com.example.viewpager;


import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class Viewpager extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        MyPagerAdapter adapter = new MyPagerAdapter();
       
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(1);
    }
}


Then create a pagerAdapter to link the pageviewer.

mypageradapter.java

package com.example.viewpager;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

public class MyPagerAdapter extends PagerAdapter {
   
    public int getCount() {
        return 3;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           
        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.left;
            break;
        case 1:
            resId = R.layout.middle;
            break;
        case 2:
            resId = R.layout.right;
            break;
      
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }


    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public Parcelable saveState() {
        return null;
    }
}


Thursday 20 September 2012

Simple Listview Activity

 
 
 This is a simple ListActivity which extends ListActivity
 
 Country_list.java
 
 
 package com.example.Listview;
 
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class Country_list extends ListActivity {
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] { "India", "Pakistan", "Sri Lanka",
        "China", "Bangladesh" ,"Nepal","Afghanistan"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }
} 
 
 
main.xml 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>