Android实现翻页特效

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

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

将它更改为:

if(((accumulatedAngle + angleDelta > lastPageIndex*180)
                && (accumulatedAngle + angleDelta <= (lastPageIndex+1) * 180)) ||  
                ((accumulatedAngle + angleDelta < lastPageIndex*180) && 
                    (accumulatedAngle + angleDelta >= (lastPageIndex-1) * 180))){
              accumulatedAngle += angleDelta;
            }

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

void postHideFlipAnimation() {
      if (inFlipAnimation) {
        handler.post(new Runnable() {
          @Override
          public void run() {
            hideFlipAnimation();
          }
        });
      }
    }

修改为:

void postHideFlipAnimation() {
      if (inFlipAnimation) {
        handler.postDelayed(new Runnable() {
          @Override
          public void run() {
            hideFlipAnimation();
          }
        }, 200);
      }
    }

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

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:

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;
  }
}

layout文件并没有什么重要信息所以不放出代码了,效果图:

      

转载于:https://my.oschina.net/gal/blog/200169

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值