Browse Source

1.添加自定义布局,实现MainActivity中的图标效果

master
wangwanlei 9 months ago
parent
commit
9d7470be00

+ 87
- 0
app/src/main/java/com/xhly/manageapp/customview/CombinationCardView.kt View File

@@ -0,0 +1,87 @@
1
+package com.xhly.manageapp.customview
2
+
3
+import android.annotation.SuppressLint
4
+import android.content.Context
5
+import android.graphics.drawable.Drawable
6
+import android.util.AttributeSet
7
+import android.view.LayoutInflater
8
+import android.view.MotionEvent
9
+import android.view.View
10
+import android.widget.FrameLayout
11
+import android.widget.ImageView
12
+import android.widget.LinearLayout
13
+import android.widget.TextView
14
+import com.xhly.manageapp.R
15
+
16
+class CombinationCardView : FrameLayout {
17
+    private var cv: LinearLayout
18
+    private var root: View
19
+    private var iv: ImageView
20
+    private var tv:TextView
21
+    private var x = 0f
22
+    private var y = 0f
23
+    private var listener: OnClickListener?=null
24
+    constructor(mContext: Context) : this(mContext, null)
25
+
26
+    constructor(mContext: Context, mAttributeSet: AttributeSet?) : this(mContext, mAttributeSet, 0)
27
+    constructor(
28
+        mContext: Context,
29
+        mAttributeSet: AttributeSet?,
30
+        defStyleAttr: Int
31
+    ) : super(mContext, mAttributeSet, defStyleAttr) {
32
+        root= LayoutInflater.from(mContext).inflate(R.layout.btncardview, this)
33
+        cv =root.findViewById(R.id.llayout_cv)
34
+        iv =root.findViewById(R.id.iv)
35
+        tv=root.findViewById(R.id.tv)
36
+    }
37
+
38
+
39
+    @SuppressLint("ClickableViewAccessibility")
40
+    override fun onTouchEvent(event: MotionEvent?): Boolean {
41
+
42
+        when (event!!.action) {
43
+            MotionEvent.ACTION_DOWN -> {
44
+                 x = event!!.x
45
+                 y = event!!.y
46
+                cv.animate().scaleX(0.93f).scaleY(0.93f).setDuration(500).start()
47
+                changeState(0)
48
+            }
49
+
50
+            MotionEvent.ACTION_UP -> {
51
+                cv.postDelayed({
52
+                    cv.animate().scaleX(1f).scaleY(1f).setDuration(500).start()
53
+                    changeState(1)
54
+                },120)
55
+                listener?.let {
56
+                    it.onClick(root)
57
+                }
58
+            }
59
+            MotionEvent.ACTION_CANCEL->{
60
+                cv.animate().scaleX(1f).scaleY(1f).setDuration(500).start()
61
+                changeState(1)
62
+            }
63
+        }
64
+        return true
65
+    }
66
+
67
+    override fun setOnClickListener(l: OnClickListener?) {
68
+        this.listener=l
69
+        super.setOnClickListener(l)
70
+    }
71
+
72
+
73
+    private fun changeState(states:Int) {
74
+        if (states == 0) {
75
+            //iv.visibility = View.INVISIBLE
76
+        } else {
77
+            //iv.visibility = View.INVISIBLE
78
+        }
79
+    }
80
+    fun setText(s:String){
81
+        tv.text = s
82
+    }
83
+
84
+    fun setImageDrawable(drawable:Drawable){
85
+        iv.setImageDrawable(drawable)
86
+    }
87
+}

+ 29
- 0
app/src/main/res/drawable/shape_shadow_bg.xml View File

@@ -0,0 +1,29 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3
+    <item
4
+        android:left="2dp"
5
+        android:top="2dp">
6
+        <shape android:shape="rectangle">
7
+            <!--这里可以是梯度的值,也可以是solid,看实际的效果自己调测-->
8
+            <gradient
9
+                android:angle="90"
10
+                android:centerX="50"
11
+                android:centerY="50"
12
+                android:endColor="#E9F4FD"
13
+                android:startColor="#DCF1FF" />
14
+            <corners
15
+                android:radius="13dp" />
16
+        </shape>
17
+    </item>
18
+
19
+    <item
20
+        android:bottom="2dp"
21
+        android:right="2dp">
22
+        <shape android:shape="rectangle">
23
+            <solid android:color="#99FFFFFF" />
24
+            <stroke android:width="2dp" android:color="@color/white"/>
25
+            <corners android:radius="13dp" />
26
+        </shape>
27
+    </item>
28
+</layer-list>
29
+

+ 33
- 0
app/src/main/res/layout/btncardview.xml View File

@@ -0,0 +1,33 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+    xmlns:app="http://schemas.android.com/apk/res-auto"
4
+    xmlns:tools="http://schemas.android.com/tools"
5
+    android:id="@+id/llayout_cv"
6
+    android:layout_width="match_parent"
7
+    android:layout_height="match_parent"
8
+    android:layout_gravity="center"
9
+    android:background="@drawable/shape_shadow_bg"
10
+    android:gravity="center"
11
+    android:orientation="vertical"
12
+    android:paddingTop="20dp"
13
+    android:paddingBottom="20dp"
14
+    tools:ignore="UselessParent">
15
+
16
+    <com.makeramen.roundedimageview.RoundedImageView
17
+        android:id="@+id/iv"
18
+        android:layout_width="47dp"
19
+        android:layout_height="47dp"
20
+        app:riv_corner_radius="13dp"
21
+        tools:ignore="ContentDescription" />
22
+
23
+    <TextView
24
+        android:id="@+id/tv"
25
+        android:layout_width="match_parent"
26
+        android:layout_height="wrap_content"
27
+        android:layout_marginLeft="2dp"
28
+        android:layout_marginTop="10dp"
29
+        android:layout_marginRight="2dp"
30
+        android:gravity="center"
31
+        android:textColor="#FF253A70"
32
+        android:textSize="15sp" />
33
+</LinearLayout>

Loading…
Cancel
Save