自定义FloatLayout实现带动画的悬浮视图

前言

最近在项目开发中有账号关联和切换账号的需求,并且还要有相应的显示和隐藏动画,这时就用到了布局动画 LayoutTransition,LayoutTransition动画是基于ViewGroup的,当你调用View的setVisibility()方法来控制其View的显示或消失时就可以触发该动画,该动画有四中类型:
1.APPEARING — 元素在容器中显现时需要动画显示
2.CHANGE_APPEARING — 由于容器中要显现一个新的元素,其它元素的变化需要动画显示
3.DISAPPEARING —元素在容器中消失时需要动画显示
4.CHANGE_DISAPPEARING — 由于容器中某个元素要消失,其它元素的变化需要动画显示

明白了LayoutTransition的基本使用,我们就可以通过自定义一个ViewGroup来实现该功能
效果图如下:

初始化状态
点击后展开状态

实现

FloatLayout全部代码:

/**
 * Created by Mersens on 2016/10/21.
 */

public class FloatLayout extends LinearLayout {
    private LayoutParams params = null;
    private Context context;
    private List<View> list = null;
    private static final int ANIMATION_TIME = 20;
    private boolean isMenuOpened;
    private int i = 0;
    private int j = 0;
    private ShowRunnable showRunnable = new ShowRunnable();
    private HideRunnable hideRunnable = new HideRunnable();
    private LayoutTransition layoutTransition;
    private LayoutInflater inflater;
    private OnItemClickListener onItemClickListener;

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

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

    public FloatLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    private void init() {
        layoutTransition = new LayoutTransition();
        setLayoutTransition(layoutTransition);
        list = new ArrayList<>();
        inflater = LayoutInflater.from(context);
        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.CENTER;
        params.rightMargin = 10;
        setGravity(Gravity.RIGHT);
        setBackgroundColor(getResources().getColor(android.R.color.transparent));
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(null, View.TRANSLATION_X, 0F, 0F).
                setDuration(layoutTransition.getDuration(LayoutTransition.APPEARING));
        layoutTransition.setAnimator(LayoutTransition.APPEARING, animator1);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(null, View.TRANSLATION_X, 0F, 0F).
                setDuration(layoutTransition.getDuration(LayoutTransition.DISAPPEARING));
        layoutTransition.setAnimator(LayoutTransition.DISAPPEARING, animator2);
    }

    public void addMainItem(final ItemBean item) {
        View mainView = inflater.inflate(R.layout.list_item, null);
        ImageView imageView = (ImageView) mainView.findViewById(R.id.ItemImage);
        TextView tv_name = (TextView) mainView.findViewById(R.id.tvname);
        Glide.with(context).load(item.getImg()).into(imageView);
        tv_name.setText(item.getName());
        mainView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMenuOpened) {
                    hidePromotedActions();
                    isMenuOpened = false;
                } else {
                    isMenuOpened = true;
                    showPromotedActions();
                }
            }
        });
        addView(mainView, params);
    }

    public void addItem(List<ItemBean> items, OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        for (int i = 0; i < items.size(); i++) {
            View item = inflater.inflate(R.layout.list_item, null);
            ImageView imageView = (ImageView) item.findViewById(R.id.ItemImage);
            Glide.with(context).load(items.get(i).getImg()).into(imageView);
            TextView tv_name = (TextView) item.findViewById(R.id.tvname);
            tv_name.setText(items.get(i).getName());
            item.setOnClickListener(new MyOnClicKListener(i));
            item.setVisibility(View.GONE);
            list.add(item);
            addView(item, params);
        }
    }

    private void hidePromotedActions() {
        invalidate();
        handler.postDelayed(hideRunnable, ANIMATION_TIME);
    }

    private void showPromotedActions() {
        invalidate();
        handler.postDelayed(showRunnable, ANIMATION_TIME);
    }


    class HideRunnable implements Runnable {
        @Override
        public void run() {
            j++;
            if (j <= list.size()) {
                list.get(j - 1).setVisibility(View.GONE);
                handler.postDelayed(hideRunnable, ANIMATION_TIME);
            } else {
                handler.removeCallbacks(hideRunnable);
                j = 0;
            }
        }
    }

    class ShowRunnable implements Runnable {
        @Override
        public void run() {
            i++;
            if (i <= list.size()) {
                list.get(i - 1).setVisibility(View.VISIBLE);
                handler.postDelayed(showRunnable, ANIMATION_TIME);
            } else {
                handler.removeCallbacks(showRunnable);
                i = 0;
            }
        }
    }

    public class MyOnClicKListener implements OnClickListener {
        private int pos;

        public MyOnClicKListener(int pos) {
            this.pos = pos;
        }

        @Override
        public void onClick(View view) {
            onItemClickListener.onItemClick(pos);

        }
    }

    public interface OnItemClickListener {
         void onItemClick(int pos);
    }


}


list_item.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/itemlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <com.mersens.floatlayout.floatlayout.CircleImageView
        android:id="@+id/ItemImage"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center_horizontal"
         >
    </com.mersens.floatlayout.floatlayout.CircleImageView>

    <TextView
        android:id="@+id/tvname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Title"
        android:textSize="12sp"
        >
    </TextView>

</LinearLayout>

这里使用了一个CircleImageView,该View是使用别人的控件,通过自定义ImageView来实现圆形的图片,网上搜索一下会有很多这样的例子,但你也不必这么做,如果你使用了Glide来加载图片的话,Glide本身就可以实现对图片的裁剪,达到圆形展示的效果,不过这里还是打算贴出CircleImageView的代码:

public class CircleImageView extends ImageView {

    private static final ScaleType SCALE_TYPE = ScaleType.CENTER_INSIDE;

    private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
    private static final int COLORDRAWABLE_DIMENSION = 1;

    private static final int DEFAULT_BORDER_WIDTH = 4;
    private static final int DEFAULT_BORDER_COLOR = Color.WHITE;

    private final RectF mDrawableRect = new RectF();
    private final RectF mBorderRect = new RectF();

    private final Matrix mShaderMatrix = new Matrix();
    private final Paint mBitmapPaint = new Paint();
    private final Paint mBorderPaint = new Paint();

    private int mBorderColor = DEFAULT_BORDER_COLOR;
    private int mBorderWidth = DEFAULT_BORDER_WIDTH;

    private Bitmap mBitmap;
    private BitmapShader mBitmapShader;
    private int mBitmapWidth;
    private int mBitmapHeight;

    private float mDrawableRadius;
    private float mBorderRadius;

    private boolean mReady;
    private boolean mSetupPending;

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

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

    public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        super.setScaleType(SCALE_TYPE);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);

        mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);
        mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR);

        a.recycle();

        mReady = true;

        if (mSetupPending) {
            setup();
            mSetupPending = false;
        }
    }

    @Override
    public ScaleType getScaleType() {
        return SCALE_TYPE;
    }

    @Override
    public void setScaleType(ScaleType scaleType) {
        if (scaleType != SCALE_TYPE) {
            throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getDrawable() == null) {
            return;
        }

        canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);
    }

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

    public int getBorderColor() {
        return mBorderColor;
    }

    public void setBorderColor(int borderColor) {
        if (borderColor == mBorderColor) {
            return;
        }

        mBorderColor = borderColor;
        mBorderPaint.setColor(mBorderColor);
        invalidate();
    }

    public int getBorderWidth() {
        return mBorderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        if (borderWidth == mBorderWidth) {
            return;
        }

        mBorderWidth = borderWidth;
        setup();
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        mBitmap = bm;
        setup();
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        mBitmap = getBitmapFromDrawable(drawable);
        setup();
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        mBitmap = getBitmapFromDrawable(getDrawable());
        setup();
    }

    private Bitmap getBitmapFromDrawable(Drawable drawable) {
        if (drawable == null) {
            return null;
        }

        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        try {
            Bitmap bitmap;

            if (drawable instanceof ColorDrawable) {
                bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
            } else {
                bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
            }

            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        } catch (OutOfMemoryError e) {
            return null;
        }
    }

    private void setup() {
        if (!mReady) {
            mSetupPending = true;
            return;
        }

        if (mBitmap == null) {
            return;
        }

        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        mBitmapPaint.setAntiAlias(true);
        mBitmapPaint.setShader(mBitmapShader);

        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setColor(mBorderColor);
        mBorderPaint.setStrokeWidth(mBorderWidth);

        mBitmapHeight = mBitmap.getHeight();
        mBitmapWidth = mBitmap.getWidth();

        mBorderRect.set(0, 0, getWidth(), getHeight());
        mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);

        mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth);
        mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);

        updateShaderMatrix();
        invalidate();
    }

    private void updateShaderMatrix() {
        float scale;
        float dx = 0;
        float dy = 0;

        mShaderMatrix.set(null);

        if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
            scale = mDrawableRect.height() / (float) mBitmapHeight;
            dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
        } else {
            scale = mDrawableRect.width() / (float) mBitmapWidth;
            dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
        }

        mShaderMatrix.setScale(scale, scale);
        mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);

        mBitmapShader.setLocalMatrix(mShaderMatrix);
    }

}

自定义属性attrs.xml文件

<resources>

    <declare-styleable name="CircleImageView">
        <attr name="border_width" format="dimension" />
        <attr name="border_color" format="color" />
    </declare-styleable>

</resources>

ItemBean实体类

/**
 * Created by Mersens on 2016/10/22.
 */

public class ItemBean {
    private String name;
    private String img;
    public ItemBean(){

    }
    public ItemBean(String name, String img) {
        this.name = name;
        this.img = img;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

添加Glide的依赖

compile 'com.github.bumptech.glide:glide:3.7.0'

具体使用方法

public class MainActivity extends AppCompatActivity {
    private FloatLayout floatLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void init(){
        floatLayout=(FloatLayout)findViewById(R.id.mFloatLayout);
        ItemBean mainItem=new ItemBean("小明","http://staging.topmd.cn/UploadFile/image/20160914103216400.jpg");
        floatLayout.addMainItem(mainItem);
        List<ItemBean> list=new ArrayList<>();
        list.add(new ItemBean("小王","http://staging.topmd.cn/UploadFile/image/20160923162109048.jpg"));
        list.add(new ItemBean("小强","http://staging.topmd.cn/UploadFile/image/20161012160235822.jpg"));
        floatLayout.addItem(list, new FloatLayout.OnItemClickListener() {
            @Override
            public void onItemClick(int pos) {
                Toast.makeText(getApplicationContext(), "item"+pos, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342

推荐阅读更多精彩内容