Android字体横向滚动条目
来源:互联网 发布:mac系统下载百度网盘 编辑:程序博客网 时间:2024/06/05 21:00
原理自定义textview
源码:http://download.csdn.net/detail/lm_zp/9517135
XMl
<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
AutoText
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 = 0;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 * 2 + 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 / 2 - baseline;}public void startScroll() {isStarting = true;invalidate();}public void stopScroll() {isStarting = false;invalidate();}@Overrideprotected 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();}}MainActivity
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 ";@Overrideprotected 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();}}
1 0
- Android字体横向滚动条目
- 【Android】GridView 横向滚动
- android listview 横向滚动
- Android ListView/recyclerView条目中EditText容易失去焦点的问题和取出横向时上下滚动有阴影
- Android 横向列表GridView 实现横向滚动
- Android 横向列表GridView 实现横向滚动
- Android 横向列表GridView 实现横向滚动
- android屏幕横向滚动,android图片横向滚动
- Android TextView横向滚动问题
- android 横向,竖向滚动Gridveiw
- android 图片横向滚动效果
- android 图片横向滚动效果
- android 图片横向滚动效果
- Android横向ListView可以滚动
- android横向循环自动滚动
- Android 自定义横向滚动条
- Android GridView实现横向滚动
- Android GridView实现横向滚动
- matlab制作及生成avi,gif动画
- 实用知识:摇一摇功能的方法使用(真简单??)
- C++ 普通内联函数 及内联函数与宏的区别
- java网络编程
- delphi概念性学习(二)
- Android字体横向滚动条目
- 文本编辑器快捷键
- JAVA之旅(二)——if,switch,for,while,do while,语句嵌套,流程控制break , continue ,函数,重载的示例总结
- 在结构体最后定义一个长度为0的字符数组(技巧)
- 百度之星(2015)
- 实用知识:计步器的方法使用
- javascript -function- 函数从基本到闭包详解
- 用ransac求解仿射变换矩阵
- 从源码理解Hashtable.java