会游走的TextView
//自定义的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的更多相关文章
- 游走 bzoj 3143
游走(2s 128MB)walk [问题描述] [输入格式] [输出格式] [样例输入] 3 3 2 3 1 2 1 3 [样例输出] 3.333 [样例说明] 题解: 主要算法:贪心:高斯消元: 题 ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- BZOJ 3143 HNOI2013 游走 高斯消元 期望
这道题是我第一次使用高斯消元解决期望类的问题,首发A了,感觉爽爽的.... 不过笔者在做完后发现了一些问题,在原文的后面进行了说明. 中文题目,就不翻大意了,直接给原题: 一个无向连通图,顶点从1编号 ...
- 3143: [Hnoi2013]游走 - BZOJ
Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...
- BZOJ 3143 游走(高斯消元)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3143 题意:一个无向连通图,顶点从1编号到n,边从1编号到m.小Z在该图上进行随机游走, ...
- 【Matlab】随机游走产生图像效果
随机游走类似布朗运动,就是随机的向各个方向走吧.产生的图像实在漂亮,所以还是贴出分享. clear all; close all; clc; n=100000; x= 0; y= 0; pixel=z ...
- bzoj 3143: [Hnoi2013]游走 高斯消元
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1026 Solved: 448[Submit][Status] ...
- 数学(概率):HNOI2013 游走
[题目描述] 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点,获得等于这 ...
- 介绍一个全局最优化的方法:随机游走算法(Random Walk)
1. 关于全局最优化求解 全局最优化是一个非常复杂的问题,目前还没有一个通用的办法可以对任意复杂函数求解全局最优值.上一篇文章讲解了一个求解局部极小值的方法--梯度下降法.这种方法对于求解精度不高 ...
随机推荐
- iOS runtime 初步学习
注: 在Xocde5之后, 使用运行时方法需要进行2步设置1. 在Build Setting中搜索'msg', 设置'Strict Checking' 为 NO2. 使用需要导入头文件 #import ...
- 概率dp专场
专题链接 第一题--poj3744 Scout YYF I 链接 (简单题) 算是递推题 如果直接推的话 会TLE 会发现 在两个长距离陷阱中间 很长一部分都是重复的 我用 a表示到达i-2步的概率 ...
- 远程连接redis
1.在ubuntu上的redis作为服务端,默认是打开的 在redis的配置文件redis.conf中,找到bind localhost注释掉. 注释掉本机,局域网内的所有计算机都能访问. bind ...
- php curl 采集
curl 采集五个步骤: 1.curl_init()初始化curl 2.curl_setopt()设置传输数据和参数 3.curl_exec()执行传输并获取返回数据 4.curl_errono()返 ...
- HDFS 核心原理
HDFS 核心原理 2016-01-11 杜亦舒 HDFS(Hadoop Distribute File System)是一个分布式文件系统文件系统是操作系统提供的磁盘空间管理服务,只需要我们指定把文 ...
- contiki-process结构体
struct process { struct process *next; #if PROCESS_CONF_NO_PROCESS_NAMES #define PROCESS_NAME_STRING ...
- Ruby--学习记录(实时更新)
变量的命名方式决定了变量的种类: 局部变量 以英文字母或者_开头: 全局变量 以$开头: 实例变量 以@开头: 类变量 以@@开头:
- 使用Let`s encrypt 免费的https 证书
在iOS上以前暂时屏蔽https使用http的接口,将在未来被苹果拒绝: 之前传苹果会在2017年1月1日强制要求支持https,但是现在苹果又延迟该计划了 https://developer.app ...
- Java学习之Iterator(迭代器)的一般用法 (转)
迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为"轻量级"对象,因为创建它的代价 ...
- Linux乱码问题解决方案
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...