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文件并没有什么重要信息所以不放出代码了,效果图:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。