Android利用自定义View实现简单的足球战术板
生活随笔
收集整理的這篇文章主要介紹了
Android利用自定义View实现简单的足球战术板
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
參考自:http://blog.csdn.net/lmj623565791/article/details/46858663
學(xué)習(xí)Android初期,之前接觸了一些UI設(shè)計,突發(fā)奇想想要做一個簡單的足球戰(zhàn)術(shù)板(自己是個足球愛好者)。
原理很簡單,也就是一個令View移動。
參考了網(wǎng)上有很多種方法,我這里是參考鴻洋的文章,利用ViewDragHelper實現(xiàn)View移動的目的。
先看一下效果圖:
附上源碼:
TacticsBoardLayout.class package com.example.chen.tacticsboard;import android.content.Context; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.LinearLayout;public class TacticsBoardLayout extends LinearLayout {private ViewDragHelper mDragger;protected void onFinshInflate(){super.onFinishInflate();}public TacticsBoardLayout(Context context, AttributeSet attrs){super(context, attrs);mDragger = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback(){/* 進行攔截,哪些view可以進行drag操作** return true 代表攔截所有view*/@Overridepublic boolean tryCaptureView(View child, int pointerId){return true;}//水平滑動,控制left@Overridepublic int clampViewPositionHorizontal(View child, int left, int dx){return left;}//垂直滑動,控制top@Overridepublic int clampViewPositionVertical(View child, int top, int dy){return top;}});}// 事件分發(fā)@Overridepublic boolean onInterceptTouchEvent(MotionEvent event){return mDragger.shouldInterceptTouchEvent(event);}@Overridepublic boolean onTouchEvent(MotionEvent event){mDragger.processTouchEvent(event);return true;}}布局:activity_main <?xml version="1.0" encoding="utf-8"?> <com.example.chen.tacticsboard.TacticsBoardLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"android:orientation="vertical"><TextViewandroid:layout_width="45dp"android:layout_height="45dp"android:id="@+id/play1"android:background="@drawable/player"android:gravity="center"android:textSize="20dp"android:text="2"android:layout_marginLeft="150dp"android:textColor="#FFFFFF"android:layout_marginTop="100dp"/><TextViewandroid:layout_width="45dp"android:layout_height="45dp"android:id="@+id/play2"android:background="@drawable/player"android:gravity="center"android:textSize="20dp"android:text="3"android:layout_marginLeft="260dp"android:textColor="#FFFFFF"android:layout_centerVertical="true"/><TextViewandroid:layout_width="45dp"android:layout_height="45dp"android:id="@+id/play3"android:background="@drawable/player"android:gravity="center"android:textSize="20dp"android:text="7"android:textColor="#FFFFFF"android:layout_marginLeft="55dp"android:layout_marginTop="25dp"/><TextViewandroid:layout_width="45dp"android:layout_height="45dp"android:id="@+id/play4"android:background="@drawable/player"android:gravity="center"android:textSize="20dp"android:text="9"android:textColor="#FFFFFF"android:layout_marginLeft="190dp"android:layout_marginTop="35dp"/><TextViewandroid:id="@+id/gk"android:layout_width="45dp"android:layout_height="45dp"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="27dp"android:background="@drawable/gk"android:gravity="center"android:text="1"android:textColor="#000000"android:textSize="18dp"android:layout_marginLeft="160dp"android:layout_marginTop="100dp"/></com.example.chen.tacticsboard.TacticsBoardLayout>
這里就是將子View實現(xiàn)最簡單的移動,其中,我們可以通過child來設(shè)置移動的對象,以及在 clampViewPositionHorizontal和 clampViewPositionVertical
中,可以寫上具體的想法為View移動進行更為詳細的定制。
總結(jié)
以上是生活随笔為你收集整理的Android利用自定义View实现简单的足球战术板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贪心算法-加油站
- 下一篇: 黄金价格走势软件下载,国内十大现货黄金正