Showing posts with label Canvas. Show all posts
Showing posts with label Canvas. Show all posts

Wednesday, August 1, 2012

How to get the Dimension of layouts in android ?


In Android, you attach a view tree to your Actitity in order to be able to interact with the UI.
Once this is done, you can get references to your widgets (views) using a call like this from your Activity:
TextView t = (TextView) findViewById(R.id.txtName);

Now, views all sport a method like this: t.getHeight()
Which, presumably returns the actual height of that view element. Problem is, this method invariably returns zero.

At first I thought it was because I was calling this during the onCreate() method in my activity class. Perhaps the layout wasn't fully calculated at this time. So I moved my code to the onResume() method. This method is called whenever the activity is entered or re-entered. But again, no luck. Strange, as the layout at this point should be fully ready to display. I think there's a bug here in Android (I'm at version 2.2 currently.)

Luckily, I accidentally hit the search button while running a test on my EVO and, lo and behold, the height values I was trying to report on a TextView in my layout suddenly had non-zero values in them. Obviously, some sort of layout process had been forced, and the state of the view tree had been updated.

How can I get notified when the layout is fully calculated? The short answer is to attached a listener for a callback when the layout is calculated. Here's what it looks like.

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  RelativeLayout vMain = (RelativeLayout) 
     this.getLayoutInflater().inflate(R.layout.main, null);
  vMain.getViewTreeObserver().addOnGlobalLayoutListener(
  new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout() {
      DisplayLayoutDimensions();
    }
  });
  setContentView(vMain);
}




public void DisplayLayoutDimensions()
{
  StringWriter sw = new StringWriter(1000);
  PrintWriter out = new PrintWriter(sw);
  TextView t = (TextView) findViewById(R.id.textview);
  ImageView img = (ImageView) findViewById(R.id.hp67);
  out.printf("\nImage Drawable\n");
  out.printf("ImageView Height dp: %d\n", img.getHeight());
  out.printf("ImageView Measured Height dp: %\n",
     img.getMeasuredHeight());
  t.setText(sw.toString());
}

Sunday, July 22, 2012

Get the color of a specific pixel Java Android example ?


ImputStream      is = this.getResources().openRawResource(R.drawable.colors);
Bitmap    mBitmap2 = BitmapFactory.decodeStream(is);

int  w = mBitmap2.getWidth();
int  h = mBitmap2.getHeight();
//  int x , y have to be smaller as w , h
int _color =  mBitmap2.getPixel(x, y);

Saturday, March 24, 2012

How to write text through Canvas in android?


public class CanvasGreetingActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(new SampleView(this));
 }

 private static class SampleView extends View {
   public SampleView(Context context) {
     super(context);
     setDrawingCacheEnabled(true);
   }

   @Override
   protected void onDraw(Canvas canvas) {
     canvas.drawColor(Color.WHITE);

     String blackText = "black";
     String redText = " red";

     Paint mPaint = new Paint();
     mPaint.setAntiAlias(true);
     mPaint.setTextSize(30);
     mPaint.setTypeface(Typeface.create(Typeface.SERIF,Typeface.ITALIC));

     float canvasWidth = canvas.getWidth();
     float blackTextWidth = mPaint.measureText(blackText);
     float sentenceWidth = mPaint.measureText(blackText + redText);
     float startPositionX = (canvasWidth - sentenceWidth) / 2;

     mPaint.setTextAlign(Paint.Align.LEFT);
     canvas.translate(0, 80);

     mPaint.setColor(Color.BLACK);
     canvas.drawText(blackText, startPositionX, 0, mPaint);
     mPaint.setColor(Color.RED);
     canvas.drawText(redText, startPositionX + blackTextWidth, 0,mPaint);
   }
 }

Friday, March 23, 2012

Combining 2 Images in Android using Canvas ?


Below are the methods to canvert the two bitmap in to one..........


 private BitmapDrawable createUserMarker(Bitmap bmp1, Bitmap bmp2) {
Bitmap bitmapStar = bmp1;
Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawARGB(0x00, 0, 0, 0);
canvas.drawBitmap(bmp2, 0, 0, null);
canvas.drawBitmap(bitmapStar, 2, 2, null);
BitmapDrawable dr = new BitmapDrawable(bmOverlay);
dr.setBounds(0, 0, dr.getIntrinsicWidth(), dr.getIntrinsicHeight());
return (dr);
    }


OR


 public Bitmap combineImages(Bitmap c, Bitmap s) { 
// can add a 3rd parameter 'String loc' if you want to save the new
// image - left some code to do that at the bottom 
   Bitmap cs = null; 
 
   int width, height = 0; 
    
   if(c.getWidth() > s.getWidth()) { 
     width = c.getWidth(); 
     height = c.getHeight() + s.getHeight(); 
   } else { 
     width = s.getWidth(); 
     height = c.getHeight() + s.getHeight(); 
   } 
 
   cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
 
   Canvas comboImage = new Canvas(cs); 
 
   comboImage.drawBitmap(c, 0f, 0f, null); 
   comboImage.drawBitmap(s, 0f, c.getHeight(), null); 
 
   // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
   /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 
 
   OutputStream os = null; 
   try { 
     os = new FileOutputStream(loc + tmpImg); 
     cs.compress(CompressFormat.PNG, 100, os); 
   } catch(IOException e) { 
     Log.e("combineImages", "problem combining images", e); 
   }*/ 
 
   return cs; 
 } 




Monday, February 13, 2012

How to invert bitmap color in android?


public static Bitmap invert(Bitmap src) {
Bitmap output = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
int A, R, G, B;
int pixelColor;
int height = src.getHeight();
int width = src.getWidth();

   for (int y = 0; y < height; y++) {
       for (int x = 0; x < width; x++) {
           pixelColor = src.getPixel(x, y);
           A = Color.alpha(pixelColor);
         
           R = 255 - Color.red(pixelColor);
           G = 255 - Color.green(pixelColor);
           B = 255 - Color.blue(pixelColor);
         
           output.setPixel(x, y, Color.argb(A, R, G, B));
       }
   }

   return output;
}

Friday, February 10, 2012

How to programmatically figure out the screen resolution of android device?

This code will give you the screen resolution at run time.
The size of your application may differ due to the status and title bar.

@Override 
public void onCreate(Bundle icicle) { 
 WindowManager w = getWindowManager(); 
 Display d = w.getDefaultDisplay(); 

 int width = d.getWidth(); 
 int height = d.getHeight();
}