//自定义的TextView

package com.bwie.androidtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView; public class AutoText extends TextView { private int width, height;
private Paint paintText;
private float posx, posy;
private float speed = 0.0f;
private String text = "hello haha";
private float textWidth = ;
private float moveDistance = 0.0f;
private boolean isStarting = false; public AutoText(Context context) {
super(context); } public AutoText(Context context, AttributeSet attrs) {
super(context, attrs); } private void initView() {
paintText = new Paint();
//字体大小
paintText.setTextSize(50.0f);
//字体颜色
paintText.setColor(Color.BLACK);
paintText.setTypeface(Typeface.DEFAULT_BOLD);
paintText.setAntiAlias(true);
text = getText().toString();
textWidth = paintText.measureText(text);
Log.e("msg", "textWidth= " + textWidth);
this.speed = textWidth;
moveDistance = textWidth * + width;
} public void initDisplayMetrics(WindowManager windowManager) {
/* 取得屏幕分辨率大小 */
DisplayMetrics dm = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(dm);
this.width = dm.widthPixels;
this.height = dm.heightPixels; initView();
this.posx = width + textWidth;
FontMetrics fm = paintText.getFontMetrics();
float baseline = fm.descent - fm.ascent;
//字体显示高度
this.posy = height / - baseline;
} public void startScroll() {
isStarting = true;
//重新绘制
invalidate();
} public void stopScroll() {
isStarting = false;
invalidate();
} @Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
canvas.drawText(text, posx - speed, posy, paintText);
if (!isStarting) {
return;
}
speed += 2.0f;
if (speed > moveDistance)
speed = textWidth;
invalidate();
} }

//主类

package com.bwie.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils.TruncateAt;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity {
String str11 = "促进青年教师全面发展,\n引导广大高校青年教师为实现中华民族伟大复兴的中国梦贡献力" + "\n"
+ "促进青年教师全面发展,\n引导广大高校青年教师为实现中华民族伟大复兴的中国梦贡献力" + "\n"
+ " djsdnh kshdfjks \n\r\t "; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scroll3();
} void scroll3() {
AutoText auto = (AutoText) findViewById(R.id.autoTxt);
auto.setText(str11);
//传递屏幕宽度
auto.initDisplayMetrics(getWindowManager());
//开始
auto.startScroll();
} }

//布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <com.bwie.androidtest.AutoText
android:id="@+id/autoTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textColor="#00ff00"
android:textSize="35sp" /> </RelativeLayout>

会游走的TextView的更多相关文章

  1. 游走 bzoj 3143

    游走(2s 128MB)walk [问题描述] [输入格式] [输出格式] [样例输入] 3 3 2 3 1 2 1 3 [样例输出] 3.333 [样例说明] 题解: 主要算法:贪心:高斯消元: 题 ...

  2. 【BZOJ-3143】游走 高斯消元 + 概率期望

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2264  Solved: 987[Submit][Status] ...

  3. BZOJ 3143 HNOI2013 游走 高斯消元 期望

    这道题是我第一次使用高斯消元解决期望类的问题,首发A了,感觉爽爽的.... 不过笔者在做完后发现了一些问题,在原文的后面进行了说明. 中文题目,就不翻大意了,直接给原题: 一个无向连通图,顶点从1编号 ...

  4. 3143: [Hnoi2013]游走 - BZOJ

    Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...

  5. BZOJ 3143 游走(高斯消元)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3143 题意:一个无向连通图,顶点从1编号到n,边从1编号到m.小Z在该图上进行随机游走, ...

  6. 【Matlab】随机游走产生图像效果

    随机游走类似布朗运动,就是随机的向各个方向走吧.产生的图像实在漂亮,所以还是贴出分享. clear all; close all; clc; n=100000; x= 0; y= 0; pixel=z ...

  7. bzoj 3143: [Hnoi2013]游走 高斯消元

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1026  Solved: 448[Submit][Status] ...

  8. 数学(概率):HNOI2013 游走

    [题目描述] 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这 ...

  9. 介绍一个全局最优化的方法:随机游走算法(Random Walk)

    1. 关于全局最优化求解   全局最优化是一个非常复杂的问题,目前还没有一个通用的办法可以对任意复杂函数求解全局最优值.上一篇文章讲解了一个求解局部极小值的方法--梯度下降法.这种方法对于求解精度不高 ...

随机推荐

  1. Linux C相关基础

    系统求助 man 函数名 man 2 函数名    -    表示函数是系统调用函数 man 3 函数名    -    表示函数是C的库函数     eg:man fread     man 2 w ...

  2. asp.net mvc4 过滤器的简单应用:登录验证

    直接上代码,不要说话. ASP.NET MVC4过滤器的简单应用:验证登录 [AcceptVerbs(HttpVerbs.Post)] public ActionResult login(FormCo ...

  3. centos 7 编译zabbix 3.2.2

    已有LNMP环境. 1.安装zabbix需要的依赖包,红色部门的包官方yum源没有,需要自己下载 yum install net-snmp fping iksemel net-snmp-devel   ...

  4. js 数组赋值问题 :值传递还是引用?

    转载于知乎var a = [1,2,3]; var b = a; a = [4,5,6]; alert(b); //[1,2,3] 面试时被问到这样一个问题,竟然从来没试过... 当时直接的理解,数组 ...

  5. Android Studio安装和配置(个人研究,有错请指导)

    安装Android Studio的原因:公司有app开发者,然而公司没有测试,只好互相测试,本人并没有接触过app开发,纯小白: 自己试着安装了一下Android Studio来这里记录并分享遇到的问 ...

  6. Attendance

    1.打怪 福利好美味(色.... 努力是为了,遇到你时,可以不用因为种种而错过. 一公司的老板对一位职员说:“我出10万买你的老婆,你卖吗?” 职员微笑着说:“那我出15万买你老婆,你同意吗?” 老板 ...

  7. win10 开机黑屏只显示鼠标

    最靓,win10开机黑屏,网上试了很多办法没有解决,花了3个小时终于解决了,现在介绍一下方法. 首先,在黑屏模式下进入任务管理器,文件=>运行新任务=>Explorer.exe 此时桌面已 ...

  8. Response.Write("<script>alert('弹出对话框!')</script>") 后跟Response.Redirect("page.aspx");不能弹出对话框,直接跳转页面了 如何解?

    Response.Write和Response.Redirect一起用的时候就会这样,write脚本和redirect脚本不能同时使用,这样不会执行脚本,最好使用ClientScript 改进方法: ...

  9. Python’s SQLAlchemy vs Other ORMs[转发 6]SQLAlchemy

    SQLAlchemy SQLAlchemy is an open source SQL toolkit and ORM for the Python programming language rele ...

  10. ThinkPHP 3.2.3(二)配置

    一.配置格式 1.PHP数组定义 默认所有配置文件的定义格式均采用返回PHP数组的方式,配置参数不区分大小写. 如果使用二维数组来配置更多的信息,则二级参数配置区分大小写.格式为: //项目配置文件r ...