Well, I don't have that code anymore, I moved on to trying to create the
bitmaps programmatically. I can do that, but as I stated, I can't for the
life of me, figure out how to position the 2 parts at all or control which
one appears on top of the other. Here's the code I'm trying now:
package com.kgreene.gauge;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.Matrix;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ImageView.ScaleType;
public class gauge extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
Bitmap bmSpeedGauge = BitmapFactory.decodeResource(getResources(),
R.drawable.speedo);
BitmapDrawable sGauge = new BitmapDrawable(bmSpeedGauge);
ImageView sgView = new ImageView(this);
// set the Drawable on the ImageView
sgView.setImageDrawable(sGauge);
// center the Image
sgView.setScaleType(ScaleType.CENTER);
// load the origial BitMap
Bitmap bmSpeedNeedle = BitmapFactory.decodeResource(getResources(),
R.drawable.speed_needle);
int width = bmSpeedNeedle.getWidth();
int height = bmSpeedNeedle.getHeight();
int newWidth = 48;
int newHeight = 212;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45, 24, 180);
// recreate the new Bitmap
Bitmap rbmSpeedNeedle = Bitmap.createBitmap(bmSpeedNeedle, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(rbmSpeedNeedle);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add sgView to the Layout
linLayout.addView(sgView,
new LinearLayout.LayoutParams(
433, 433
)
);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
48, 212
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
}
Screenshot: http://kgreene.com/android/gauge.jpg
As you can see, the needle is drawn to the right of the gauge face, and
slightly underneath it.
I realize that I shouldn't expect them to magically appear stacked properly
because I'm not trying to position them at all, and that is part of the
problem. I haven't seen anything that explains how to position or layer
bitmaps at all, aside from very vague references to using linearLayout or
gridLayout. Also note that I'm coming from a web development background, and
this is my first experience with java of any kind. I'm used to being able to
position things exactly where I want them, very easily, with CSS or
javascript.
Basically, I'm looking for the android equivalent of the css properties top,
left and z-index.
Also, if you can point me to an example of number 2 below (creating the
elements in XML and then accessing them programmatically) that would be
great, as that seems to be preferable to creating everything in code.
Thanks,
Keith
On Mon, Jan 4, 2010 at 10:32 PM, Justin Anderson <[email protected]>wrote:
> Please post your XML file and the code you are using to access the
> different parts of it...
>
> ----------------------------------------------------------------------
> There are only 10 types of people in the world...
> Those who know binary and those who don't.
> ----------------------------------------------------------------------
>
>
> On Thu, Dec 31, 2009 at 1:09 PM, keith <[email protected]> wrote:
>
>> Hello all,
>> I'm very new to android development, and I'm coming from a php/web
>> development background. For my first exercise, I'm trying to draw a
>> gauge face and a needle, and be able to rotate that needle to whatever
>> direction I want (not dynamically, just by changing the angle in code
>> and recompiling)
>> I'm running into a problem that is 2-fold:
>>
>> 1, If I build the layout programmatically using LinearLayout, I can
>> rotate the needle, but I cannot manage to get the needle to sit in the
>> proper position on top of the gauge, in fact, I can't seem to be able
>> to control the position of either image at all. I looked into using
>> AbsoluteLayout, but that is depricated.
>>
>> 2. If I use the xml layout, I can position the gauge and needle
>> exactly where I want them, but I cannot seem to access the needle
>> programmatically to rotate it. I have tried setting android:@+id and
>> using getById, but that doesn't do anything, although it doesn't throw
>> an error either.
>>
>> Does anyone have any tips or references or examples on how to
>> accomplish this seemingly easy task?
>>
>> Thanks,
>> Keith
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Android Beginners" group.
>>
>> NEW! Try asking and tagging your question on Stack Overflow at
>> http://stackoverflow.com/questions/tagged/android
>>
>> To unsubscribe from this group, send email to
>> [email protected]<android-beginners%[email protected]>
>> For more options, visit this group at
>> http://groups.google.com/group/android-beginners?hl=en
>>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Beginners" group.
>
> NEW! Try asking and tagging your question on Stack Overflow at
> http://stackoverflow.com/questions/tagged/android
>
> To unsubscribe from this group, send email to
> [email protected]<android-beginners%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-beginners?hl=en
>
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en