Android#16 -- MyDay App (2)

2017-10-19  本文已影响0人  喜欢书的女孩

[6] View

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_drawer_layout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.cnhct.dynamicview.MainPageActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:orientation="vertical"
            android:background="@color/colorPrimaryDark"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/Theme.AppCompat.Light"
                app:elevation="0dp">

                <android.support.v7.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/main_toolbar">

                </android.support.v7.widget.Toolbar>

                <android.support.design.widget.TabLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabBackground="@color/colorPrimaryDark"
                    app:tabIndicatorColor="@color/white"
                    android:id="@+id/main_tab_layout">

                </android.support.design.widget.TabLayout>
            </android.support.design.widget.AppBarLayout>


                <android.support.v4.view.ViewPager
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/main_view_pager"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">

                </android.support.v4.view.ViewPager>

                <android.support.design.widget.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|bottom"
                    android:layout_margin="@dimen/fab_margin"
                    android:id="@+id/main_floating_action_btn"
                    android:background="@android:drawable/ic_input_add"
                    android:backgroundTint="@color/colorPrimaryDark"
                    android:src="@drawable/ic_arrow_upward"
                    tools:targetApi="lollipop"
                    tools:ignore="RtlHardcoded" />

        </android.support.design.widget.CoordinatorLayout>
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:id="@+id/main_nav_view"
        android:background="@color/white"
        app:headerLayout="@layout/app_home_title"
        app:menu="@menu/menu_nav"
        >
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

package com.cnhct.dynamicview.timeline;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.cnhct.dynamicview.R;


/**
 * 自定义View
 */

public class TimeLineMakerView extends View{

    private int mMarkerSize = 24; //圆点的大小
    private int mLineSize = 2;   //线段粗细
    private Drawable mBeginLine; //上面线段颜色
    private Drawable mEndLine;   //下面线段颜色
    private Drawable mMarkerDrawable; //圆点颜色
    private boolean orientation;   //竖屏false或横屏true

    public TimeLineMakerView(Context context){
        this(context,null);
    }

    public TimeLineMakerView(Context context, AttributeSet attrs){
        this(context,attrs,0);
    }

    public TimeLineMakerView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context,attrs,defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs){
        final TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.TimeLineMaker);

        mMarkerSize = array.getDimensionPixelSize(R.styleable.TimeLineMaker_makerSize,mMarkerSize);

        mLineSize = array.getDimensionPixelSize(R.styleable.TimeLineMaker_lineSize,mLineSize);

        mBeginLine = array.getDrawable(R.styleable.TimeLineMaker_beginLine);

        mEndLine = array.getDrawable(R.styleable.TimeLineMaker_endLine);

        mMarkerDrawable = array.getDrawable(R.styleable.TimeLineMaker_maker);

        orientation = array.getBoolean(R.styleable.TimeLineMaker_orientation,false);

        array.recycle();

        if (mBeginLine != null)
            mBeginLine.setCallback(this);
        if (mEndLine != null)
            mEndLine.setCallback(this);
        if (mMarkerDrawable != null)
            mMarkerDrawable.setCallback(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        if (orientation){
            if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
                setMeasuredDimension(120, 80);  //针对wrap情况处理
            }
            else if (widthSpecMode == MeasureSpec.AT_MOST){
                setMeasuredDimension(120, heightSpecSize);
            }
            else if (heightSpecMode == MeasureSpec.AT_MOST){
                setMeasuredDimension(widthSpecSize, 80);
            }
        }
        else {
            if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){
                setMeasuredDimension(80, 120);  //针对wrap 情况处理
            }
            else if (widthSpecMode == MeasureSpec.AT_MOST){
                setMeasuredDimension(80, heightSpecSize);
            }
            else if (heightSpecMode == MeasureSpec.AT_MOST){
                setMeasuredDimension(widthSpecSize, 120);
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        if (mBeginLine != null){
            mBeginLine.draw(canvas);
        }
        if (mEndLine != null){
            mEndLine.draw(canvas);
        }
        if (mMarkerDrawable != null){
            mMarkerDrawable.draw(canvas);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh){
        super.onSizeChanged(w, h, oldw, oldh);
        initDrawableSize();

    }

    private void initDrawableSize(){
        int pLeft = getPaddingLeft();
        int pRight = getPaddingRight();
        int pTop = getPaddingTop();
        int pBottom = getPaddingBottom();
        int width = getWidth();
        int height = getHeight();
        int cWidth = width-pLeft-pRight;
        int cHeight = height-pTop-pBottom;
        Rect bounds;
        int mMarkerSizePx = ResourceUtil.dip2px(getContext(), mMarkerSize);
        int mLineSizePx = ResourceUtil.dip2px(getContext(), mLineSize);
        //横向
        if (orientation) {
            if (mMarkerDrawable != null) {
                int markSize = Math.min(Math.min(cWidth, cHeight), mMarkerSizePx);
                mMarkerDrawable.setBounds(pLeft + width / 2 - markSize / 2, pTop, pLeft + width / 2 - markSize / 2 + markSize, pTop + markSize);
                bounds = mMarkerDrawable.getBounds();
            } else {
                bounds = new Rect(pLeft + width / 2, pTop, pLeft + width / 2, pTop);
            }
            int halfLine = mLineSizePx >> 1;
            int lineTop = bounds.centerY() - halfLine;
            if (mBeginLine != null) {
                mBeginLine.setBounds(0, lineTop, bounds.left, lineTop + mLineSizePx);
            }
            if (mEndLine != null) {
                mEndLine.setBounds(bounds.right, lineTop, width, lineTop + mLineSizePx);
            }
        }
        //竖屏
        else{
            if (mMarkerDrawable != null){
                int markSize = Math.min(Math.min(cWidth,cHeight), mMarkerSizePx);
                mMarkerDrawable.setBounds(pLeft, pTop + height/2 -markSize/2, pLeft + markSize, pTop + height/2 - markSize/2 + markSize);
                bounds = mMarkerDrawable.getBounds();
            }
            else{
                bounds = new Rect(pLeft + mLineSizePx/2, pTop + height/2, pLeft + mLineSizePx/2, pTop + height/2);
            }
            int halfLine = mLineSizePx >> 1;
            int lineLeft = bounds.centerX() - halfLine;
            if (mBeginLine != null){
                mBeginLine.setBounds(lineLeft, 0, lineLeft + mLineSizePx, bounds.top);
            }
            if (mEndLine != null){
                mEndLine.setBounds(lineLeft, bounds.bottom, lineLeft + mLineSizePx, height);
            }

        }
    }
    public void setLineSize(int lineSize){
        if (this.mLineSize != lineSize){
            mLineSize = lineSize;
            initDrawableSize();
            invalidate();
        }
    }

    public void setMakerSize(int markerSize){
        if (this.mMarkerSize != markerSize){
            markerSize = markerSize;
            initDrawableSize();
            invalidate();
        }
    }

    public void setBeginLine(Drawable beginLine){
        if (this.mBeginLine != beginLine){
            this.mBeginLine = beginLine;
            if (mBeginLine != null){
                mBeginLine.setCallback(this);
            }
            initDrawableSize();
            invalidate();
        }
    }

    public void setEndLine(Drawable endLine) {
        if (this.mEndLine != endLine) {
            this.mEndLine = endLine;
            if (mEndLine != null) {
                mEndLine.setCallback(this);
            }
            initDrawableSize();
            invalidate();
        }
    }

    public void setMarkerDrawable(Drawable markerDrawable) {
        if (this.mMarkerDrawable != markerDrawable) {
            this.mMarkerDrawable = markerDrawable;
            if (mMarkerDrawable != null) {
                mMarkerDrawable.setCallback(this);
            }
            initDrawableSize();
            invalidate();
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical"
   android:background="@color/colorPrimaryDark"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <android.support.design.widget.AppBarLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       style="@style/Theme.AppCompat.Light"
       app:elevation="0dp">

       <android.support.v7.widget.Toolbar
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:id="@+id/main_toolbar">

       </android.support.v7.widget.Toolbar>

       <android.support.design.widget.TabLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           app:tabBackground="@color/colorPrimaryDark"
           app:tabIndicatorColor="@color/white"
           android:id="@+id/main_tab_layout">

       </android.support.design.widget.TabLayout>
   </android.support.design.widget.AppBarLayout>

   <android.support.design.widget.AppBarLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       style="@style/Theme.AppCompat.Light"
       app:elevation="0dp">

       <android.support.v4.view.ViewPager
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:id="@+id/main_view_pager"
           app:layout_behavior="@string/appbar_scrolling_view_behavior">

       </android.support.v4.view.ViewPager>

       <android.support.design.widget.FloatingActionButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="bottom|end"
           android:layout_margin="@dimen/fab_margin"
           android:id="@+id/main_floating_action_btn"
           android:background="@android:drawable/ic_input_add"
           android:backgroundTint="@color/colorPrimaryDark"
           android:src="@drawable/ic_arrow_upward"
           tools:targetApi="lollipop" />
   </android.support.design.widget.AppBarLayout>



</android.support.design.widget.CoordinatorLayout>

[7] 清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cnhct.dynamicview">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/icon_gcoding"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".view.DynamicView" />
        <activity android:name=".view.BaseView" />
        <activity android:name=".MainPageActivity">

        </activity>
    </application>

</manifest>

[8] 依赖包

apply plugin: 'com.android.application'
apply plugin: 'android-apt'

android {
   compileSdkVersion 25
   buildToolsVersion "25.0.3"
   defaultConfig {
       applicationId "com.cnhct.dynamicview"
       minSdkVersion 19
       targetSdkVersion 25
       versionCode 1
       versionName "1.0"
       testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
   }
   buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
   }
}

dependencies {
   compile fileTree(include: ['*.jar'], dir: 'libs')
   androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
       exclude group: 'com.android.support', module: 'support-annotations'
   })
   //视图绑定 butterknife
   //引入okhttp
   //引入retrofit
   //引入rxjava
   //引入Log拦截器,方便DEBUG模式输出log信息
   //引入rxjava适配器,方便rxjava与retrofit的结合
   //引入json转换器,方便将返回的数据转换为json格式
   //引入rxandroid
   compile 'com.android.support:appcompat-v7:25.3.1'
   compile 'com.android.support.constraint:constraint-layout:1.0.2'
   compile 'com.android.support:design:25.3.1'
   compile 'com.jakewharton:butterknife:8.4.0'
   compile 'de.greenrobot:eventbus:3.0.0-beta1'
   compile 'com.squareup.okhttp3:okhttp:3.5.0'
   compile 'com.squareup.retrofit2:retrofit:2.1.0'
   compile 'io.reactivex.rxjava2:rxjava:2.0.4'
   compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'
   compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
   compile 'com.squareup.retrofit2:converter-gson:2.1.0'
   compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
   compile 'com.github.bumptech.glide:glide:3.7.0'
   compile 'com.android.support:support-v4:25.3.1'
   testCompile 'junit:junit:4.12'
   apt 'com.jakewharton:butterknife-compiler:8.4.0'
   compile files('libs/commons-lang3-3.6.jar')
   compile 'com.mcxiaoke.volley:library:1.0.19'
}

[9] 团队成员

文档.png

[10] 感谢

  1. 感谢老师的辛苦教学,您辛苦了!
  2. 感谢团队伙伴们的团结一致,共同完成任务。
  3. 感谢项目中参考的技术博客简书文章的作者。

[11] 遇到的难题

服务器请求数据返回数据量很大,解析对应出错
部分功能未实现

上一篇下一篇

猜你喜欢

热点阅读