夜神面试记录 2020-03-14

2020-03-16  本文已影响0人  向前的zz

夜神面试总结
字节码的理解, 单例,view布局, 滑动触摸

第一个题大概是这样的

public class RunTest {
    public static class A {
        public A() {
            System.out.println("A");
        }
    }

    public static class B extends A {
        public B() {
            System.out.println("B");
        }
    }

    public static class D {
        public  A a2 = new A();
        public static A a = new B();
        static  {
            System.out.println("------");
        }
    }

    public static void main(String[] args) {
        new D();
    }
}

默认先new静态的对象,然后再new非静态的对象
执行结果如下:

A
B
------
A

单例

静态内部类,双锁

view布局优化

略,一个icon变成4个icon然后使用frameLayout,我觉得就可以了

滑动触摸

就是实现下面类似的功能


1.gif

实现方式如下(可能有许多种,先写这一种)

//MainActivity
class MainActivity : AppCompatActivity() {
    companion object {
        val TAG = "MainActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        aView.setBView(bView)
        aView.setCView(cView)

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <View
        android:id="@+id/cView"
        android:background="#ff0000"
        android:layout_gravity="bottom"
        android:layout_width="200dp"
        android:layout_height="400dp"/>

    <View
        android:id="@+id/bView"
        android:background="#00ff00"
        android:layout_gravity="bottom"
        android:layout_width="100dp"
        android:layout_height="300dp"/>

    <com.zhousaito.eventdemo.widget.CView
        android:id="@+id/aView"
        android:background="#0000ff"
        android:layout_gravity="bottom"
        android:layout_width="100dp"
        android:layout_height="50dp"/>
</FrameLayout>
class CView
@JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) :
    View(context, attrs, defStyle) {
    companion object {
        var TAG = "CView"
    }

    var mLastY = 0f;

    var mBView: View? = null
    var mCView: View? = null

    override fun onTouchEvent(event: MotionEvent): Boolean {
        Log.e(MainActivity.TAG, "${y}  ${event.y}")
        val y = event.y

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mLastY = y
            }
            MotionEvent.ACTION_MOVE -> {
                val offsetY = y - mLastY
                Log.e(TAG, "" + top + " -- ${mBView?.top}")

                var bTop = mBView?.top ?: 0

                layout(
                    left, (top + offsetY).toInt(),
                    right, (bottom + offsetY).toInt()
                )

                //当top相同的时候,一起滑动
                if (bTop >= top) {
                    mBView?.layout(
                        mBView?.left ?: 0, ((mBView?.top ?: 0) + offsetY).toInt(),
                        mBView?.right ?: 0, ((mBView?.bottom ?: 0) + offsetY).toInt()
                    )
                    var cTop = mCView?.top ?: 0
                    if (cTop >= top) {
                        mCView?.layout(
                            mCView?.left ?: 0, ((mCView?.top ?: 0) + offsetY).toInt(),
                            mCView?.right ?: 0, ((mCView?.bottom ?: 0) + offsetY).toInt()
                        )
                    }
                }
            }

        }
        return true
    }

    fun setBView(bView: View?) {
        mBView = bView
    }

    fun setCView(cView: View?) {
        mCView = cView
    }
}
上一篇下一篇

猜你喜欢

热点阅读