android textview坐标,android – 获取TextView中文本的位置
看看几个Paint方法:
getTextBounds()和
measureText.我们可以使用它们来确定TextView中文本的偏移量.确定TextView中的偏移后,我们可以将其添加到TextView本身的位置,以确定文本的屏幕坐标(如果需要).
以下示例在三个TextView中查找文本的边界,并在文本周围绘制一个矩形.矩形包含TextView中文本的(x,y)坐标.
activity_main.xml中
一个简单的演示布局.
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@android:color/holo_blue_light"
android:padding="24dp"
android:text="Hello World"
android:textColor="@android:color/black"
android:textSize="50sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@android:color/holo_blue_light"
android:padding="24dp"
android:text="Hello Worldly"
android:textColor="@android:color/black"
android:textSize="50sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView1" />
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@android:color/holo_blue_light"
android:padding="24dp"
android:text="aaaaaaaaaa"
android:textColor="@android:color/black"
android:textSize="50sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView2" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawTextBounds((TextView) findViewById(R.id.textView1));
drawTextBounds((TextView) findViewById(R.id.textView2));
drawTextBounds((TextView) findViewById(R.id.textView3));
}
private void drawTextBounds(TextView textView) {
// Force measure of text pre-layout.
textView.measure(0,0);
String s = (String) textView.getText();
// bounds will store the rectangle that will circumscribe the text.
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
// Get the bounds for the text. Top and bottom are measured from the baseline. Left
// and right are measured from 0.
textPaint.getTextBounds(s,s.length(),bounds);
int baseline = textView.getBaseline();
bounds.top = baseline + bounds.top;
bounds.bottom = baseline + bounds.bottom;
int startPadding = textView.getPaddingStart();
bounds.left += startPadding;
// textPaint.getTextBounds() has already computed a value for the width of the text,// however,Paint#measureText() gives a more accurate value.
bounds.right = (int) textPaint.measureText(s,s.length()) + startPadding;
// At this point,(x,y) of the text within the TextView is (bounds.left,bounds.top)
// Draw the bounding rectangle.
Bitmap bitmap = Bitmap.createBitmap(textView.getMeasuredWidth(),textView.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint rectPaint = new Paint();
rectPaint.setColor(Color.RED);
rectPaint.setStyle(Paint.Style.STROKE);
rectPaint.setStrokeWidth(1);
canvas.drawRect(bounds,rectPaint);
textView.setForeground(new BitmapDrawable(getResources(),bitmap));
}
}
android textview坐标,android – 获取TextView中文本的位置相关推荐
- pdf python 位置_如何使用PDFMiner获取PDF中文本的位置?
您正在每个布局对象上查找bbox属性.PDFMiner文档中有一些关于how to parse the layout hierarchy的信息,但它并没有涵盖所有内容. 下面是一个例子:from pd ...
- android 获取布局textview,android – 获取TextView中文本的位置
看看几个Paint方法: getTextBounds()和 measureText.我们可以使用它们来确定TextView中文本的偏移量.确定TextView中的偏移后,我们可以将其添加到TextVi ...
- android motionevent 坐标,Android开发中MotionEvent坐标获取方法分析
本文实例讲述了Android开发中MotionEvent坐标获取方法.分享给大家供大家参考,具体如下: Android MotionEvent中getX()与getRawX()都是获取屏幕坐标(横), ...
- android 动画坐标,android TranslateAnimation动画执行时的坐标获取。
android 的Tween动画并不会改变控件的属性值,比如以下测试片段: 定义一个从屏幕右边进入,滚动到屏幕左边消失的一个TranslateAnimation动画: <?xml version ...
- android motionevent 坐标,Android坐标系、视图坐标系与触控事件(MotionEvent)
前言:本篇文章讲解Android坐标系.视图坐标系与触控事件(MotionEvent) 一. Android 的坐标系:#### 在Android中,将屏幕左上角的定点座位Android坐标系的原点, ...
- android 动画坐标,Android应用坐标系统全面详解
1 背景 去年有很多人私信告诉我让说说自定义控件,其实通观网络上的很多博客都在讲各种自定义控件,但是大多数都是授之以鱼,却很少有较为系统性授之于渔的文章,同时由于自己也迟迟没有时间规划这一系列文章,最 ...
- android触摸屏坐标,android触摸屏坐标手工校准/调整成功
1.简述 android 的坐标转换处理: This implementation is a linear transformation using 7 parameters (a, b, c, d, ...
- android 绝对坐标,Android布局之绝对布局AbsoluteLayout详解
本文实例为大家分享了Android绝对布局AbsoluteLayout的具体代码,供大家参考,具体内容如下 1>AbsoluteLayout(绝对布局) 又可以叫做坐标布局,可以直接指定子元素的 ...
- C语言文件操作:获取文件中文本的大小(长度)
1.方法 1.1 获取文件大小 ftell()函数 描述:C 库函数 long int ftell(FILE *fp ) 返回给定流 stream 的当前文件位置. 参数:fp -- 这是指向 FIL ...
最新文章
- 多进程PHP脚本实现海量数据转移总结
- 24.Interpreter-解释器模式
- golang打包HTML为Android,使用Go开发Web服务,并打包html/js/css等静态资源文件
- pom.xml中的常用依赖包总结
- 菜鸟教程终极篇之Microsoft Windows Pre-installation Environment (Windows PE) 2.0
- 广告banner:手动滑动切换,自动切换,点击跳转,异步加载网络图片
- [BuildRelease Management]Team City
- iOS开发 CGAffineTransform 让图片旋转, 旋转后获得图片旋转的角度
- .net core实践系列之SSO-同域实现
- bec初级第一课_在您的第一个初级开发人员工作中如何生存和发展
- GROOVY简单语法实习
- 适用于Mac的WinX HD视频转换器:视频转换教程
- 通信专业顶刊_通信类会议期刊排名(转)
- 区块链底层架构概览:第一原则框架
- vmware workstation 16 安装centos7 全记录(文字版)
- 麒麟V10 kylin v10服务器版yum软件源官方源亲测可用
- 电脑误删分区如何恢复?图文详解
- 爬取拉勾网招聘信息笔记
- Python可视化基础----从0学会matplotlib折线图,条形图,散点图
- lol手游日服服务器未响应,LOL手游进不去怎么回事?日服登陆失败解决办法[多图]...