背景をタップ、あるいは回転で変更


---(アプリケーションの説明)---

画面の上部にアナログ時計を置きます。
背景をタップ、あるいは画面の向きを変えると背景画像が変わります。
縦1 縦2 横1 横2

---(ソースリストjava/jp.co-site.yukos.testapp/MainActivity.java)---

package jp.co_site.yukos.ourclock;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.content.SharedPreferences;                      //カウンタ保存用
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
    SharedPreferences pref;
    int counter=1;
    View layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref=getSharedPreferences("iCount", MODE_PRIVATE);
        counter=pref.getInt("count", 1);                       //前回カウンタ取得
        layout=findViewById(R.id.RelativeLayout01);
        layout.setOnClickListener(new View.OnClickListener(){  //背景時クリック関数
            @Override
            public void onClick(View v) {                
                counter++;                                     //前回と異なるカウンタセット
                if(counter==3){counter=1;}
                String resourceName="img"+counter;             //カウンタに合った背景セット
                int viewId=getResources().getIdentifier(resourceName,"drawable",getPackageName());
                v.setBackgroundResource(viewId);
            }
        });
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    @Override
    protected  void onResume(){                                                           
        String resourceName="img"+counter;                     //カウンタに合った背景セット
        int viewId=getResources().getIdentifier(resourceName,"drawable",getPackageName());
        layout.setBackgroundResource(viewId);
        super.onResume();
    }
    @Override
    protected void onStop() {
        SharedPreferences.Editor editor=pref.edit();
        editor.putInt("count",counter);                        //今回カウンタ保存
        editor.commit();
        super.onStop();
    }
    @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);
    }
}

---(res/layout/content_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"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:id="@+id/RelativeLayout01"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity"
    android:background="@drawable/img1">
<AnalogClock 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/analogClock"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:dial="@drawable/dial"
    android:hand_hour="@drawable/hand_hour"
    android:hand_minute="@drawable/hand_minute">
</AnalogClock>
</RelativeLayout>

---(res/values/strings.xml)---

<resources>
    <string name="app_name">OurClock</string>
    <string name="action_settings">Settings</string>
</resources>

---(リソース)---

リソース