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 钢琴界面实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android listview下拉刷新
- 下一篇: 中国载人航天工程公开征集航天育种,让优良