API Demo MulitiRes 学习

multi_res.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/background"> <LinearLayout android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@+id/image_container" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/image_container"> <ImageView android:id="@+id/image_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitCenter"/> <TextView android:layout_gravity="bottom|center_horizontal" android:id="@+id/status_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:shadowDx="1.0" android:shadowDy="1.0" android:shadowRadius="1" android:layout_margin="5dip" android:shadowColor="@android:color/background_dark" android:textColor="@android:color/primary_text_dark" /> </FrameLayout> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_marginTop="10dip"> <Button android:text="@string/next_image" android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:drawableLeft="@drawable/android" /> </LinearLayout> </LinearLayout>
public class MultiRes extends Activity{ 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}; private Button mNextButton; private ImageView mImageView; private TextView mStatusText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.multi_res); mNextButton =(Button) findViewById(R.id.next_button); mImageView = (ImageView) findViewById(R.id.image_view); mStatusText = (TextView) findViewById(R.id.status_text); showPhoto(mCurrentPhotoIndex); mNextButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mCurrentPhotoIndex = (mCurrentPhotoIndex + 1) % mPhotoIds.length; showPhoto(mCurrentPhotoIndex); } }); } @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("photo_index", mCurrentPhotoIndex); super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { mCurrentPhotoIndex = savedInstanceState.getInt("photo_index"); showPhoto(mCurrentPhotoIndex); super.onRestoreInstanceState(savedInstanceState); } protected void showPhoto(int photoIndex) { mImageView.setImageResource(mPhotoIds[photoIndex]); mStatusText.setText(String.format("%d/%d", mCurrentPhotoIndex + 1, mPhotoIds.length)); } } API Demo MulitiRes 学习

问题一:

@Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("photo_index", mCurrentPhotoIndex); super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { mCurrentPhotoIndex = savedInstanceState.getInt("photo_index"); showPhoto(mCurrentPhotoIndex); super.onRestoreInstanceState(savedInstanceState); }

void com.gao.apidemo.multires.MultiRes.onSaveInstanceState(Bundle outState)

Called to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate or onRestoreInstanceState (the Bundle populated by this method will be passed to both).

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate or onRestoreInstanceState.

Do not confuse this method with activity lifecycle callbacks such as onPause, which is always called when an activity is being placed in the background or on its way to destruction, or onStop which is called before destruction. One example of when onPause and onStop is called and not this method is when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState on B because that particular instance will never be restored, so the system avoids calling it. An example when onPause is called and not onSaveInstanceState is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact.

The default implementation takes care of most of the UI per-instance state for you by calling android.view.View.onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState). If you override this method to save additional information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be prepared to save all of the state of each view yourself.

If called, this method will occur before onStop. There are no guarantees about whether it will occur before or after onPause.

Parameters:
outState Bundle in which to place your saved state.
void com.gao.apidemo.multires.MultiRes.onRestoreInstanceState(Bundle savedInstanceState)

This method is called after onStart when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState.

This method is called between onStart and onPostCreate.

Parameters:
savedInstanceState the data most recently supplied in onSaveInstanceState.
问题二:

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

这种写法和

mCurrentPhotoIndex++; if (mCurrentPhotoIndex >= mPhotoIds.length){ mCurrentPhotoIndex = 0; }
效果是一样的,注意下标是从0开始的所以是》=

问题三:

ImageView的属性android:scaleType,即 ImageView.setScaleType(ImageView.ScaleType)。android:scaleType是控制图片如何 resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截 取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长 (宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小 或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把 图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把 图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制

问题四:

LinearLayout有两个非常相似的属性:
android:gravity与android:layout_gravity。
他们的区别在于:
android:gravity 属性是对该view中内容的限定.比如一个button 上面的text. 你可以设置该text 相对于view的靠左,靠右等位置.
android:layout_gravity是用来设置该view相对与父view 的位置比如一个button 在linearlayout里,你想把该button放在linearlayout里靠左、靠右等位置就可以通过该属性设置.
即android:gravity用于设置View中内容相对于View组件的对齐方式,而android:layout_gravity用于设置View组件相对于Container的对齐方式。
原理跟android:paddingLeft、android:layout_marginLeft有点类似。如果在按钮上同时设置这两个属性。
android:paddingLeft="30px" 按钮上设置的内容离按钮左边边界30个像素
android:layout_marginLeft="30px" 整个按钮离左边设置的内容30个像素
下面回到正题, 我们可以通过设置android:gravity="center"来让EditText中的文字在EditText组件中居中显示;同时我们设置EditText的android:layout_gravity="right"来让 EditText组件在LinearLayout中居右显示
问题五:

<TextView android:id="@+id/tvText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="text1" android:textSize="28sp" android:textStyle="bold" android:textColor="#FFFFFF" android:shadowColor="#ff000000" android:shadowDx="2" android:shadowDy="2" android:shadowRadius="1"/>android:shadowColor 阴影颜色
android:shadowDx 阴影的水平偏移量
android:shadowDy 阴影的垂直偏移量

大多数情况下,某一类的TextView控件需要统一的风格,如加阴影等,那么可以使用style。
android:shadowRadius 阴影的范围
<TextView style="@style/StyleBarTitle"
android:id="@+id/txBarTitle1"
android:text="资讯行情" />

然后在values文件夹下创建styles.xml文件,内容为:

<?xml version="1.0" encoding="utf-8"?> <resources> <style name="StyleBarTitle"> <item name="android:layout_gravity">center_vertical</item> <item name="android:layout_width">0dp</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_weight">1</item> <item name="android:gravity">center_horizontal</item> <item name="android:textSize">@dimen/text_size_vlarge</item> <item name="android:textStyle">bold</item> <item name="android:textColor">#FFFFFF</item> <item name="android:shadowColor">#ff000000</item> <item name="android:shadowDx">2</item> <item name="android:shadowDy">2</item> <item name="android:shadowRadius">1</item> <item name="android:background">@null</item> </style> </resources>这样的最大优点是减低代码冗余度,在需要更改某一类控件的样式时,不用一个一个的改过来,只需要更改styles文件中即可。