仿天猫热点,淘宝头条向上自动滚动的textview
2016-05-21 11:33
316 查看
先贴出效果图:
实现思路:
自定义TextSwitcher,并在里边添加两个动画分别是向上出的和向上进.
使用:
scrollTextView.next();//跳向下一个 在这句话之后必须紧跟着scrollTextView.setText("这是第" + count + "次滚动");来设置内容
下面是源码:
首先先给scrollTextView的代码:
布局文件
点击这里下载源码
实现思路:
自定义TextSwitcher,并在里边添加两个动画分别是向上出的和向上进.
使用:
scrollTextView.next();//跳向下一个 在这句话之后必须紧跟着scrollTextView.setText("这是第" + count + "次滚动");来设置内容
下面是源码:
首先先给scrollTextView的代码:
<span style="font-size:14px;">public class ScrollTextView extends TextSwitcher implements ViewSwitcher.ViewFactory { private Context context; // inAnimation,outAnimation分别构成翻页的进出动画 private ScrollAnimation inAnimation; private ScrollAnimation outAnimation; public ScrollTextView(Context context) { this(context, null); } public ScrollTextView(Context context, AttributeSet attrs) { super(context, attrs); //将获取自定义textSize的值,如果没有,则使用默认的值,15。 TypedArray typedArray = context.obtainStyledAttributes(attrs, new int[] { 0x7f010000 }); typedArray.recycle(); this.context = context; init(); } private void init() { setFactory(this);//创建两个view inAnimation = createAnim(-90, 0, true, true); outAnimation = createAnim(0, 90, false, true); setInAnimation(inAnimation); setOutAnimation(outAnimation); } private ScrollAnimation createAnim(float start, float end, boolean turnIn, boolean turnUp) { final ScrollAnimation rotation = new ScrollAnimation(start, end, turnIn, turnUp); rotation.setDuration(800); rotation.setFillAfter(false); rotation.setInterpolator(new AccelerateInterpolator()); return rotation; } /** * 这里返回的TextView,就是我们看到的View */ @Override public View makeView() { TextView textView = new TextView(context); textView.setGravity(Gravity.LEFT); textView.setPadding(0, 6, 0, 6); textView.setTextSize(12); textView.setGravity(Gravity.CENTER_VERTICAL);// gravity center_vertical textView.setText(""); textView.setSingleLine(true); textView.setEllipsize(TruncateAt.END); return textView; } public void next() { if (getInAnimation() != inAnimation) { setInAnimation(inAnimation); } if (getOutAnimation() != outAnimation) { setOutAnimation(outAnimation); } } class ScrollAnimation extends Animation { private final float mFromDegrees; private final float mToDegrees; private float mCenterX; private float mCenterY; private final boolean mTurnIn; private final boolean mTurnUp; private Camera mCamera; public ScrollAnimation(float fromDegrees, float toDegrees, boolean turnIn, boolean turnUp) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mTurnIn = turnIn; mTurnUp = turnUp; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); mCenterY = getHeight() / 2; mCenterX = getWidth() / 2; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float fromDegrees = mFromDegrees; float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final int derection = mTurnUp ? 1 : -1; final Matrix matrix = t.getMatrix(); camera.save(); if (mTurnIn) { camera.translate(0.0f, derection * mCenterY * (interpolatedTime - 1.0f), 0.0f); } else { camera.translate(0.0f, derection * mCenterY * (interpolatedTime), 0.0f); } camera.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } } }</span>接下来就是使用了 使用起来非常简单
布局文件
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:auto3d="http://schemas.android.com/apk/res/com.example.scrolltextview" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.scrolltextview.ScrollTextView android:id="@+id/switcher02" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="#ff669900" android:paddingBottom="5dp" android:paddingTop="5dp" auto3d:textSize="30sp" > </com.example.scrolltextview.ScrollTextView> </RelativeLayout></span>MainActivity中,现在oncreat方法中先给一个初始值 然后在子线程中给个1秒发一个空消息 在主线程中收到消息后跳到下一个并设置文字
<span style="font-size:14px;">public class MainActivity extends Activity { private ScrollTextView scrollTextView; private static int count = 0; private boolean isContinue; private Thread myThread; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: scrollTextView.next(); count++; scrollTextView.setText("这是第" + count + "次滚动"); break; default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { scrollTextView = (ScrollTextView) findViewById(R.id.switcher02); scrollTextView.setText("初始值");// 设置初始值 isContinue = true; myThread = new Thread() { public void run() { while (isContinue) { SystemClock.sleep(2000); handler.sendEmptyMessage(0);// 每隔一秒 发一个空消息 滚动一次 } }; }; myThread.start(); } /** * 在销毁的时候干掉线程 */ @Override protected void onDestroy() { super.onDestroy(); isContinue = false; if (myThread != null && myThread.isAlive()) { myThread.interrupt(); } } }</span>
点击这里下载源码
相关文章推荐
- (版本定制)第8课:Spark Streaming源码解读之RDD生成生命周期彻底研究和思考
- DB扩展名的数据库文件怎么打开:两种db数据库的打开方式
- ISCC 2016 WEB WP
- 第十三周項目-阅读项目-4
- C++11进阶知识列表
- 负载均衡,示意图。
- HDU 5247 找连续数
- 用python播放音乐--pygame package
- JQuery、JSON、Ajax在Servlet中的应用
- Redis简介,安装和配置,停止,卸载(图解方式)
- git 分支新建与推送
- HTML 5 标准定了,哪家欢乐哪家愁
- CSS之边框属性
- 用于求两个向量之间的距离
- json多层数组的反序列化
- 软件设计原则--开放封闭原则
- 网站如何添加访客统计代码
- Unity陀螺仪应用
- 树、森林与二叉树的相互转换
- SDR 研究