DrawerLayout

一,Canvas二次贝塞尔曲线操作实例

的网站贝塞尔曲线操作

finshinit

1,DrawLayout

package com.drawlayout.ui;

import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * 作者: songli
 * QQ : 2734030745
 * on 2017/7/2 0002上午 11:56.
 * 邮箱: 15850774503@163.com
 */

public class MyDrawLayout extends DrawerLayout implements DrawerLayout.DrawerListener{
    private static final String TAG = "MyDrawLayout";


    /**
     * slideBar
     */
    private MyDrawSlideBar myDrawSlideBar;


    /**
     * 背景view
     */
    private MyDrawBgRealativeLayout myDrawBgRealativeLayout;


    /**
     * 内容控件
     */
    private View contentView;


    /**
     * 滑动的百分比
     */
    private float slideOffest;


    private float y;

    public MyDrawLayout(Context context) {
        super(context);
    }

    public MyDrawLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    private void init() {
        for (int i = 0; i < getChildCount(); i++)
        {
            //内容控件
            View view = getChildAt(i);

            if (view instanceof MyDrawSlideBar)
            {
                myDrawSlideBar = (MyDrawSlideBar) view;
            }else{
                contentView = view;
            }

        }
        //移除 myDreawSlideBar  再添加myDreawSlideBar
        removeView(myDrawSlideBar);

        //实例化背景
        myDrawBgRealativeLayout = new MyDrawBgRealativeLayout(myDrawSlideBar);

        //换
        addView(myDrawBgRealativeLayout);

        addDrawerListener(this);

        /*//先移除 myDrawSlideBar      再来添加  RelativeLayout
        removeView(myDrawSlideBar);

        //实例化  背景RealativeLayout
        myDrawBgRealativeLayout=new MyDrawBgRealativeLayout(myDrawSlideBar);
        //偷梁换柱
        addView(myDrawBgRealativeLayout);
        addDrawerListener(this);*/

    }


    /**
     * 子控件
     * @param event
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        y = event.getY();
        if (event.getAction() == MotionEvent.ACTION_UP)
        {
            myDrawSlideBar.onMotionUp();
            return super.dispatchTouchEvent(event);
        }

        //没有打开之前不拦截      打开拦截   大于1
        if (slideOffest < 1)
        {
            return super.dispatchTouchEvent(event);

        }
        else
        {
            myDrawBgRealativeLayout.setTouchY(y, slideOffest);

        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 划动百分比
     * @param drawerView
     * @param slideOffset
     */
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        this.slideOffest = slideOffset;
        myDrawBgRealativeLayout.setTouchY(y, slideOffset);

        //内容区的平移
        float contentViewoffset = drawerView.getWidth() * slideOffset / 2;
        contentView.setTranslationX(contentViewoffset);
    }




    @Override
    public void onDrawerOpened(View drawerView) {

    }

    @Override
    public void onDrawerClosed(View drawerView) {

    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }
}
package com.drawlayout.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.LinearLayout;

import com.drawlayout.activity.R;

/**
 * 作者: songli
 * QQ : 2734030745
 * on 2017/7/2 0002上午 11:56.
 * 邮箱: 15850774503@163.com
 */

public class MyDrawSlideBar extends LinearLayout {
    private static final String TAG = "MyDrawSlideBar";

    private boolean opened = false;


    private float maxTranslationX;

    public MyDrawSlideBar(Context context) {
        this(context,null);

    }

    public MyDrawSlideBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        setOrientation(VERTICAL);

        Log.d(TAG, "VERTICEL---------->   ");
        if (attrs != null)
        {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.SideBar);
            maxTranslationX = a.getDimension(R.styleable.SideBar_maxTranslationX, 0);
            a.recycle();
            Log.d(TAG, "VERTICEL---------->  attrs view ");
        }
    }

    public void setTouchY(float y, float slideOffset) {
        //每一个子控件进行平移
        //如果slideOfset = 1 显示
        opened = slideOffset == 1;
        for (int i = 0; i < getChildCount(); i++)
        {
            View child = getChildAt(i);
            child.setPressed(false);
            //要判断y坐标 松手的那个手
            /**
             *
             * |-----------------------
             * ^
             * |   两个线之间的距离
             * |_______________________
             */
            boolean isHover = opened && y > child.getTop() && y < child.getBottom();


            if (isHover)
            {
                child.setPressed(true);
                //BackCall 不应该这里调用


            }
            //平移
            apply(getParent(), child, y, slideOffset);
        }
    }

    private void apply(ViewParent parent, View child, float y, float slideOffset) {
        //平移距离
        float transslationX = 0;
        int centerY = child.getTop() * child.getHeight() / 2;
        //控件中心距离手指的距离
        float distance = Math.abs(y - centerY);


        float scale = distance / getHeight() * 3;//放大系数

        transslationX = maxTranslationX - scale  * maxTranslationX;

        Log.d(TAG, "parent= " + parent + "child = " + child + ", Y = "+ y + ", slideOffset = "+ slideOffset);
        child.setTranslationX(transslationX);

    }

    /**
     * 手指弹起来
     */
    public void onMotionUp() {

        for (int i = 0; opened && i < getChildCount(); i++)
        {
            View view = getChildAt(i);
            if (view.isPressed())
            {
                view.performClick();
                //BackCall 回调接口
            }
        }
    }
}
package com.drawlayout.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * 作者: songli
 * QQ : 2734030745
 * on 2017/7/2 0002上午 11:58.
 * 邮箱: 15850774503@163.com
 *
 * 中间
 */

public class MyDrawBgRealativeLayout extends RelativeLayout {
    private static final String TAG = "MyDrawBgRealativeLayout";

    /**
     * SlideBar
     */
    private MyDrawSlideBar myDrawSlideBar;


    /**
     * 背景图片
     */
    private MyDrawBgView myDrawBgView;

    public MyDrawBgRealativeLayout(MyDrawSlideBar myDrawSlideBar) {
        super(myDrawSlideBar.getContext());

        init(myDrawSlideBar);
    }


    private void init(MyDrawSlideBar myDrawSlideBar) {
        this.myDrawSlideBar = myDrawSlideBar;

        //height and width    把slidBar的
        setLayoutParams(myDrawSlideBar.getLayoutParams());

        //背景先添加进入
        myDrawBgView = new MyDrawBgView(getContext());
        addView(myDrawBgView, 0/*背景的顺序*/,
                new LayoutParams(/*ViewGroup.*/LayoutParams.MATCH_PARENT,
                /*ViewGroup.*/LayoutParams.MATCH_PARENT));

        //背景颜色取出来 设置myDraw  设置
        //myDrawBgView.setColor(((ColorDrawable)myDrawSlideBar.getBackground()).getColor());
        myDrawBgView.setColor(myDrawSlideBar.getBackground());
        myDrawSlideBar.setBackgroundColor(Color.TRANSPARENT);
        addView(myDrawSlideBar, new /*ViewGroup.*/LayoutParams(/*ViewGroup.*/LayoutParams.MATCH_PARENT,
                /*ViewGroup.*/LayoutParams.MATCH_PARENT));

    }


    /**
     * 专递view
     * @param y
     * @param slideOffset
     */
    public void setTouchY(float y, float slideOffset) {

        myDrawBgView.setTouchY(y, slideOffset);
        myDrawSlideBar.setTouchY(y, slideOffset);
    }
}
package com.drawlayout.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 作者: songli
 * QQ : 2734030745
 * on 2017/7/2 0002上午 11:55.
 * 邮箱: 15850774503@163.com
 *
 * 背景图片
 */

public class MyDrawBgView extends View {

    private static final String TAG = "MyDrawBgView";
    private Paint paint;
    private Path path;


    public MyDrawBgView(Context context) {
        this(context,null);

    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        path = new Path();
    }

    public MyDrawBgView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    /**
     *   动态的控制动画
     * @param y
     * @param parcent  出来的百分比
     */
    public void setTouchY(float y, float parcent)
    {
        path.reset();

        float width = getWidth() * parcent;
        float height = getHeight();

        //起始点
        float offsetY = height / 8;
        path.lineTo(0, offsetY);
        path.quadTo(width * 3 / 2, y, 0, height + offsetY);
        path.lineTo(0, height);
        path.close();

        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawPath(path, paint);
    }


    /**
     * 传递颜色
     * @param color
     */
    public void setColor(Drawable color)
    {
        if (color instanceof ColorDrawable)
        {
            ColorDrawable colorDrawable = (ColorDrawable) color;
            paint.setColor(colorDrawable.getColor());
        }
        else
        {
            //背景图片的实现变换
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.drawlayout.activity.MainActivity">

    <com.drawlayout.ui.MyDrawLayout

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

        <!--内容  区域-->
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/fake"
            />

        <!--划-->
        <com.drawlayout.ui.MyDrawSlideBar
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity= "start"
            app:maxTranslationX="66dp"
            android:background="@color/colorPrimary"
            >


            <TextView
                style="@style/MenuText"
                android:drawableLeft="@drawable/circle"
                android:text="朋友圈" />

            <TextView
                style="@style/MenuText"
                android:drawableLeft="@drawable/wallet"
                android:text="钱包" />

            <TextView
                style="@style/MenuText"
                android:drawableLeft="@drawable/coupon"
                android:text="优惠券" />


        </com.drawlayout.ui.MyDrawSlideBar>
    </com.drawlayout.ui.MyDrawLayout>


</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值