Android5.0开发范例大全 读书笔记(一)

Android5.0开发范例大全(第4版)android

Dave Smith与Jeff Friesen著ide

书本的内容是5.0的一些新特性,挺厚的。不过由于是歪果仁写的,翻译过来再看总有一种说不清道不明的隔膜。函数

我本身是有强迫症的,图书馆借的书总想看到最后一页。因而断断续续花了一个月终于啃完,满分10分我给6分。oop

如下草草记录下我的认为的新鲜内容。布局

(一)布局和视图动画

1.1样式化常见组件this

1.相同的样式能够在styles.xml中定义,样式之间存在继承关系。spa

2.主题是一种全局化的样式线程

1.3自定义视图翻译

1.构造函数有三种

View(Context context) //经过Java代码构造视图
View(Context context, AttributeSet attrs)//经过XML填充视图,AttributeSet 包括附加到视图的XML元素全部属性
View(Context context, AttributeSet attrs, int defStyleAttr)//与前一个方法相似,在将样式属性添加到XML元素时调用

2.测量时的getMeasurement()方法,基本是固定写

 private int getMeasurement(int measureSpec, int contentSize) {
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (MeasureSpec.getMode(measureSpec)) {
            case MeasureSpec.AT_MOST:
                return Math.min(specSize, contentSize);
            case MeasureSpec.UNSPECIFIED:
                return contentSize;
            case MeasureSpec.EXACTLY:
                return specSize;
            default:
                return 0;
        }
    }

 1.4动画视图

1.ViewPropertyAnimator方法是对视图内容作动画最便利的方法,但仅限于简单动画

 textView.animate().alpha(0f).translationX(200f).setDuration(500);

其中全部方法都会在一块儿执行在(主线程的Looper中)

 2.ObjectAnimator能够实现复杂动画

 mFlipper = ObjectAnimator.ofFloat(mFlipImage, "rotationY", 0f, 360f).setDuration(2000);
        mFlipper.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (animation.getAnimatedFraction() >= 0.25f && mIsHeads) {
                    mFlipImage.setImageBitmap(mTailsImage);
                    mIsHeads = false;
                }
                if (animation.getAnimatedFraction() >= 0.75 && !mIsHeads) {
                    mFlipImage.setImageBitmap(mHeadsImages);
                    mIsHeads = true;
                }
            }
        });

    }
  

  还能够添加监听器

@Override
    public boolean onTouchEvent(MotionEvent event) {
        mFlipper.start();
        return true;
    }

 1.5布局变化时的动画

1.在XML中添加 android:animateLayoutChanges="true",便可实现任何ViewGroup改变布局时的动画效果

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:onClick="add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="add"/>
    <LinearLayout
        android:id="@+id/ll_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:orientation="vertical"
        />
</LinearLayout>

 1.6实现针对具体场景的布局

在res下建立不一样的资源文件夹便可1.针对不一样方向

resource-land

resource-port

2.针对不一样尺寸

resource-small:最小尺寸426dp*320dp

resource-medium:最小尺寸470dp*320dp

resource-large:最小尺寸640dp*480dp

resource-xlarge:最小尺寸960dp*720dp

3.布局别名

<resources>
<item name="rename" type="layout">@layout/name</item>
</resources>

通常状况下都是3者联合起来使用

1.7自定义AdapterView的空视图

调用AdapterView.setEmptyView()

1.11自定义过分动画

1.在Activity切换时实现动画效果,通常要在res/anim下建立4个互补的动画

如activity_close_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:fromDegrees="-90"
    android:toDegrees="0"
    android:pivotX="0%"
    android:pivotY="0%"
    android:fillEnabled="true"
    android:fillBefore="true"
    android:fillAfter="true"
    android:duration="500"/>
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:fillEnabled="true"
        android:fillBefore="true"
        android:fillAfter="true"
        android:duration="500"/>
</set>

activity_close_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:fromDegrees="0"
    android:toDegrees="90"
    android:pivotX="0%"
    android:pivotY="0%"
    android:fillEnabled="true"
    android:fillBefore="true"
    android:fillAfter="true"
    android:duration="500"/>
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:fillEnabled="true"
        android:fillBefore="true"
        android:fillAfter="true"
        android:duration="500"/>
</set>

 activity_open_enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:fromDegrees="90"
    android:toDegrees="0"
    android:pivotX="0%"
    android:pivotY="0%"
    android:fillEnabled="true"
    android:fillBefore="true"
    android:fillAfter="true"
    android:duration="500"/>
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:fillEnabled="true"
        android:fillBefore="true"
        android:fillAfter="true"
        android:duration="500"/>
</set>

 activity_open_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
    android:fromDegrees="0"
    android:toDegrees="-90"
    android:pivotX="0%"
    android:pivotY="0%"
    android:fillEnabled="true"
    android:fillBefore="true"
    android:fillAfter="true"
    android:duration="500"/>
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:fillEnabled="true"
        android:fillBefore="true"
        android:fillAfter="true"
        android:duration="500"/>
</set>

接着在Activity中调用以下代码便可

public void startActivity(View view) {
        Intent intent=new Intent(this,SecondActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.activity_open_enter,R.anim.activity_open_exit);
        finish();
        overridePendingTransition(R.anim.activity_close_enter,R.anim.activity_close_exit);
    }
相关文章
相关标签/搜索