自定义view实用

Android 自定义view之悬浮动画

2019-07-31  本文已影响21人  总会颠沛流离
[图片上传中...(超级截屏_20190731_161319.png-40817c-1564564445176-0)] 超级截屏_20190731_161319.png image

第一步 EpetTypeSwitchView继承RelativeLayout

/**
 * @author 薛志辉
 * @Date: 2019/7/31
 * @Describe 标定管理
 */
public class EpetTypeSwitchView extends RelativeLayout {
public EpetTypeSwitchView(Context context) {
    super(context);
    init(context);
}



public EpetTypeSwitchView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);

}

public EpetTypeSwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
}
private void init(Context context) {
    View inflate = LayoutInflater.from(context).inflate(R.layout.view_epettype_switch, this,true);
    MyImageView viewById = (MyImageView) inflate.findViewById(R.id.epet_prompt);
    MyImageView imageViewDog = (MyImageView) inflate.findViewById(R.id.epet_type);
    LinearLayout switchlayout = (LinearLayout) inflate.findViewById(R.id.layout);
    //设置动画背景
    //其中R.drawable.animation_list就是上一步准备的动画描述文件的资源名
    imageViewDog.setBackgroundResource(R.drawable.anim_tab_home_switch);
    //获得动画对象
    //不一定是设置背景,也可以作为src图片设置
    AnimationDrawable animaition = (AnimationDrawable) imageViewDog.getBackground();
    //是否重复播放
    animaition.setOneShot(false);
    animaition.start();
    //点击眼睛后的点击事件
    switchlayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}
}

第二步:MyImageView 中

   @SuppressLint("AppCompatCustomView")
  public class MyImageView extends ImageView {


public MyImageView(Context paramContext)
{
    super(paramContext);
    initViews();
}

public MyImageView(Context paramContext, AttributeSet paramAttributeSet)
{
    super(paramContext, paramAttributeSet);
    initViews();
}

public MyImageView(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
{
    super(paramContext, paramAttributeSet, paramInt);
    initViews();
}

@TargetApi(21)
public MyImageView(Context paramContext, AttributeSet paramAttributeSet, int paramInt1, int paramInt2)
{
    super(paramContext, paramAttributeSet, paramInt1, paramInt2);
    initViews();
}

protected void initViews() {}
}

第三步:anim_tab_home_switch.xml

  <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"><!--持续循环播放-->
<item android:drawable="@drawable/epettype1_dog" android:duration="500" />
<item android:drawable="@drawable/epettype2_dog" android:duration="800" />
<item android:drawable="@drawable/epettype1_dog" android:duration="1500" />
</animation-list>

第四步view_epettype_switch(布局)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/andr  oid">
<LinearLayout
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:id="@+id/layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <detongs.hbqianze.him.amimaio.viwe.MyImageView
        android:id="@+id/epet_prompt"
        android:layout_width="25dp"
        android:layout_height="45dp"
        android:layout_marginRight="1.0dip"
        android:src="@drawable/epet_type_arrow_dog" />
    <detongs.hbqianze.him.amimaio.viwe.MyImageView
        android:id="@+id/epet_type"
        android:background="@drawable/anim_tab_home_switch"
        android:layout_width="25dp"
        android:layout_height="45dp" />
</LinearLayout>
</RelativeLayout>

第五步:

<?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=".MainActivity">
<detongs.hbqianze.him.amimaio.viwe.EpetTypeSwitchView
    android:id="@+id/epetTypeSwitchView"
    android:layout_width="wrap_content"
    android:background="@drawable/anim_tab_home_switch"
   android:visibility="visible"
    android:layout_marginBottom="80dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content"
>


</detongs.hbqianze.him.amimaio.viwe.EpetTypeSwitchView>


</RelativeLayout>

githup地址:https://github.com/xuezhihuixzh/Floating-animation.git

上一篇 下一篇

猜你喜欢

热点阅读