我正在研究一個具有主屏幕的應用程序。這個主屏幕應該像安卓主屏,您可以通過觸摸屏上的手指在幾個視圖之間切換。開發Android主屏幕
該解決方案很簡單。我有3視圖實例,右,左和當前視圖。我從之前初始化的viewflipper中獲得這些實例。由於我有一臺HTC G1屏幕,寬度爲320像素,高度爲480像素。
想象一下,當您觸摸屏幕時,您會捕捉動作向下動作事件的向下值。然後你移動你的手指,屏幕應該以完全相同的方式移動,所以你必須重新計算視圖的位置。它迄今爲止適用於我,但我面臨一個奇怪的問題。當您在不移動手指的情況下觸摸右側視圖,但將其保持在屏幕上時,視圖會消失並顯示左側視圖。
這裏是我的代碼:
public class MainActivity extends Activity implements OnTouchListener{
private ViewFlipper vf;
private float downXValue;
private View view1, view2, view3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.vf = (ViewFlipper) findViewById(R.id.flipper);
if(this.vf != null){
this.view1 = vf.getChildAt(0);
this.view2 = vf.getChildAt(1);
this.view3 = vf.getChildAt(2);
vf.setDisplayedChild(0);
}
LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main);
layMain.setOnTouchListener((OnTouchListener) this);
}
public boolean onTouch(View v, MotionEvent arg1) {
final View currentView = vf.getCurrentView();
final View leftView, rightView;
if(currentView == view1){
leftView = view3;
rightView = view2;
}else if(currentView == view2){
leftView = view1;
rightView = view3;
}else if(currentView == view3){
leftView = view2;
rightView = view1;
}else{
leftView = null;
rightView = null;
}
switch (arg1.getAction()){
case MotionEvent.ACTION_DOWN:{
this.downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:{
float currentX = arg1.getX();
if ((downXValue < currentX)){
if(currentView != view3){
float t3 = (320-(currentX-downXValue))/320;
this.vf.setInAnimation(AnimationHelper.inFromLeftAnimation(t3));
this.vf.setOutAnimation(AnimationHelper.outToRightAnimation(t3));
this.vf.showPrevious(); }
}
if ((downXValue > currentX)){
if(currentView != view2){
float t = (320-(downXValue-currentX))/320;
this.vf.setInAnimation(AnimationHelper.inFromRightAnimation(t));
this.vf.setOutAnimation(AnimationHelper.outToLeftAnimation(t));
this.vf.showNext();}
}
}
break;
case MotionEvent.ACTION_MOVE:{
leftView.setVisibility(View.VISIBLE);
rightView.setVisibility(View.VISIBLE);
float currentX = arg1.getX();
if(downXValue > currentX){
if(currentView != view2){
currentView.layout((int) (currentX - downXValue),
currentView.getTop(),
(int) (currentX - downXValue) + 320,
currentView.getBottom());
}
}
if(downXValue < currentX){
if(currentView != view3){
currentView.layout((int) (currentX - downXValue),
currentView.getTop(),
(int) (currentX - downXValue) + 320,
currentView.getBottom());
}
}
leftView.layout(currentView.getLeft()-320, leftView.getTop(),
currentView.getLeft(), leftView.getBottom());
rightView.layout(currentView.getRight(), rightView.getTop(),
currentView.getRight() + 320, rightView.getBottom());
}
}
return true;
}
public static class AnimationHelper {
public static Animation inFromRightAnimation(float param) {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +param,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(250);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
public static Animation outToLeftAnimation(float param) {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -param,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(250);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
// for the next movement
public static Animation inFromLeftAnimation(float param) {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -param,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(250);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
public static Animation outToRightAnimation(float param) {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +param,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(250);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
}
}
我覺得這樣的主屏幕是一個有趣的UI元素。
任何想法?
您也可以下載工作Eclipse項目在這裏:因爲似乎有
:http://www.megaupload.com/?d=3M3IYGGM
你可以重新加載ur eclipse項目,因爲megaupload.com被解僱了嗎? – Kalpesh 2012-07-03 13:26:34