日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android钢琴滑动代码,android 钢琴界面实现

發布時間:2023/12/19 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android钢琴滑动代码,android 钢琴界面实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

近在做一個鋼琴的東西,關于這個界面如何設計畫了很長時間,主要是考慮到針對不同的分辨率,如果只針對一種分辨率的話用絕對布局可以實現,實現的基本思想是每個白色的鍵的位置是可以計算出來的,屏幕的寬度可以獲得到,白鍵是將屏幕均勻的分成8份,所以每個白鍵所處的位置是可以得到的,而由于黑鍵的實現采用的是重寫ViewGroup的方法,先計算出每個黑鍵的位置,然后再執行onLayout方法將黑鍵放在指定的位置。

布局如下:

[html]

android:id="@+id/mClickLayout"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal">

android:id="@+id/mPanoClickWhiteOne"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="1"

/>

android:id="@+id/mPanoClickWhiteTwo"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="2"

/>

android:id="@+id/mPanoClickWhiteThree"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="3"

/>

android:id="@+id/mPanoClickWhiteFour"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="4"

/>

android:id="@+id/mPanoClickWhiteFive"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="5"

/>

android:id="@+id/mPanoClickWhiteSix"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="6"

/>

android:id="@+id/mPanoClickWhiteSeven"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="7"

/>

android:id="@+id/mPanoClickWhiteEight"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="8"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/mPanoClickBlackOne"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="9"

/>

android:id="@+id/mPanoClickBlackTwo"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="10"

/>

android:id="@+id/mPanoClickBlackThree"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="11"

/>

android:id="@+id/mPanoClickBlackFour"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="12"

/>

android:id="@+id/mPanoClickBlackFive"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="13"

/>

android:id="@+id/mClickLayout"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal">

android:id="@+id/mPanoClickWhiteOne"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="1"

/>

android:id="@+id/mPanoClickWhiteTwo"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="2"

/>

android:id="@+id/mPanoClickWhiteThree"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="3"

/>

android:id="@+id/mPanoClickWhiteFour"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="4"

/>

android:id="@+id/mPanoClickWhiteFive"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="5"

/>

android:id="@+id/mPanoClickWhiteSix"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="6"

/>

android:id="@+id/mPanoClickWhiteSeven"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="7"

/>

android:id="@+id/mPanoClickWhiteEight"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1000"

android:tag="8"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/mPanoClickBlackOne"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="9"

/>

android:id="@+id/mPanoClickBlackTwo"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="10"

/>

android:id="@+id/mPanoClickBlackThree"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="11"

/>

android:id="@+id/mPanoClickBlackFour"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="12"

/>

android:id="@+id/mPanoClickBlackFive"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:tag="13"

/>

LinearLayout內的控件為白鍵,一共有8個白鍵,每個白鍵的權值是1000,所以每個白鍵的位置是可以計算出來的,com.example.crazypanp.view.BlackLayout是重寫的ViewGroup.主要用來排放黑鍵。

代碼如下:

[java]

public class BlackLayout extends ViewGroup {

private List mLeftPositions;

public BlackLayout(Context context, AttributeSet attrs) {

super(context, attrs);

mLeftPositions = new ArrayList();

}

private void addPosition(int pScreenWidth, int pChildWidth){

int pScreenUnit = pScreenWidth / 8;

mLeftPositions.clear();

mLeftPositions.add(pScreenUnit - 2 * pChildWidth / 3);

mLeftPositions.add(2 * pScreenUnit - pChildWidth / 3);

mLeftPositions.add(4 * pScreenUnit - 2 * pChildWidth / 3);

mLeftPositions.add(5 * pScreenUnit - pChildWidth / 2);

mLeftPositions.add(6 * pScreenUnit - pChildWidth / 3);

}

@Override

protected void onLayout(boolean arg0, int l, int t, int r, int b) {

// TODO Auto-generated method stub

int childCount = this.getChildCount();

int gap = 0;

int space=0;

if(this.getChildCount() >= 1){

addPosition(this.getWidth(), this.getChildAt(0).getWidth());

}

for( int i = 0; i < this.getChildCount(); i++){

View child = this.getChildAt(i);

child.measure(r - l, b - t);

}

for (int i = 0; i < childCount; i++) {

View child = this.getChildAt(i);

child.setVisibility(View.VISIBLE);

child.measure(r - l, b - t);

int childWidth = child.getMeasuredWidth();

int childHeight = child.getMeasuredHeight();

child.layout(mLeftPositions.get(i), t, mLeftPositions.get(i) + childWidth, t + childHeight);

}

}

}

public class BlackLayout extends ViewGroup {

private List mLeftPositions;

public BlackLayout(Context context, AttributeSet attrs) {

super(context, attrs);

mLeftPositions = new ArrayList();

}

private void addPosition(int pScreenWidth, int pChildWidth){

int pScreenUnit = pScreenWidth / 8;

mLeftPositions.clear();

mLeftPositions.add(pScreenUnit - 2 * pChildWidth / 3);

mLeftPositions.add(2 * pScreenUnit - pChildWidth / 3);

mLeftPositions.add(4 * pScreenUnit - 2 * pChildWidth / 3);

mLeftPositions.add(5 * pScreenUnit - pChildWidth / 2);

mLeftPositions.add(6 * pScreenUnit - pChildWidth / 3);

}

@Override

protected void onLayout(boolean arg0, int l, int t, int r, int b) {

// TODO Auto-generated method stub

int childCount = this.getChildCount();

int gap = 0;

int space=0;

if(this.getChildCount() >= 1){

addPosition(this.getWidth(), this.getChildAt(0).getWidth());

}

for( int i = 0; i < this.getChildCount(); i++){

View child = this.getChildAt(i);

child.measure(r - l, b - t);

}

for (int i = 0; i < childCount; i++) {

View child = this.getChildAt(i);

child.setVisibility(View.VISIBLE);

child.measure(r - l, b - t);

int childWidth = child.getMeasuredWidth();

int childHeight = child.getMeasuredHeight();

child.layout(mLeftPositions.get(i), t, mLeftPositions.get(i) + childWidth, t + childHeight);

}

}

}

在onLayout函數中首先確定每個黑鍵的位置,然后再通過onLayout函數進行位置的擺放。

總結

以上是生活随笔為你收集整理的Android钢琴滑动代码,android 钢琴界面实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。