文档章节

Android 移动换位小标签(类似小红帽)

胖子白
 胖子白
发布于 2016/06/20 13:50
字数 1154
阅读 13
收藏 0

 

ILabelView 代码:

package com.jm.labelviewdemo;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by FatWhite on 2015/11/25.
 */
public class ILabelView extends RelativeLayout {
    private static final int ANIMATIONEACHOFFSET = 600;
    //圆点中心点
    private int mTop;
    private int mLeft;
    //计算在父布局位置参数
    private int _xDelta;
    private int _yDelta;
    private ImageView labelIcon;
    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;
    private TextView text5;
    private TextView text6;
    private int type = 1;
    private int parentWidth = 0;
    int iconWidth = 0;
    int iconHeight = 0;
    private ArrayList<TextView> aList = new ArrayList<TextView>();

    public ILabelView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.label_view, this);
        labelIcon = (ImageView) findViewById(R.id.label_icon);
        text1 = (TextView) findViewById(R.id.text1);
        text2 = (TextView) findViewById(R.id.text2);
        text3 = (TextView) findViewById(R.id.text3);
        text4 = (TextView) findViewById(R.id.text4);
        text5 = (TextView) findViewById(R.id.text5);
        text6 = (TextView) findViewById(R.id.text6);
        aList.add(text1);
        aList.add(text2);
        aList.add(text3);
        aList.add(text4);
        aList.add(text5);
        aList.add(text6);
        updateVisibility();
        int w = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
        int h = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
        labelIcon.measure(w, h);
        iconWidth = labelIcon.getMeasuredWidth();
        iconHeight = labelIcon.getMeasuredHeight();
        labelIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                type = type == 6 ? 1 : ++type;
                updateVisibility();
                updateLocation(mLeft, mTop);
            }
        });
    }

    public ILabelView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.label_view, this);
        labelIcon = (ImageView) findViewById(R.id.label_icon);
        text1 = (TextView) findViewById(R.id.text1);
        text2 = (TextView) findViewById(R.id.text2);
    }
    //初次调用画布局,啦啦啦啦
    public void draw(ViewGroup parent, int mLeft, int mTop) {
        this.parentWidth = parent.getWidth();
        this.mLeft = mLeft;
        this.mTop = mTop;
        updateLocation(mLeft, mTop);
        parent.addView(this);

    }

    //点击的时候更换label背景
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void updateLocation(int mLeft, int mTop) {
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int w = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
        int h = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
        LayoutParams iconparams = (LayoutParams) labelIcon.getLayoutParams();
        switch (type) {
            case 1:
                LayoutParams p1 = (LayoutParams) text1.getLayoutParams();
                p1.setMargins(0, 0, iconWidth / 2, iconHeight / 2);
                text1.setLayoutParams(p1);
                text1.measure(w, h);
                params.width = text1.getMeasuredWidth() + iconWidth / 2;
                params.height = text1.getMeasuredHeight() + iconHeight / 2;
                iconparams.removeRule(RelativeLayout.CENTER_VERTICAL);
                iconparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                iconparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                params.setMargins(mLeft - text1.getMeasuredWidth(), mTop - text1.getMeasuredHeight(), 0, 0);
                break;
            case 2:
                LayoutParams p2 = (LayoutParams) text2.getLayoutParams();
                p2.setMargins(iconWidth / 2, 0, 0, iconHeight / 2);
                text2.setLayoutParams(p2);
                text2.measure(w, h);
                params.width = text2.getMeasuredWidth() + iconWidth / 2;
                params.height = text2.getMeasuredHeight() + iconHeight / 2;
                iconparams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                iconparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.setMargins(mLeft - iconWidth / 2, mTop - text2.getMeasuredHeight(), 0, 0);
                break;
            case 3:
                LayoutParams p3 = (LayoutParams) text3.getLayoutParams();
                p3.setMargins(iconWidth / 2, 0, 0, 0);
                text3.setLayoutParams(p3);
                text3.measure(w, h);
                params.width = text3.getMeasuredWidth() + iconWidth / 2;
                params.height = text3.getMeasuredHeight();
                iconparams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                iconparams.addRule(RelativeLayout.CENTER_VERTICAL);
                params.setMargins(mLeft - iconWidth / 2, mTop - text3.getMeasuredHeight() / 2, 0, 0);
                break;
            case 4:
                LayoutParams p4 = (LayoutParams) text4.getLayoutParams();
                p4.setMargins(iconWidth / 2, iconHeight / 2, 0, 0);
                text4.setLayoutParams(p4);
                text4.measure(w, h);
                params.width = text4.getMeasuredWidth() + iconWidth / 2;
                params.height = text4.getMeasuredHeight() + iconHeight / 2;
                iconparams.removeRule(RelativeLayout.CENTER_VERTICAL);
                iconparams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                params.setMargins(mLeft - iconWidth / 2, mTop - iconHeight / 2, 0, 0);
                break;
            case 5:
                LayoutParams p5 = (LayoutParams) text5.getLayoutParams();
                p5.setMargins(0, iconHeight / 2, iconWidth / 2, 0);
                text5.setLayoutParams(p5);
                text5.measure(w, h);
                params.width = text5.getMeasuredWidth() + iconWidth / 2;
                params.height = text5.getMeasuredHeight() + iconHeight / 2;
                iconparams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
                iconparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                params.setMargins(mLeft - text5.getMeasuredWidth(), mTop - iconHeight / 2, 0, 0);
                break;
            case 6:
                LayoutParams p6 = (LayoutParams) text6.getLayoutParams();
                p6.setMargins(0, 0, iconWidth / 2, 0);
                text6.setLayoutParams(p6);
                text6.measure(w, h);
                params.width = text6.getMeasuredWidth() + iconWidth / 2;
                params.height = text6.getMeasuredHeight();
                iconparams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
                iconparams.addRule(RelativeLayout.CENTER_VERTICAL);
                params.setMargins(mLeft - text6.getMeasuredWidth(), mTop - text6.getMeasuredHeight() / 2, 0, 0);
                break;
        }
        setLayoutParams(params);
        wave();
    }

    //小圆点动画,可以不加哦
    public void wave() {
        AnimationSet as = new AnimationSet(true);
        ScaleAnimation sa = new ScaleAnimation(1f, 1.5f, 1f, 1.5f, ScaleAnimation.RELATIVE_TO_SELF,
                0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(ANIMATIONEACHOFFSET * 3);
        sa.setRepeatCount(10);// 设置循环
        AlphaAnimation aniAlp = new AlphaAnimation(1, 0.1f);
        aniAlp.setRepeatCount(10);// 设置循环
        as.setDuration(ANIMATIONEACHOFFSET * 3);
        as.addAnimation(sa);
        as.addAnimation(aniAlp);
        labelIcon.startAnimation(as);
    }

    //更新Visibility
    private void updateVisibility() {
        for (int i = 0; i < aList.size(); i++) {
            if (i + 1 == type) {
                aList.get(i).setVisibility(View.VISIBLE);
            } else {
                aList.get(i).setVisibility(View.GONE);
            }
        }
    }

    //移动的时候更新小小圆点中心哦
    public void updateMarginValue(int left, int top) {
        int w = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
        int h = MeasureSpec.makeMeasureSpec(0,
                MeasureSpec.UNSPECIFIED);
        switch (type) {
            case 1:
                text1.measure(w, h);
                mLeft=left+text1.getMeasuredWidth();
                mTop=top+text1.getMeasuredHeight();
                break;
            case 2:
                text2.measure(w, h);
                mLeft=left+iconWidth / 2;
                mTop=top+text2.getMeasuredHeight();
                break;
            case 3:
                text3.measure(w, h);
                mLeft=left+iconWidth / 2;
                mTop=top+text3.getMeasuredHeight() / 2;
                break;
            case 4:
                text4.measure(w, h);
                mLeft=left+iconWidth / 2;
                mTop=top+iconHeight / 2;
                break;
            case 5:
                text5.measure(w, h);
                mLeft=left+text5.getMeasuredWidth();
                mTop=top+iconHeight / 2;
                break;
            case 6:
                text6.measure(w, h);
                mLeft=left+text6.getMeasuredWidth();
                mTop=top+text6.getMeasuredHeight()/2;
                break;

        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                LayoutParams lParams = (LayoutParams) this
                        .getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                LayoutParams layoutParams = (LayoutParams) this
                        .getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                setLayoutParams(layoutParams);
                updateMarginValue(X - _xDelta,Y - _yDelta);
                break;
        }
//        _root.invalidate();
        return true;
    }
}

 

对应xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tag_1"
        android:text="I'm label"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tag_2"
        android:text="I'm label"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/tag_3"
        android:text="I'm label"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tag_4"
        android:text="I'm label"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tag_5"
        android:text="I'm label"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/text6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/tag_6"
        android:text="I'm label"
        android:textColor="#ffffff" />

    <ImageView
        android:id="@+id/label_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/dot" />

</RelativeLayout>

应用:

private void initView(){
    parentRL= (RelativeLayout) findViewById(R.id.parentRl);
    RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) parentRL.getLayoutParams();
    params.width= (int) PhoneSize.getInstance().getWidth4pix();
    params.height=(int) PhoneSize.getInstance().getWidth4pix();
    parentRL.setLayoutParams(params);
    ILabelView labelView=new ILabelView(ParentActivity.this);
    labelView.draw(parentRL,500,500);
}

 

菜鸟一枚,有很多 不足之处请不吝指导。

© 著作权归作者所有

共有 人打赏支持
胖子白
粉丝 0
博文 14
码字总数 1976
作品 0
东城
程序员
私信 提问
Android游戏开发之Tween动画的实现(三十二)

Android游戏开发之Tween动画的实现 雨松MOMO原创文章如转载,请注明:转载自雨松MOMO的博客原文地址:http://blog.csdn.net/xys289187120/article/details/6747877 今天和大伙讨论一下Android...

彭博
2012/03/09
395
0
Android游戏开发之Tween动画的实现(三十二)

Android游戏开发之Tween动画的实现 雨松MOMO原创文章如转载,请注明:转载自雨松MOMO的博客原文地址:http://blog.csdn.net/xys289187120/article/details/6747877 今天和大伙讨论一下Android...

晨曦之光
2012/03/07
549
0
【组图】Android 3.0界面图赏 首款平板电脑亮相

虽然Android 3.0(蜂巢)还没有发布,不过第一款将会搭载这套系统上市的平板电脑Moto Xoom已经在CES大展上登台亮相。令人遗憾的是这次发布的机器中并没有预装Android 3.0系统,不过厂商在这台机...

老枪
2011/01/13
2.5K
13
高焕堂《android从程序员到架构师之路》YY答疑

台湾Android教父高焕堂老师新作《Android从程序员架构师之路》国内唯一的课程,学习高老师提出的EIT架构设计模式,能从普通Android工程师往Android架构设计师这个新的台阶攀登,同时更加熟悉...

达尔文子
2013/07/17
879
2
简单链接Simplelink 传感器标签SensorTag

传感器标签现在支持iBeacon技术 为了让你的传感器标签升级到与iBeancon技术兼容,你需要和苹果公司签约一个iBeacon许可,然后你可以请求访问软件,通过点击这里http://www.ti.com/tool/sens...

刘小米
2014/11/11
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Spring源码学习笔记-1-Resource

打算补下基础,学习下Spring源码,参考书籍是《Spring源码深度解析》,使用版本是Spring 3.2.x,本来想试图用脑图记录的,发现代码部分不好贴,还是作罢,这里只大略记录下想法,不写太细了 ...

zypy333
今天
11
0
RestClientUtil和ConfigRestClientUtil区别说明

RestClientUtil directly executes the DSL defined in the code. ConfigRestClientUtil gets the DSL defined in the configuration file by the DSL name and executes it. RestClientUtil......

bboss
今天
17
0

中国龙-扬科
昨天
2
0
Linux系统设置全局的默认网络代理

更改全局配置文件/etc/profile all_proxy="all_proxy=socks://rahowviahva.ml:80/"ftp_proxy="ftp_proxy=http://rahowviahva.ml:80/"http_proxy="http_proxy=http://rahowviahva.ml:80/"......

临江仙卜算子
昨天
11
0
java框架学习日志-6(bean作用域和自动装配)

本章补充bean的作用域和自动装配 bean作用域 之前提到可以用scope来设置单例模式 <bean id="type" class="cn.dota2.tpye.Type" scope="singleton"></bean> 除此之外还有几种用法 singleton:......

白话
昨天
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部