0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[ Android Studio ] 画像のサイズ調整と回転

Posted at

今回は画像の扱い方について記載していく。

画像サイズ調整

 サイズ調整には以下のようなプログラムを用いる。

    imageView.setScaleType(ImageView.ScaleType.FIT_XY);

 FIT_XYとは画像の大きさをレイアウトのImageViewサイズに合わせる定数である。
 引数には様々な定数が存在する。
 以下のサイトがとても参考になるので、見て頂きたい。
https://akira-watson.com/android/imageview_scaletype.html

画像回転

 まずいつも通り回転用ボタンを実装。
 (ボタンのID:rotate)
 (ImageViewのID:imageBluePaint)

 そして以下のようなクリックイベントを実装。
iは回転の度合いを表している。

    int i = 0;
    rotateButton.setOnClickListener((View v) -> {
        i += 5;
        ((ImageView)findViewById(imageBluePaint)).setRotation(i);
    })

まとめ

 以下がサンプルプログラムである。

public class MainActivity extends Activity {

    int i = 0;

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

    /*
    表示非表示ボタンの実装
    */
    public void imageDisplayInitialize() {
        //クリックイベントを取得するボタン
        Button rotateButton = (Button) findViewById(R.id.rotate);

        //画像表示
        ImageView imageView = findViewById(R.id.imageBluePaint);
        imageView.setImageResource(R.drawable.bluepaint);

        //画像サイズ変更用プログラム
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        //ボタンにOnClickListenerインターフェースを実装
        //回転用ボタンの機能実装
        rotateButton.setOnClickListener((View v5) -> {
            i+=50;
            ((ImageView)findViewById(R.id.imageBluePaint)).setRotation(i);
        });
    }
}

 以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?