知识共享-android TextView各种效果(雷惊风)

知识共享-android TextView各种效果(雷惊风)

2023年7月14日发(作者:)

常用控件应用之文本框(TextView)特效

ew之跑马灯效果

android:ellipsize属性使用之跑马灯效果

android:ellipsize

设置当文字过长时,该控件该如何显示。有如下值设置:

"start"—–省略号显示在开头

"end"——省略号显示在结尾

"middle"—-省略号显示在中间

"marquee" ——以跑马灯的方式显示(动画横向向左移动)

布局文件中给TextView加入如下属性即可:

android:ellipsize="marquee"

android:marqueeRepeatLimit="marquee_forever"

android:focusable="true"

android:focusableInTouchMode="true"

布局文件中设置如下(完整代码稍后给出)

android:id="@+id/marquee_effect"

android:layout_width="100dip"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:ellipsize="marquee"

android:focusable="true"

android:focusableInTouchMode="true"

android:marqueeRepeatLimit="marquee_forever"

android:singleLine="true"

android:text="@string/marquee_effect" />

如果一个页面想实现多个TextView同时跑马灯效果解决方案:给要跑动的textview加上如下代码就行了

ected(true);

ew之阴影效果(可在布局文件里加入如下属性进行设置也可通过程序设置)

android:shadowDx——设置阴影横向坐标开始位置(相对于文本内容)

android:shadowDy——设置阴影纵向坐标开始位置(相对于文本内容)

android:shadowRadius——设置阴影的半径

android:shadowColor——指定文本阴影的颜色

//关键代码(完整代码稍后给出)

dowLayer(2.5f, 15, -10, 0xff00ff00);

标签设置样式效果

补充:

oLinkMask(_URLS);// 当文本内容中包含超链接格式的文本时,自动转换成超链接样式,点击会自动跳转到指定的网页

oLinkMask(_NUMBERS);//自动转手机号码点击它可进入系统拨号界面

oLinkMask(_ADDRESSES);//自动转邮件地址点击它可发送邮件(要提前设置好自己的电子邮件)

oLinkMask(_ADDRESSES);//自动转街道地址点击它可查看位置(前提已安装了google地图)

oLinkMask();//包括上面4种情况

关键代码(完整代码稍后给出):

TextView tv = (TextView) findViewById(ml_effect);

StringBuffer sb = new StringBuffer();

("

Html标签方式:

");

("

href='/bravestarrhu/'>");

(getString(ml_effect));

("

");

t(ml(ng()));

ementMethod(tance());// 这句很重要,使超链接起作用

ew之动画效果(rotate旋转、alpha透明度、scale缩放、translate移动)

实现动画需要在res/anim目录下新建对应的xml文件(稍后给出)

关键代码(完整代码稍后给出):

TextView tv = null;

// TextView旋转 动画效果

tv = (TextView) findViewById();

Animation mAnimationRight = imation(

, );

mation(mAnimationRight);

// TextView透明度动画效果

tv = (TextView) findViewById();

mAnimationRight = imation(

, );

mation(mAnimationRight);

// TextView缩放动画效果

tv = (TextView) findViewById();

mAnimationRight = imation(

, );

mation(mAnimationRight);

// TextView移动动画效果

tv = (TextView) findViewById(ate);

mAnimationRight = imation(

, ate);

mation(mAnimationRight);

ew之霓虹灯效果

采用timer+TimerTask+Handler实现

主要用到SpannableStringBuilder对象

关键代码(完整代码稍后给出):

// 霓虹灯效果(此段代码会使"光"变红色)

String wholeContent = "欢迎光临";

SpannableStringBuilder spannable = new SpannableStringBuilder(

wholeContent);

n(new ForegroundColorSpan(), 2,

3, _EXCLUSIVE_INCLUSIVE); // 设置指定位置文字的颜色(索引0开始)

ew之包含图片的效果

实现步骤(1.构建ImageGetter;2.直接使用append进行追加)

关键代码(完整代码稍后给出):

TextView tv = (TextView) findViewById(_effect);

t(_effect);

// 通过HTML标记获得res目录下指定的图片

ImageGetter imageGetter = new ImageGetter() {

@Override

public Drawable getDrawable(String source) {

int id = nt(source);

// 根据id从资源文件中获取图片对象

Drawable d = getResources().getDrawable(id);

nds(0, 0, rinsicWidth(), rinsicHeight());

return d;

}

};

(ml("",

imageGetter, null));

完整代码:

1>清单文件

package="eweffect"

android:versionCode="1"

android:versionName="1.0" >

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

android:name=".TextViewEffectActivity"

android:label="@string/app_name" >

2>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/marquee_effect"

android:layout_width="100dip"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:ellipsize="marquee"

android:focusable="true"

android:focusableInTouchMode="true"

android:marqueeRepeatLimit="marquee_forever"

android:singleLine="true"

android:text="@string/marquee_effect" />

android:id="@+id/shadow_effect"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:singleLine="true" />

android:id="@+id/fromhtml_effect"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:singleLine="true" />

android:id="@+id/alpha"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/alpha_animation_effect"

/>

android:id="@+id/rotate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/rotate_animation_effect"/>

android:id="@+id/scale"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/scale_animation_effect"/>

android:id="@+id/translate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/translate_animation_effect"/>

android:id="@+id/neonlights_effect"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/image_effect"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

3>

TextView效果集锦

跑马灯效果

阴影效果

fromhtml设置效果

文本透明度动画效果

文本旋转动画效果

文本缩放动画效果

文本移动动画效果

霓虹灯效果

包含图片的TextView效果

4>

android:duration="500"

android:fromAlpha="1.0"

android:toAlpha="0.0"

android:repeatCount="10"/>

5>

xmlns:android="/apk/res/android"

android:duration="500"

android:fromDegrees="0"

android:interpolator="@android:anim/linear_interpolator"

android:pivotX="50%"

android:pivotY="50%"

android:repeatCount="10"

android:toDegrees="-90" />

6>

android:interpolator= "@android:anim/decelerate_interpolator"

android:fromXScale="0.0"

android:toXScale="1.5"

android:fromYScale="0.0"

android:toYScale="1.5"

android:pivotX="50%"

android:pivotY="50%"

android:startOffset="0"

android:duration="5000"

android:repeatCount="3"

android:repeatMode="reverse"

/>

7>

android:fromXDelta="320"

android:toXDelta="0"

android:fromYDelta="480"

android:toYDelta="0"

android:duration="5000"

android:repeatCount="10" />

8>

package eweffect;

import ist;

import p;

import ;

import ;

import ;

import ask;

import ty;

import ;

import le;

import ;

import r;

import e;

import ;

import etter;

import vementMethod;

import ion;

import ionUtils;

import ew;

import bleStringBuilderUtils;

/**

* @Author BraveStarr

* @QQ 1733259520

* @Blog /bravestarrhu/,/

* wanghubravestarr

*

*/

public class TextViewEffectActivity extends Activity {

private static final int[] colors = new int[] { , ,

, , };

private int currColor = 0;// 当前第一个文字的颜色

@Override

public void onCreate(Bundle savedInstanceState) {

te(savedInstanceState);

setContentView();

TextView tv = null;

// 跑马灯效果文本框

tv = (TextView) findViewById(e_effect);

tSize(28);

tColor(0xffd0eeee);

ected(true);// 要实现界面上有多个跑马灯时需要加入这句

// 阴影效果

tv = (TextView) findViewById(_effect);

tSize(28);

tColor(0xffd0eeee);

t(_effect);

dowLayer(2.5f, 15, -10, 0xff00ff00);

// HTML标签实现效果

tv = (TextView) findViewById(ml_effect);

// 当文本内容中包含超链接格式的文本时,自动转换成超链接样式,点击会自动跳转到指定的网页,文本框还能实现自动转手机号码/邮件/地图

// _NUMBERS为自动转手机号码点击它可进入系统拨号界面

// _ADDRESSES为自动转邮件地址点击它可发送邮件(要提前设置好自己的电子邮件)

// _ADDRESSES为自动转街道地址点击它可查看位置(前提已安装了google地图)

// 包括WEB_URLS、EMAIL_ADDRESSES、PHONE_NUMBERS和MAP_ADDRESSES

// oLinkMask(_URLS);

StringBuffer sb = new StringBuffer();

("

Html标签方式:

");

("

href='/bravestarrhu/'>");

(getString(ml_effect));

("

");

t(ml(ng()));

ementMethod(tance());// 这句很重要,使超链接起作用

// TextView旋转 动画效果

tv = (TextView) findViewById();

Animation mAnimationRight = imation(

, );

mation(mAnimationRight);

// TextView透明度动画效果

tv = (TextView) findViewById();

mAnimationRight = imation(

, );

mation(mAnimationRight);

// TextView缩放动画效果

tv = (TextView) findViewById();

mAnimationRight = imation(

, );

mation(mAnimationRight);

// TextView移动动画效果

tv = (TextView) findViewById(ate);

mAnimationRight = imation(

, ate);

mation(mAnimationRight);

// 霓虹灯效果,延时1秒转第一次,从此以后每隔1秒又开始执行执行

le(task_neon, 1000, 1000);

// 包含图片的效果(1.构建ImageGetter;2.直接使用append进行追加)

tv = (TextView) findViewById(_effect);

t(_effect);

// 通过HTML标记获得res目录下指定的图片

ImageGetter imageGetter = new ImageGetter() {

@Override

public Drawable getDrawable(String source) {

int id = nt(source);

// 根据id从资源文件中获取图片对象

Drawable d = getResources().getDrawable(id);

nds(0, 0, rinsicWidth(), rinsicHeight());

return d;

}

};

(ml("",

imageGetter, null));

}

Timer timer = new Timer();

TimerTask task_neon = new TimerTask() {

public void run() {

Message message = new Message();

= 1;

ssage(message);

}

};

final Handler handler = new Handler() {

public void handleMessage(Message msg) {

switch () {

case 1:

// 霓虹灯效果

TextView tv = (TextView) findViewById(ghts_effect);

String neontext = getString(ghts_effect);

int len = ();

int colorsize = ;

List> data = new ArrayList

Object>>();

for (int i = 0; i < len; i++) {

int index = currColor + i;// 颜色索引

if (index >= colorsize)

index = index % colorsize;

Map target = new HashMap();

("content", ing(i, i + 1));

("color", colors[index]);

(target);

}

currColor++;

if (currColor == colorsize)

currColor = 0;

t(SpannableStringBuilderUtils

.highlight(neontext, data));

break;

}

Message(msg);

}

};

protected void onDestroy() {

if (timer != null) {

();

timer = null;

}

roy();

}

}

9>

package ;

import ;

import ;

import r;

import n;

import ble;

import bleStringBuilder;

import oundColorSpan;

/**

* @Author BraveStarr

* @QQ 1733259520

* @Blog

/bravestarrhu/,/wanghubravestarr

*

* SpannableStringBuilder助手类

*/

public class SpannableStringBuilderUtils {

/**

* 指定内容高亮显示(此方法缺点:变色目标文字有多处匹配时效果不好)

* @param wholeContent 文本内容

* @param data Map数组包含"content"和"color"字段

* @return

*

* 如何使用

*

* String content = "文本框局部改变颜色"

* List> data = new ArrayList>();

Map target = null;

target = new HashMap();

("content", "改变");//指定要改变颜色的内容

("color", );//指定颜色

(target);

t(highlight(content, data));

*/

public static SpannableStringBuilder highlight(String wholeContent,

List> data) {

SpannableStringBuilder spannable = new SpannableStringBuilder(

wholeContent);

for (Map targetdata : data) {

Pattern p = e(("content").toString());

Matcher m = r(wholeContent);

while (()) {

n(

new

ForegroundColorSpan(f(("color").toString())),

(), (), _EXCLUSIVE_EXCLUSIVE);

}

}

return spannable;

}

/**

*

* @param wholeContent 文本内容

* @param data Map数组包含"start"、"end"和"color"字段

* @return

* 如何使用

*

* String content = "文本框局部改变颜色"

* List> data = new ArrayList>();

Map target = new HashMap();

("start", "5");//指定要改变颜色的起始位置

("end", "7");//指定要改变颜色的结束位置

("color", );//指定颜色

(target);

t(subHighlight(content, data));

*/

public static SpannableStringBuilder subHighlight(String wholeContent,

List> data) {

SpannableStringBuilder spannable = new SpannableStringBuilder(

wholeContent);

for (Map targetdata : data) {

int color = f(("color").toString());

int start = f(("start").toString());

int end = f(("end").toString());

n(new ForegroundColorSpan(color), start,

end, _EXCLUSIVE_INCLUSIVE); // 设置指定位置文字的颜色(索引0开始)

}

return spannable;

}

}

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689327968a230504.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信