MultiResolution例子研究

1. 在res/layout/main.xml文件中

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"

 // 竖直方向
  android:orientation="vertical"  

 // LinearLayout的背景 background.9.png
  android:background="@drawable/background">  

  <LinearLayout
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- Notice that widget sizes are expressed in dip, or device-independent
         pixels, while text sizes are expressed in sp, or scale-independent
         pixels, to factor in user-chosen font sizes. -->
    <FrameLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@+id/image_container"

     // FrameLayout的背景 image_container.9.png
      android:background="@drawable/image_container">
      <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/image_view"

       // 按比例缩放 图片宽度等于ImageView的宽度 居中显示
        android:scaleType="fitCenter"/>
      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

       // 在FrameLayout的底部 水平居中
        android:layout_gravity="bottom|center_horizontal"
        android:id="@+id/status_text"
        android:textColor="@android:color/primary_text_dark"
        android:textSize="16sp"
        android:shadowDx="1.0"
        android:shadowDy="1.0"
        android:shadowRadius="1"
        android:layout_margin="5dip"
        android:shadowColor="@android:color/background_dark"/>
    </FrameLayout>
  </LinearLayout>

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:gravity="center">
    <Button
      android:text="@string/next_button"
      android:id="@+id/next_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:drawableLeft="@drawable/android"
      android:textSize="24sp"/>
  </LinearLayout>

</LinearLayout>

2. 在MultiRes.onSaveInstanceState(Bundle outState)方法中

outState.putInt("photo_index", mCurrentPhotoIndex);
super.onSaveInstanceState(outState);

// 点击HOME键时 会自动调用onSaveInstanceState这个方法

3. 在MultiRes.showPhoto(int photoIndex)方法中

// 设置ImageResource

ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageResource(mPhotoIds[photoIndex]);

// 设置当前位置

TextView statusText = (TextView) findViewById(R.id.status_text);
statusText.setText(String.format("%d/%d", photoIndex + 1,
        mPhotoIds.length));

4. 在MultiRes.onCreate方法中

// 点击“NEXT”按钮时 mCurrentPhotoIndex加1

mCurrentPhotoIndex = (mCurrentPhotoIndex + 1) % mPhotoIds.length;
showPhoto(mCurrentPhotoIndex);

5. 在MultiRes开始定义变量

private int mCurrentPhotoIndex = 0;
private int[] mPhotoIds = new int[] { R.drawable.sample_0,
        R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
        R.drawable.sample_7 };

原文地址:https://www.cnblogs.com/fengzhblog/p/2752926.html