Android实现翻页特效
时间:2022-06-26 10:34:20|栏目:Android代码|点击: 次
本文实例为大家分享了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文件并没有什么重要信息所以不放出代码了,效果图:
上一篇:Android消息推送:手把手教你集成小米推送(附demo)
栏 目:Android代码
下一篇:Android UniversalVideoView实现视频播放器
本文标题:Android实现翻页特效
本文地址:http://www.codeinn.net/misctech/205987.html