m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

androidでimageviewの画像を回転

バージョンが新しいとなんかいろいろできるらしいけど

基本としては古めのバージョンでやった

imageviewで回転は困難なようです

一度ビットマップに変換してから回転したものを使用するらしい

実装

ソース
package jp.mshige1979.app.sampleimagerotate;

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


public class MainActivity extends ActionBarActivity {

    private ImageView img1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img1 = (ImageView)findViewById(R.id.img1);
        img1.setImageResource(R.drawable.janken_choki);

        //getDrawableメソッドで取り戻したものを、BitmapDrawable形式にキャストする
        BitmapDrawable bd = (BitmapDrawable)img1.getDrawable();
        //getBitmapメソッドでビットマップファイルを取り出す
        Bitmap bmp = bd.getBitmap();
        //回転させる
        Matrix matrix = new Matrix();
        matrix.postRotate(140.6f);
        //Bitmap回転させる
        Bitmap flippedBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);
        //加工したBitmapを元のImageViewにセットする
        img1.setImageDrawable(new BitmapDrawable(flippedBmp));

    }


    @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);
    }
}

結果

f:id:m_shige1979:20150117131945p:plain

所感

デザイン面を気にしなければしょぼいアプリなら作れるのでは?と思ったけど動かしてみて
なんかダサいって思いがちな感じがしてきている。
基本としては作ってからいろいろ精錬していく感じなので作ることが重要とは思うけど

そろそろブログを書くのを一時的に停止して何かのアプリ開発を考えた方がいいかも