Textview 文本旋转,倾斜
有时候Android自带的控件无法满足我们的某些要求,这时就需要我们自定义控件来实现这些功能。比如需要一个TextView里的字倾斜一定的角度,就需要自定义TextView。
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.android.textviewrotatedemo;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.Gravity;import android.widget.TextView;/** * Created by zhenxixianzai on 2015/5/5. */public class RotateTextView extends TextView { private static final int DEFAULT_DEGREES = 0; private int mDegrees; public RotateTextView(Context context) { super(context, null); } public RotateTextView(Context context, AttributeSet attrs) { super(context, attrs, android.R.attr.textViewStyle); this.setGravity(Gravity.CENTER); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotateTextView); mDegrees = a.getDimensionPixelSize(R.styleable.RotateTextView_degree, DEFAULT_DEGREES); a.recycle(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); } @Override protected void onDraw(Canvas canvas) { canvas.save(); canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop()); canvas.rotate(mDegrees, this.getWidth() / 2f, this.getHeight() / 2f); super.onDraw(canvas); canvas.restore(); } public void setDegrees(int degrees) { mDegrees = degrees; }} |
styles 如下:
|
1
2
3
|
<declare-styleable name="RotateTextView"> </attr></declare-styleable> |
用法:
|
1
2
3
4
5
6
7
8
9
10
|
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <com.android.textviewrotatedemo.rotatetextview android:autolink="all" android:gravity="center" android:id="@+id/ceshi" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="http://blog.csdn.net/u014649598"> <com.android.textviewrotatedemo.rotatetextview android:autolink="all" android:gravity="center" android:id="@+id/ceshi1" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="测试测试测试"> <com.android.textviewrotatedemo.rotatetextview android:autolink="all" android:gravity="center" android:id="@+id/ceshi2" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="测试测试测试"> <com.android.textviewrotatedemo.rotatetextview android:autolink="all" android:gravity="center" android:id="@+id/ceshi3" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="测试测试测试4"> <com.android.textviewrotatedemo.rotatetextview android:autolink="all" android:gravity="center" android:id="@+id/ceshi4" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="测试测试测试"></com.android.textviewrotatedemo.rotatetextview></com.android.textviewrotatedemo.rotatetextview></com.android.textviewrotatedemo.rotatetextview></com.android.textviewrotatedemo.rotatetextview></com.android.textviewrotatedemo.rotatetextview></linearlayout> |
Java 部分:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.android.textviewrotatedemo;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity { private RotateTextView mTextView; private RotateTextView mTextView1; private RotateTextView mTextView2; private RotateTextView mTextView3; private RotateTextView mTextView4; private RotateTextView mTextView5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (RotateTextView) findViewById(R.id.ceshi); mTextView.setDegrees(10); mTextView1 = (RotateTextView) findViewById(R.id.ceshi1); mTextView1.setDegrees(350); mTextView2 = (RotateTextView) findViewById(R.id.ceshi2); mTextView2.setDegrees(180); mTextView3 = (RotateTextView) findViewById(R.id.ceshi3); mTextView3.setDegrees(90); mTextView4 = (RotateTextView) findViewById(R.id.ceshi4); mTextView4.setDegrees(290); }} |
附上效果图:
项目地址:
https://coding.net/u/leigo/p/RotateTextView/git
Textview 文本旋转,倾斜的更多相关文章
- Java 设置PDF中的文本旋转、倾斜
本文介绍通过Java程序在PDF文档中设置文本旋转.倾斜的方法.设置文本倾斜时,通过定义方法TransformText(page);并设置page.getCanvas().skewTransform( ...
- WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示
原文:WPF/Silverlight中图形的平移,缩放,旋转,倾斜变换演示 为方便描述, 这里仅以正方形来做演示, 其他图形从略. 运行时效果图:XAML代码:// Transform.XAML< ...
- Delphi实现图像文本旋转特效完整代码
Delphi实现图像文本旋转特效完整代码,本程序利用的控件主要是Panel 控件.Image 控件.Edit 控件.Label 控件和Button 控件.本程序的关键是利用Delphi 的bmp_ro ...
- Android TextView文本处理库推荐
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/115 Android TextView文本处理库推荐 现在 ...
- 浅谈ClickableSpan , 实现TextView文本某一部分文字的点击响应
超文本:http://www.baidu.com 这么一个效果:一行文本当中 前面显示黑色颜色的“超文本:”,后面显示红色颜色的“http://www.baidu.com” 并且要求红色字体的部分可以 ...
- Android学习笔记-TextView(文本框)(二)
2.4 使用autoLink属性识别链接类型 当文字中出现了URL,E-Mail,电话号码,地图的时候,我们可以通过设置autoLink属性:当我们点击 文字中对应部分的文字,即可跳转至某默认APP, ...
- Android学习笔记-TextView(文本框)(一)
1.基础属性详解: id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeL ...
- 2.3.1 TextView(文本框)详解
http://www.runoob.com/w3cnote/android-tutorial-textview.html 1.基础属性详解: 通过下面这个简单的界面,我们来了解几个最基本的属性: 布局 ...
- Andriod给textview文本关键字循环标亮加粗
在开发中,搜索到得关键字信息在展示时,通常需要标亮加粗,如下图(截取自蓝鲸医生助手搜索后的结果) 在文本中,关键字是“嘎”,所有“嘎”字都标亮加粗,标亮就是换种颜色.这里就要用到SpannableSt ...
随机推荐
- 服务器上搭建spark开发环境
1.安装相应的软件 (1)安装jdk 下载地址:http://www.Oracle.com/technetwork/java/javase/downloads/index.html (2)安装scal ...
- opencv Iplimage结构简介
IplImage 结构解读: typedef struct _IplImage{int nSize; /* IplImage大小 */int ID; ...
- SGU 101.Domino (欧拉路)
时间限制: 0.5 sec 空间限制: 4096 KB 描述 多米诺骨牌,一种用小的方的木块或其他材料,每个都被一些点在面上标记,这些木块通常被称为骨牌.每个骨牌的面都被一条线分成两个 方形,两边 ...
- Linux 系统命令及其使用详解(大全)
(来源: 中国系统分析员) cat cd chmod chown cp cut 1.名称:cat 使用权限:所有使用者 使用方式:cat [-AbeEnstTuv] [--help] [--versi ...
- js 去除字符串开头或者前几个字符。slice 也可以用于截取某一部分
摘草自w3 slice() 方法可从已有的数组中返回选定的元素. 语法 arrayObject.slice(start,end) 参数 描述 start 必需.规定从何处开始选取.如果是负数,那么它规 ...
- Linux系统分区
在Linux系统里面,"分区",被称作"挂载点" 挂载点 意思就是把一部分硬盘容量,分成一个文件夹的形式,用来做某些事情,这个文件夹的名字,就叫做:挂载点 (如 ...
- Bootstrap 和 LESS
Bootstrap 简介 什么是 Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的 ...
- cf E. Fox and Card Game
http://codeforces.com/contest/389/problem/E 题意:给你n个序列,然后两个人x,y,两个人玩游戏,x从序列的前面取,y从序列的后面取,两个人都想自己得到的数的 ...
- [C#] - 注入DLL
原文:http://xyzlht.blog.163.com/blog/static/69301417200882834211787/ ) { MessageBox.Show("创建远程线程失 ...
- List、Set、 数组等转字符串
public class Test { public static void main(String[] args) { String str = ""; // list转字符串 ...