在解碼/載入圖片到內(nèi)存(Bitmap對象)之前,要先判斷一下圖片的尺寸,獲取圖片的尺寸和類型:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
設(shè)置options.inJustDecodeBounds = true,將只讀取文件信息,不會為圖片分配內(nèi)存
載入和屏幕尺寸適配的圖片尺寸,否則空占內(nèi)存卻沒有視覺效果的提升,需要考慮以下因素:
當(dāng)前設(shè)備的屏幕尺寸和像素密度
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
...
mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
...
BitmapFactory.Options.inBitmap,會使得bitmap mutable,不太建議使用The Transitions Framework
http://wiki.jikexueyuan.com/project/notes/images/transitions_diagram.png" alt="transitions_diagram.png" />
Creating a Scene
從xml創(chuàng)建
scene root
<FrameLayout
android:id="@+id/scene_root">
<include layout="@layout/a_scene" />
</FrameLayout>
starting scene
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scene_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text_view1
android:text="Text Line 1" />
<TextView
android:id="@+id/text_view2
android:text="Text Line 2" />
</LinearLayout>
ending scene
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scene_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text_view2
android:text="Text Line 2" />
<TextView
android:id="@+id/text_view1
android:text="Text Line 1" />
</LinearLayout>
創(chuàng)建scene
Scene mAScene;
Scene mAnotherScene;
// Create the scene root for the scenes in this app
mSceneRoot = (ViewGroup) findViewById(R.id.scene_root);
// Create the scenes
mAScene = Scene.getSceneForLayout(mSceneRoot, R.layout.a_scene, this);
mAnotherScene =
Scene.getSceneForLayout(mSceneRoot, R.layout.another_scene, this);
代碼創(chuàng)建
Scene mScene;
// Obtain the scene root element
mSceneRoot = (ViewGroup) mSomeLayoutElement;
// Obtain the view hierarchy to add as a child of
// the scene root when this scene is entered
mViewHierarchy = (ViewGroup) someOtherLayoutElement;
// Create a scene
mScene = new Scene(mSceneRoot, mViewHierarchy);
Scene.setExitAction()和Scene.setEnterAction()應(yīng)用動效
從xml創(chuàng)建
res/transition/fade_transition.xml
<fade xmlns:android="http://schemas.android.com/apk/res/android" />
inflate
Transition mFadeTransition =
TransitionInflater.from(this).
inflateTransition(R.transition.fade_transition);
Transition mFadeTransition = new Fade();TransitionManager.go(mEndingScene, mFadeTransition);removeTarget(),addTarget()同時使用多種效果:TransitionSet
在res/transitions/目錄下創(chuàng)建資源文件
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
android:transitionOrdering="sequential">
<fade android:fadingMode="fade_out" />
<changeBounds />
<fade android:fadingMode="fade_in" />
</transitionSet>
自定義Transition
繼承Transition類
public class CustomTransition extends Transition {
@Override
public void captureStartValues(TransitionValues values) {}
@Override
public void captureEndValues(TransitionValues values) {}
@Override
public Animator createAnimator(ViewGroup sceneRoot,
TransitionValues startValues,
TransitionValues endValues) {}
}
Capture View Property Values
captureStartValuesscene中的每個view都會調(diào)用一遍,TransitionValues包含view對象,和一個map,用于記錄view對象的各個感興趣的屬性值
public class CustomTransition extends Transition {
// Define a key for storing a property value in
// TransitionValues.values with the syntax
// package_name:transition_class:property_name to avoid collisions
private static final String PROPNAME_BACKGROUND =
"com.example.android.customtransition:CustomTransition:background";
@Override
public void captureStartValues(TransitionValues transitionValues) {
// Call the convenience method captureValues
captureValues(transitionValues);
}
// For the view in transitionValues.view, get the values you
// want and put them in transitionValues.values
private void captureValues(TransitionValues transitionValues) {
// Get a reference to the view
View view = transitionValues.view;
// Store its background property in the values map
transitionValues.values.put(PROPNAME_BACKGROUND, view.getBackground());
}
...
}
captureEndValues和captureStartValues類似@Override
public void captureEndValues(TransitionValues transitionValues) {
captureValues(transitionValues);
}
createAnimator,transition framework調(diào)用此函數(shù)的次數(shù)與scene的變化有關(guān)
View.animate()方法返回ViewPropertyAnimator對象,對其可以進行各種property animationViewPager的使用
Fragment使用ListView類似,ViewPager也需要一個Adapter,FragmentStatePagerAdapter的實現(xiàn)類ViewPager.PageTransformer接口,通過其transformPage函數(shù)自定義page切換動效
position參數(shù)表示當(dāng)前page的位置setCustomAnimations,注意:這個函數(shù)的調(diào)用順序很重要,一定在add, replace等操作之前。ObjectAnimator來實現(xiàn)縮略圖放大到大圖的動畫效果,以及反向效果。setLayoutTransition()設(shè)置自定義layout變化動效Scene框架也可以做這個事情TransitionManager.beginDelayedTransition(ViewGroup)