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

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

Dave Smith与Jeff Friesen著

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

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

以下草草记录下个人认为的新鲜内容。

(一)布局和视图

1.1样式化常见组件

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

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);
    }
原文地址:https://www.cnblogs.com/xx-wqj/p/5983466.html