Friday, January 10, 2014

Resizeable ImageView in android:

package com.yourpackage;
 import android.content.Context;
 import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.widget.ImageView; 


 public class ResizableImageView extends ImageView { 
          public ResizableImageView(Context context, AttributeSet attrs) {
          super(context, attrs); 
 }
 public ResizableImageView(Context context) {
      super(context); 
 } 
 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
                   Drawable d = getDrawable(); if (d == null) {                           super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); 
                  return; 
 } 
 int imageHeight = d.getIntrinsicHeight(); 
 int imageWidth = d.getIntrinsicWidth(); 
 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
 int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
 float imageRatio = 0.0F; 
 if (imageHeight > 0) { 
 imageRatio = imageWidth / imageHeight; 
 } 
 float sizeRatio = 0.0F; 
 if (heightSize > 0) { 
 sizeRatio = widthSize / heightSize;
 } 
 int width; int height; 
 if (imageRatio >= sizeRatio) { 
 // set width to maximum allowed width = widthSize; 
 // scale height height = width * imageHeight / imageWidth; 
 } else {
 // set height to maximum allowed height = heightSize; 
 // scale width width = height * imageWidth / imageHeight; 
 }
 setMeasuredDimension(width, height);
 }
 }

THIS IS THE IMAGEVIEW:
`< com.YOURPACKAGENAME.ResizableImageView 
android:id="@+id/imagetweet" 
android:layout_width="match_parent" 
android:layout_height="160dp"
 android:layout_alignParentLeft="true"
 android:layout_alignParentTop="true" 
android:adjustViewBounds="true"
 android:scaleType="centerCrop" />

Saturday, December 21, 2013

How To Change Unix TimeStamp To Date,Days and Hours in Java and Android.

long timeInMilliseconds = 1388205000; 
long end=timeInMilliseconds*1000; 
long current = System.currentTimeMillis(); 
long diff = end - current ;
 int hrCount = (int) ((diff / (1000 * 60 * 60)) % 24); 
int dayCount = (int) diff / (24 * 60 * 60 * 1000);

Sunday, December 15, 2013

Crop image in circular shape in android.

Public Class Rounder{
           public Bitmap getRoundedShape(Bitmap scaleBitmapImage) { 
            int targetWidth = 125;
            int targetHeight = 125; 
           Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,            targetHeight,Bitmap.Config.ARGB_8888); 

         Canvas canvas = new Canvas(targetBitmap);  
         Path path = new Path(); 
         path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2, (Math.min(((float)     targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW); 
        canvas.clipPath(path); 
        Bitmap sourceBitmap = scaleBitmapImage; 
       canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),    sourceBitmap.getHeight()), new Rect(0, 0, targetWidth, targetHeight), null); 
 return targetBitmap; }

}

and use it as

Bitmap roundedBitmapImage=new Rounder().getRoundedShape(YourNormalBitmapImage);