82、文字混动效果

第一种方式实现:ListView + Timer  ,简单,但是效果生硬。

<ListView
        android:id="@+id/lv_news"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:fadeScrollbars="true"
        android:layout_alignBottom="@id/txt_home_news_ico"
        android:layout_alignTop="@id/txt_home_news_ico"
        android:layout_marginLeft="10dp"
        android:cacheColorHint="#00000000"
        android:divider="@null"
        android:layout_toRightOf="@id/txt_home_news_ico"
        android:background="@color/transparent"
        android:scrollbars="none" />
 1 private ListView lv_news = null;
 2 private int myIndex = 0;
 3
 4 *******   省略
 5
 6 if (listNewsAll.size() > 1) {
 7             new Timer().schedule(new TimerTask() {
 8                 @Override
 9                 public void run() {
11                     myIndex += 1;
14                     if (myIndex >= listNewsAll.size()) {
15                         myIndex = 0;
16                     }
18                     lv_news.smoothScrollToPositionFromTop (myIndex, 2);
20                 }
21             }, 0, 2000);
22         }
23     }

第二种方式实现:自定义控件 + handler。 有动画效果。

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

/**
 * 垂直翻滚
 */
public class AutoTextView extends TextSwitcher implements
        ViewSwitcher.ViewFactory {

    private float mHeight;
    private Context mContext;
    //mInUp,mOutUp分别构成向下翻页的进出动画
    private Rotate3dAnimation mInUp;
    private Rotate3dAnimation mOutUp;

    //mInDown,mOutDown分别构成向下翻页的进出动画
    private Rotate3dAnimation mInDown;
    private Rotate3dAnimation mOutDown;

    public AutoTextView(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }

    public AutoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
       // TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.auto3d);
        //mHeight = a.getDimension(R.styleable.auto3d_textSize, 36);
        mHeight = 20;
     //   a.recycle();
        mContext = context;
        init();
    }

    private void init() {
        // TODO Auto-generated method stub
        setFactory(this);
        mInUp = createAnim(-90, 0, true, true);
        mOutUp = createAnim(0, 90, false, true);
        mInDown = createAnim(90, 0, true, false);
        mOutDown = createAnim(0, -90, false, false);
        //TextSwitcher主要用于文件切换,比如 从文字A 切换到 文字 B,
        //setInAnimation()后,A将执行inAnimation,
        //setOutAnimation()后,B将执行OutAnimation
        setInAnimation(mInUp);
        setOutAnimation(mOutUp);
    }

    private Rotate3dAnimation createAnim(float start, float end, boolean turnIn, boolean turnUp) {
        final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, turnIn, turnUp);
        //动画持续时间
        rotation.setDuration(300);
        rotation.setFillAfter(false);
        rotation.setInterpolator(new AccelerateInterpolator());
        return rotation;
    }

    public void setData() {

    }

    //这里返回的TextView,就是我们看到的View
    @Override
    public View makeView() {
        // TODO Auto-generated method stub
        TextView t = new TextView(mContext);
        t.setGravity(Gravity.CENTER);
        t.setTextSize(mHeight);
        t.setMaxLines(2);
        t.setPadding(0, 5, 0, 5);
        //设置文字颜色
        t.setTextColor(Color.WHITE);
        return t;
    }

    //定义动作,向下滚动翻页
    public void previous() {
        if (getInAnimation() != mInDown) {
            setInAnimation(mInDown);
        }
        if (getOutAnimation() != mOutDown) {
            setOutAnimation(mOutDown);
        }
    }

    //定义动作,向上滚动翻页
    public void next() {
        if (getInAnimation() != mInUp) {
            setInAnimation(mInUp);
        }
        if (getOutAnimation() != mOutUp) {
            setOutAnimation(mOutUp);
        }
    }

    class Rotate3dAnimation 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 Rotate3dAnimation(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.rotateX(degrees);
            camera.getMatrix(matrix);
            camera.restore();

            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
        }
    }
}
<com.dr.mk.project.txt01.AutoTextView
            android:id="@+id/switcher02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
 1 public class TxtMainActivity extends Activity {
 2
 3     private AutoTextView mTextView02;
 4     final Handler handler = new Handler();
 5     // 自定义信息条数
 6     private static int sCount = 0;
 7     private List<String> str = new ArrayList<String>();
 8
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12
13         setContentView(R.layout.activity_txt_fan_zhuan);
14
15         init();
16     }
17
18     private void init() {
19         //垂直滚动
20         // 初始化数据
21         str.add("信息1");
22         str.add("信息2");
23         str.add("信息3");
24         sCount = str.size();
25         mTextView02 = (AutoTextView) findViewById(R.id.switcher02);
26         mTextView02.setText(str.get(0));
27         //启动计时器
28         handler.postDelayed(runnable, 3000);
29         //handler.removeCallbacks(runnable);// 关闭定时器处理
30     }
31
32     Runnable runnable = new Runnable() {
33         @Override
34         public void run() {
35             // 在此处添加执行的代码
36             mTextView02.next();
37             sCount++;
38             if(sCount>=Integer.MAX_VALUE) {
39                 sCount = str.size();
40             }
41             mTextView02.setText(str.get(sCount % (str.size())));
42             if (str.size()>1) {
43                 handler.postDelayed(this, 2000);// 50是延时时长
44             }
45
46         }
47     };
48
49 }
时间: 2024-10-03 18:44:13

82、文字混动效果的相关文章

[ActionScript 3.0] AS3 拖拽混动效果之一

package { import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.net.URLRequest; /** * @author Frost.Yen * @E-mail [email protected] * @create 2015-11-17 上午10:20:30 * */ [SWF(

CSS文字环绕图片 图文混排效果

CSS实现文字环绕图片效果,也就是大家常见到的文中图效果,比如文字会自动围绕一个方块状的图片广告,这一切都是自动的,不需要另外排版,对此有需要有网页,可借鉴本代码,主要是利用CSS的clear属性实现. <html> <head> <title>CSS文字环绕图片 图文混排效果丨石家庄电缆附件</title> </head> <body> <div style="float:left;"> <di

css实现漂亮的文字滑动折叠效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

CSS3实现文字扫光效果

本篇文章由:http://xinpure.com/css3-text-light-sweep-effect/ CSS3 实现的文字扫光效果,几乎可以和 Flash 相媲美了 效果解析 我们分析一下实现这个效果需要实现的功能: 实现一个扫光背景块,因为光是移动的,所以要加入渐变效果 (例如: 手电筒照射的一小块区域) 将扫光背景块控制到文本上 (即实现文本背景) 实现扫光动画 (扫光块从左往右循环移动) 思路理清了,接下来就是一步一步实现了 背景渐变 -webkit-linear-gradient

css实现鼠标经过导航文字偏位效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ

android TextView里边实现图文混配效果

用TextView实现这样的效果,图片文字混排,文字不同颜色字体,打电话和吊起浏览器等等 代码如下: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTextView = (TextView) this.findViewById(R.id.img_iv); //创建一个

javaScript 利用随机数和定时器完成几个有用的文字小操作 文字的滚动效果,文字的打印效果和文字的颜色闪烁

文字的滚动 <body> <textarea id="textarea"></textarea> <script> str=" 文 字 的 滚 动 效 果"; function roll(){ str=str.substr(1,str.length)+str.substring(0,1); document.getElementById("textarea").style.fontSize=&quo

[Swift通天遁地]八、媒体与动画-(15)使用TextKit实现精美的图文混排效果

本文将演示制作一款更加精美的图文的图文混排效果:将文字紧贴图片边缘的图文混排效果. 往项目中导入一份文本文件. 在左侧的项目导航区,打开视图控制器的代码文件[ViewController.swift] 1 import UIKit 2 3 class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup afte

Android第四期 - 单侧滑动效果

Android的设置或者登陆或者其他的一些主窗体要展示的功能需要用到sliding的效果,下面就叫大家怎么做.直接上代码!! MainActivity部分: package net.ting.sliding; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.