首页 > 代码库 > 基于Android小说阅读器滑动效果的一种实现
基于Android小说阅读器滑动效果的一种实现
看过小说都知道小说阅读器翻页有好多种效果,比如仿真翻页,滑动翻页,等等。由于某种原因,突然想写一个简单点的滑动翻页效果。在这里写出来也没有什么意图,希望大家可以根据这个效果举一反三,写出其他的效果。图就不上了。
下面是代码:大家理解onTouch事件即可
package com.example.testscroll.view; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.widget.Scroller; public class FlipperLayout extends ViewGroup { private Scroller mScroller; private VelocityTracker mVelocityTracker; private int mVelocityValue = http://www.mamicode.com/0;>Activity测试文件:
package com.example.testscroll; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.content.res.AssetManager; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import com.example.testscroll.view.FlipperLayout; import com.example.testscroll.view.FlipperLayout.TouchListener; import com.example.testscrollactivity.R; public class MainActivity extends Activity implements OnClickListener, TouchListener { private String text = ""; private int textLenght = 0; private static final int COUNT = 400; private int currentTopEndIndex = 0; private int currentShowEndIndex = 0; private int currentBottomEndIndex = 0; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { FlipperLayout rootLayout = (FlipperLayout) findViewById(R.id.container); View recoverView = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_new, null); View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_new, null); View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_new, null); rootLayout.initFlipperViews(MainActivity.this, view2, view1, recoverView); textLenght = text.length(); System.out.println("----textLenght----->" + textLenght); TextView textView = (TextView) view1.findViewById(R.id.textview); if (textLenght > COUNT) { textView.setText(text.subSequence(0, COUNT)); textView = (TextView) view2.findViewById(R.id.textview); if (textLenght > (COUNT << 1)) { textView.setText(text.subSequence(COUNT, COUNT * 2)); currentShowEndIndex = COUNT; currentBottomEndIndex = COUNT << 1; } else { textView.setText(text.subSequence(COUNT, textLenght)); currentShowEndIndex = textLenght; currentBottomEndIndex = textLenght; } } else { textView.setText(text.subSequence(0, textLenght)); currentShowEndIndex = textLenght; currentBottomEndIndex = textLenght; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new ReadingThread().start(); } @Override public void onClick(View v) { } @Override public View createView(final int direction) { String txt = ""; if (direction == TouchListener.MOVE_TO_LEFT) { currentTopEndIndex = currentShowEndIndex; final int nextIndex = currentBottomEndIndex + COUNT; currentShowEndIndex = currentBottomEndIndex; if (textLenght > nextIndex) { txt = text.substring(currentBottomEndIndex, nextIndex); currentBottomEndIndex = nextIndex; } else { txt = text.substring(currentBottomEndIndex, textLenght); currentBottomEndIndex = textLenght; } } else { currentBottomEndIndex = currentShowEndIndex; currentShowEndIndex = currentTopEndIndex; currentTopEndIndex = currentTopEndIndex - COUNT; txt = text.substring(currentTopEndIndex - COUNT, currentTopEndIndex); } View view = LayoutInflater.from(this).inflate(R.layout.view_new, null); TextView textView = (TextView) view.findViewById(R.id.textview); textView.setText(txt); System.out.println("-top->" + currentTopEndIndex + "-show->" + currentShowEndIndex + "--bottom-->" + currentBottomEndIndex); return view; } @Override public boolean whetherHasPreviousPage() { return currentShowEndIndex > COUNT; } @Override public boolean whetherHasNextPage() { return currentShowEndIndex < textLenght; } @Override public boolean currentIsFirstPage() { boolean should = currentTopEndIndex > COUNT; if (!should) { currentBottomEndIndex = currentShowEndIndex; currentShowEndIndex = currentTopEndIndex; currentTopEndIndex = currentTopEndIndex - COUNT; } return should; } @Override public boolean currentIsLastPage() { boolean should = currentBottomEndIndex < textLenght; if (!should) { currentTopEndIndex = currentShowEndIndex; final int nextIndex = currentBottomEndIndex + COUNT; currentShowEndIndex = currentBottomEndIndex; if (textLenght > nextIndex) { currentBottomEndIndex = nextIndex; } else { currentBottomEndIndex = textLenght; } } return should; } private class ReadingThread extends Thread { public void run() { AssetManager am = getAssets(); InputStream response; try { response = am.open("text.txt"); if (response != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = -1; while ((i = response.read()) != -1) { baos.write(i); } text = new String(baos.toByteArray(), "UTF-8"); baos.close(); response.close(); handler.sendEmptyMessage(0); } } catch (IOException e) { e.printStackTrace(); } } } }
xml布局文件:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/textview" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1.0" android:background="#666666" android:gravity="center" android:text="新建的View" android:textColor="@android:color/white" android:textSize="16sp" android:visibility="visible" /> <View android:layout_width="5dp" android:layout_height="match_parent" android:background="#FFFF00" android:gravity="center" android:textSize="25sp" android:visibility="visible" /> </LinearLayout>
activity布局文件:<com.example.testscroll.view.FlipperLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > </com.example.testscroll.view.FlipperLayout>
备注:上面为什么加一个速率计算器呢,其实只是为了识别这个动作是不是快速滑动的动作,就算滑动的距离不到屏幕的1/3,但是只要速率满足都可以判定改滑动是一个翻页的动作。注意哦:这只是其中一个滑动的效果而已啊,不包括小说分章节的逻辑哦。虽然有些粗糙,但是还是有可以值得学习的地方,大家如果还有什么好的解决方案,可以一起讨论。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。