How to set an angle to an imageview in xml

I am working in an android application and I want to set my image view in a particular angle in the xml itself. How can I do this ? Please help me. Thanks in advance.

Answers


I am working in an android application and I want to set my image view in a particular angle in the xml itself.

I am not sure about above approach.But programatically its possible.

You can use Matrix to rotate image.

Here is working code.

package org.sample;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;

public class SampleActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView imageView = (ImageView) findViewById(R.id.image);
        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.image);
        imageView.setImageBitmap(rotate(bm, 45.0f));

    }

    Bitmap rotate(Bitmap src, float degree)
    {
        // create new matrix
        Matrix matrix = new Matrix();
        // setup rotation degree
        matrix.postRotate(degree);
        // return new bitmap rotated using matrix
        return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(),
                matrix, true);
    }
}

Below is Output

If you are targeting 3.0 or above platform.Then life is little easier. You can put following attribute in ImageView

 <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotation="45" />

On Honeycomb end newer SDK you can do it in XML like this:

android:rotationX="10dp"

There is an easier way. It to use a RotateDrawable. Google documentation is quite poor, but the feature is very pratical to use. To acomplish what you want just do this:

1 - Create your Rotate drawable in a XML drawable file:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="120"
    android:interpolator="@android:anim/linear_interpolator"
    android:drawable="@drawable/myImageIwantToRotate"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360"/>

Notice the "fromDegrees". That is the angle you want your image to rotate when it`s loaded. If you dont want to animate or rotate programatically you dont need to worry about the "toDegrees" parameter.

2 - Add that to your activity layout just like you would add a regular image, but instead select the XML file you just created (step 1) as the src:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myDrawableXMLfileName"
    android:id="@+id/myRotatedImageID" />

IMPORTANT:The Rotate drawable XML file has to be created inside the res/drawable folder.


I think you need to use Camera translate function to do.

 Camera mCamera = new Camera();
 mCamera.translate(0.0f, 0.0f, -295.0f);

I am not sure but i think it help you.

Thanks


Need Your Help

How to install weinre on mac?

macos remote-debugging weinre

I wanted to debug my mobile web app that would be running on a mobile device or simulator (like iOS simulator). On googling a lot I came across Weinre but could not figure out how to install on my

Connecting 3 different java files

java string object arraylist

First it starts off with the class College tester, it askes the user for a command.