Thursday, March 24, 2016

Android ImageView Rotate Example.

Hello this is the tutorial for the android imageview rotate example.

MainActivity.java

package androidanswers.imagerotateapplication;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    private Bitmap bmp;
    private Bitmap operation;
    ImageView i;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        i = (ImageView) this.findViewById(R.id.imageView);

        BitmapDrawable abmp = (BitmapDrawable) i.getDrawable();
        bmp = abmp.getBitmap();

        rotate180(bmp);


    }



    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.  
getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void rotate180(Bitmap bmp) { operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig()); int width = bmp.getWidth(); //the Width of the original image
int
height = bmp.getHeight();//the Height of the original image for (int i = 0; i < bmp.getWidth(); i++) { for (int j = 0; j < bmp.getHeight(); j++) { operation.setPixel(bmp.getWidth() - i - 1, bmp.getHeight() - j - 1, bmp.getPixel(i, j)); } } i.setImageBitmap(operation); }







Activity_main.xml 





<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:
tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="androidanswers.imagerotateapplication.MainActivity"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView" android:layout_marginTop="92dp" android:contentDescription="SampleImage" android:src="@drawable/spidey" android:scaleType="center" /> </LinearLayout>

Output Screen Shot.


After the Prg Run




No comments:

Post a Comment