Showing posts with label image. Show all posts
Showing posts with label image. Show all posts

Tuesday, 30 April 2013

create a resize / compress image


Compress and Resize the Image
 
Bitmap compressBitmap(String file, int width, int height) {

  BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
  bmpFactoryOptions.inJustDecodeBounds = true;
  Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
  int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
    / (float) height);
  int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
    / (float) width);
  if (heightRatio > 1 || widthRatio > 1) {
   if (heightRatio > widthRatio) {
    bmpFactoryOptions.inSampleSize = heightRatio;
   } else {
    bmpFactoryOptions.inSampleSize = widthRatio;
   }
  }
  bmpFactoryOptions.inJustDecodeBounds = false;
  bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
  return bitmap;
 }

create a new image file in cache memory

 
public File gettempImagefile(Bitmap bitmap) {
  // create a file to write bitmap data
  File f = new File(context.getCacheDir() + "imges.jpg");
  try {
   f.createNewFile();
  } catch (IOException e) {
   e.printStackTrace();
  }
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  bitmap.compress(CompressFormat.JPEG, 90 /* ignored for PNG */, bos);
  byte[] bitmapdata = bos.toByteArray();
  // write the bytes in file
  FileOutputStream fos;
  try {
   fos = new FileOutputStream(f);
   fos.write(bitmapdata);
   return f;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
 }

How to use / call this methods

 
File image = gettempImagefile(compressBitmap(imagePath, 250, 250));

Saturday, 6 April 2013

Sample 9 patch images


                                                                                                                                                      








  How to use these images?

Well, that’s quit simple,just drop these images in a drawable folder of your project and set it as a background of a button. I’ve put all these images in the drawable-hdpi folder. Created a button style in strings.xml like:
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
<style name="ButtonText">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ffffff</item>
        <item name="android:gravity">center</item>
        <item name="android:layout_margin">3dp</item>
        <item name="android:textSize">30dp</item>
        <item name="android:shadowColor">#000000</item>
        <item name="android:shadowDx">1</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowRadius">2</item>
        <item name="android:paddingLeft">15dp</item>
        <item name="android:paddingRight">15dp</item>
    </style>
Thenn, just create a layout with some buttons:

?
01
02
03
04
05
06
07
08
09
10
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <Button android:text="Button" android:id="@+id/button1" style="@style/ButtonText" android:background="@drawable/btn_army_glossy"></Button>
    <Button android:text="Button" android:id="@+id/button2" style="@style/ButtonText" android:background="@drawable/btn_blue_glossy"></Button>
    <Button android:text="Button" android:id="@+id/button3" style="@style/ButtonText" android:background="@drawable/btn_blue_pink_glossy"></Button>
    <Button android:text="Button" android:id="@+id/button4" style="@style/ButtonText" android:background="@drawable/btn_green_glossy"></Button>
    <Button android:text="Button" android:id="@+id/button5" style="@style/ButtonText" android:background="@drawable/btn_lightblue_glossy"></Button>
    <Button android:text="Button" android:id="@+id/button6" style="@style/ButtonText" android:background="@drawable/btn_pink_glossy"></Button>
</LinearLayout>

Ofcourse it is possible to setup a more rubust environment where a button will have multiple states. Therefore we need a selector file which is just a simple xml file stored in the drawablefolder. Just call it btn_blue_states.xml or something like that. This file will contain state definitions of the buttons:


Disqus for Android