網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android實現(xiàn)翻頁特效的具體代碼,供大家參考,具體內(nèi)容如下
android-flip是一個能夠輕松幫你實現(xiàn)水平以及豎直翻頁特效的庫,但是在判斷翻頁的時候有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;
? ? ? ? ? ? }
而在翻頁的時候會有閃爍現(xiàn)象產(chǎn)生,為了減輕現(xiàn)象的發(fā)生,我們需要修改另外一個地方,在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();
?? ?}
}
創(chuàng)建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://blog.csdn.net/nekocode/article/details/18413779
相關(guān)推薦
- 2022-12-09 Opencv中的cv2.calcHist()函數(shù)的作用及返回值說明_python
- 2023-11-21 高階函數(shù)HoF:用filter()方法編寫一個素數(shù)生成函數(shù)primes()
- 2022-06-20 go語言實現(xiàn)屏幕截圖的示例代碼_Golang
- 2022-07-01 淺談C語言中的sizeof()和strlen()的區(qū)別_C 語言
- 2022-12-30 解決React報錯useNavigate()?may?be?used?only?in?context
- 2024-03-05 git的使用
- 2022-09-14 C#實現(xiàn)自由組合本地緩存、分布式緩存和數(shù)據(jù)查詢_C#教程
- 2022-10-06 react-router-dom入門使用教程(路由的模糊匹配與嚴(yán)格匹配)_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支