Magnetic sensor and image rotation in Android - java

trying to build an magnetic compass, I have the following code.
public void onSensorChanged(SensorEvent event) {
imageCompass = (ImageView) findViewById(R.id.imageMapDrawView);
Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.compass);
Matrix matrix = new Matrix();
matrix.postRotate(event.values[0] );
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
matrix, true);
imageCompass.setImageBitmap(rotated);
}
I have the following questions,
A. I guess event.values[0] is in degree? not in radian?
B. I want to get the image from image view and rotate it around the center of the image, where I am telling that?
C. I want to draw another image (an indicator on top of that Image), can I do this? I have already an compass image in the image view and I want to draw another image on top. I can't redraw whole view. How can I achieve it?

A. Check http://developer.android.com/reference/android/hardware/SensorEvent.html.
The length and contents of the values array depends on which sensor type is being monitored.
Sensor.TYPE_ACCELEROMETER is in m/s²
Sensor.TYPE_MAGNETIC_FIELD is in uT (micro Tesla)
Sensor.TYPE_GYROSCOPE is in rad/s
and go on for all sensors (check the link).
Also, interface SensorListener is Deprecated since API level 3. You should use SensorEventListener instead.
B and C. You should consider using OpenGL ES for drawing your images. Specially OpenGL ES 2.0 gives you so much details and ways manipulate your images that would be easy to do let you do about anything you need.

Related

Libgdx get Texture from Image or Drawable

I would like to get the Texture data out of an image, so it may be rendered directly, but I see no straightforward way of making it, google searches show the other way around only.
I am able to get the Drawable, but the interface doesn't specify the exact texture data, so there's no way that I see to convert an Image actor into a Texture.
What I am trying to achieve is to have brushes, where the data of the brushes are stored in the image of an ImageButton. So upon clicking on an ImageButton The user would be able to draw on the screen based on the image stored in the buttons.
How might I be able to do that?
After digging deeper, I found a solution on the forums:
Your best bet is probably to draw your textures to a FrameBuffer with
a 1:1 scale, then you can use
ScreenUtils.getFrameBufferPixmap
To copy a pixmap off the FrameBuffer and resize as you wish.
Something like this:
fb.begin();
sb.begin();
sb.draw(texture, 0, 0);
sb.end();
Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);
fb.end();
It's not exactly a texture, but it is exactly what I would have needed: the pixel data extracted from a drawable.
After much thought I decided that it is easier to just store the texture data redundantly next to to the Imagebuttons, and query directly the stored data, instead of using the ImageButton as a kind of container, which it shouldn't be.

Options to put corners in Android ImageView

I'm trying to put corners im my ImageView and I find these "workarounds":
1 - Take the Bitmap soure and paint then
2 - put a second ImageView on my layout and use shape+corners as its source
It's not possible take the image view and put a corner around then?
In my app i have a List with a lot of ImageView (the Bitmap used in this ImageView's is programmatically) and I want to put corners in every ImageView.
There is any other option?
You can create a selector that contains shape with rounded corners, and then apply it as the ImageView's background using xml.
Look at this answers here
You can also do it via code by using PorterDuffXfermode. PorterDuffXfermode uses alpha compositing that will allow you to create and intersection (things of mathematical sets here) between the Canvas you get from onDraw() and an off screen Canvas you will have to create. You will use one of the PorterDuff.Mode flags to tell the framework you want to only render the pixels that intersect from the two Bitmaps in each Canvas.
More on Alpha Compositing

How to resize a Bitmap while runnning in Android

I have a Panel class, which extends SurfaceView. The panel fills my whole activity. Inside the panel I draw a Ball - Bitmap (and some other stuff(lines/squares)).
I add some pseudo depth. Something like 2.5 D, not really 3D, but kind of. But now I am facing a problem, when the Ball goes into depth I don't know how to re size it. Because the Ball is constantly moving back and forth (left/right, up/down too).
I don't know where to read about it, but I think if I add the Bitmap to an ImageView, this would solve all my problems.
My First question, does this even solve my problem? And if not, how else could I solve it.
And my second question is, how do I add an Imageview on a SurfaceView?
I found some good hints here: Implement a ImageView to a SurfaceView
but it only says what to do, not how to do it. Because nothing happens in my activity. Everything happens in my Panel.
I think the answer to one of my questions may help you: Unable to draw on top of a custom SurfaceView class. It's not possible to add a ImageView into a SurfaceView because SurfaceView doesn't extent the ViewGroup class, but as that answer shows, it is straightforward to draw on top of a SurfaceView.
In that question, I had a custom view called DrawOnTop which inherited directly from View, and I drew resized and rotated bitmaps onto that view's canvass without any problems. My guess is that you'll be able to do the same will your ball object. The code I used was as follows:
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap);
int bitmapOrg_width = bitmapOrg.getWidth();
int bitmapOrg_height = bitmapOrg.getHeight();
float bitmapScaleFactor = 1.5; // to create a resized bitmap 50% bigger
float angle = 90; // to rotate by 90 degrees
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(bitmapScaleFactor, bitmapScaleFactor);
// rotate the Bitmap
matrix.postRotate(angle);
// create the resized bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, bitmapOrg_width, bitmapOrg_height, matrix, true);
canvas.drawBitmap(resizedBitmap, left, top, null);

Android - Canvas.drawBitmap doesn't work correctly on certain devices

I have a problem with my android app which draws graphs of functions on a plane. To do this I use a custom View. The user can pan the coordinate system with touch. In the onDraw method, the coordinate system and the labels are drawn directly on the view's canvas, but the graphs themselves are buffered onto an additional bitmap(for performace reasons) - during the movement of the finger this bitmap is drawn onto the view's canvas translated so as to match the coordinate system, and the functions are redrawn only when the user releases the touch.
public void onDraw(Canvas canvas){
if (start) {
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_4444);
hCanvas = new Canvas(bitmap);
start = false;
}
drawCoordinateSystem(canvas);
drawFunctions(canvas,bitmap,hCanvas);
}
In onTouch event listener I call invalidate(). There is a boolean "moving", which is set to true for ACTION_UP and to false for ACTION_DOWN.
public void drawFunctions(Canvas canvas,Bitmap bitmap,Canvas hCanvas){
if (moving) canvas.drawBitmap(bitmap,left+translateX,top+translateY,paint);
else {
hCanvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR);
//code for drawing the functions on hCanvas
canvas.drawBitmap(bitmap,0,0,paint);
}
}
In ACTION_MOVE I calculate the translateX and translateY values appropriately.
Now where is the problem:
If I compile the app for sdk 10, it works fine on all devices I tested it on(android 2.2-4.2 smartphones and tablets, virtual and physical). However, when I compile it for sdk 14, it works well for many devices, except for some tablets, where the coordinate system is drawn correctly, but somehow the bitmap containing the functions is not drawn on the view's canvas and the functions are not visible. The strange thing is, if I try to make a bitmap out of the view's canvas:
Bitmap bmp = this.getDrawingCache();
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
In the saved bitmap the functions are visible.
Another thing is - the issue occurs for i.a. Nexus 7, meanwhile on an AVD created with the preset Nexus 7 everything works fine.
What can be the problem?
http://developer.android.com/reference/android/graphics/Bitmap.Config.html
I'll copy and paste the relevant part:
Bitmap.Config ARGB_4444 This field was deprecated in API level . Because of the poor
quality of this configuration, it is advised to use ARGB_8888 instead.
Bitmap.Config ARGB_8888 Each pixel is stored on 4 bytes.
Change your 4444 to 8888 and it should work.

Simple image rotation

I am making a game in Java. I made a planet seen from outer space and I want to make it appear like the planet is slowly rotating. But I don't know how to rotate a image. I need a simple command that rotates my image 1 degree around its own center once. Any help?
This is what I want to do:
Image
Take a look at these tutorials:
Java2D: Have Fun With Affine Transform
Coordinate Translations and Rotations: Example Code
Transforming Shapes, Text, and Images
What you are describing is not rotating an image, but changing an image to represent a 3D rotation of the object in the image.
Ideally you wouldn't be working with this as an image but rather as a 3D object with a different camera angle. Then you would simply rotate the camera around the object and display the resulting image to the user.
However if you're set on doing this as an image, then you need to create a different images representing various states of rotation of your planet and have a separate thread that would replace the displayed image with the next one in sequence, at repeated intervals. Search the web for "java image animation" - there are plenty of tutorials on how to do this.
If you want to rotate an image in 2d space, you can use something like this:
Image image = ...
Graphics2D g2d = ...; //
g2d.translate(170, 0); // If needed
g2d.rotate(1); // Rotate the image by 1 radian
//or g2d.rotate(180.0/3.14); to rotate by 1 degree
g2d.drawImage(image, 0, 0, 200, 200, observer);

Categories