• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.multires;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.view.View;
22 import android.widget.Button;
23 import android.widget.ImageView;
24 import android.widget.TextView;
25 
26 public final class MultiRes extends Activity {
27 
28     private int mCurrentPhotoIndex = 0;
29     private int[] mPhotoIds = new int[] { R.drawable.sample_0,
30             R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
31             R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
32             R.drawable.sample_7 };
33 
34     /** Called when the activity is first created. */
35     @Override
onCreate(Bundle savedInstanceState)36     public void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         setContentView(R.layout.main);
39 
40         showPhoto(mCurrentPhotoIndex);
41 
42         // Handle clicks on the 'Next' button.
43         Button nextButton = (Button) findViewById(R.id.next_button);
44         nextButton.setOnClickListener(new View.OnClickListener() {
45             public void onClick(View v) {
46                 mCurrentPhotoIndex = (mCurrentPhotoIndex + 1)
47                         % mPhotoIds.length;
48                 showPhoto(mCurrentPhotoIndex);
49             }
50         });
51     }
52 
53     @Override
onSaveInstanceState(Bundle outState)54     protected void onSaveInstanceState(Bundle outState) {
55         outState.putInt("photo_index", mCurrentPhotoIndex);
56         super.onSaveInstanceState(outState);
57     }
58 
59     @Override
onRestoreInstanceState(Bundle savedInstanceState)60     protected void onRestoreInstanceState(Bundle savedInstanceState) {
61         mCurrentPhotoIndex = savedInstanceState.getInt("photo_index");
62         showPhoto(mCurrentPhotoIndex);
63         super.onRestoreInstanceState(savedInstanceState);
64     }
65 
showPhoto(int photoIndex)66     private void showPhoto(int photoIndex) {
67         ImageView imageView = (ImageView) findViewById(R.id.image_view);
68         imageView.setImageResource(mPhotoIds[photoIndex]);
69 
70         TextView statusText = (TextView) findViewById(R.id.status_text);
71         statusText.setText(String.format("%d/%d", photoIndex + 1,
72                 mPhotoIds.length));
73     }
74 }
75