当前位置: 主页 >业界资讯 >查看内容
Android翻页特效

IT蓝豹发表于 2016/1/4 10:04

对大家有帮助可以看看,

android-flip 是一个能够轻松帮你实现水平以及竖直翻页特效的库,但是在判断翻页的时候有bug,我们需要在FlipCards.java中找到这一段:

 

1
2
3
if (Math.abs(getPageIndexFromAngle(accumulatedAngle + angleDelta) - lastPageIndex) <= 1) {
              accumulatedAngle += angleDelta;
           }

将它更改为:

1
2
3
4
5
6
if(((accumulatedAngle + angleDelta > lastPageIndex*180)
                && (accumulatedAngle + angleDelta <= (lastPageIndex+1) * 180)) || 
                ((accumulatedAngle + angleDelta < lastPageIndex*180) &&
                    (accumulatedAngle + angleDelta >= (lastPageIndex-1) * 180))){
              accumulatedAngle += angleDelta;
            }


而在翻页的时候会有闪烁现象产生,为了减轻现象的发生,我们需要修改另外一个地方,在FlipViewController.java中找到这一段:

1
2
3
4
5
6
7
8
9
10
void postHideFlipAnimation() {
      if (inFlipAnimation) {
        handler.post(new Runnable() {
          @Override
          public void run() {
            hideFlipAnimation();
          }
        });
      }
    }


修改为:

1
2
3
4
5
6
7
8
9
10
void postHideFlipAnimation() {
      if (inFlipAnimation) {
        handler.postDelayed(new Runnable() {
          @Override
          public void run() {
            hideFlipAnimation();
          }
        }, 200);
      }
    }


然后我们就可以轻松地用它来为我们的app添加翻页特效,在Activity中添加代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.nekocode.xuedao;
 
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.aphidmobile.flip.FlipViewController;
import com.nekocode.xuedao.adapter.SubscribeIndexAdapter;
 
public class SubsecribeIndexActivity extends SherlockFragmentActivity {
    private PublicData pd;
    private FlipViewController mFlipView;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pd = PublicData.getInstance();
         
        mFlipView = new FlipViewController(this, FlipViewController.HORIZONTAL);
        mFlipView.setAdapter(new SubscribeIndexAdapter(this));
         
        setContentView(mFlipView);
    }
 
    @Override
    protected void onResume() {
        super.onResume();
        mFlipView.onResume();
    }
 
    @Override
    protected void onPause() {
        super.onPause();
        mFlipView.onPause();
    }
}


创建FlipAdapter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.nekocode.xuedao.adapter;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
import com.aphidmobile.utils.UI;
import com.nekocode.xuedao.R;
 
public class SubscribeIndexAdapter extends BaseAdapter {
  private LayoutInflater inflater;
 
  public SubscribeIndexAdapter(Context context) {
    inflater = LayoutInflater.from(context);
  }
 
  @Override
  public int getCount() {
    return 5;
  }
 
  @Override
  public Object getItem(int position) {
    return position;
  }
 
  @Override
  public long getItemId(int position) {
    return position;
  }
 
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View layout = convertView;
    if (convertView == null) {
      layout = inflater.inflate(R.layout.item_subscribe_index, null);
    }
 
    UI
        .<TextView>findViewById(layout, R.id.textView7)
        .setText("今日热点" + position);
 
    return layout;
  }
}