IT/Android

android - menu, imageView 응용

노마드오브 2018. 9. 20. 23:50

파일명 : activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/jeju1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="각도입력" />
<EditText
android:id="@+id/edtAngle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textView1"
android:text="0" />

</RelativeLayout>


파일명 : menu1.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemRotate"
android:title="그림회전" />
<group
android:checkableBehavior="single">
<item
android:id="@+id/item1"
android:title="한라산">
</item>
<item
android:id="@+id/item2"
android:title="추자도">
</item>
<item
android:id="@+id/item3"
android:title="범섬">
</item>

</group>

</menu>


파일명 : MainActivity.java

package com.example.it.myapplication7_4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

EditText edtText;
ImageView imageView1;

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

edtText = (EditText) findViewById(R.id.edtAngle);
imageView1 = (ImageView) findViewById(R.id.imageView1);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater in = getMenuInflater();
in.inflate(R.menu.menu1, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case R.id.itemRotate:
imageView1.setRotation(Float.parseFloat(edtText.getText().toString()));
return true;
case R.id.item1:
imageView1.setImageResource(R.drawable.jeju2);
return true;
case R.id.item2:
imageView1.setImageResource(R.drawable.jeju3);
return true;
case R.id.item3:
imageView1.setImageResource(R.drawable.jeju4);
return true;
}

return super.onOptionsItemSelected(item);

}
}