看看几个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中文本的位置相关推荐

  1. pdf python 位置_如何使用PDFMiner获取PDF中文本的位置?

    您正在每个布局对象上查找bbox属性.PDFMiner文档中有一些关于how to parse the layout hierarchy的信息,但它并没有涵盖所有内容. 下面是一个例子:from pd ...

  2. android 获取布局textview,android – 获取TextView中文本的位置

    看看几个Paint方法: getTextBounds()和 measureText.我们可以使用它们来确定TextView中文本的偏移量.确定TextView中的偏移后,我们可以将其添加到TextVi ...

  3. android motionevent 坐标,Android开发中MotionEvent坐标获取方法分析

    本文实例讲述了Android开发中MotionEvent坐标获取方法.分享给大家供大家参考,具体如下: Android MotionEvent中getX()与getRawX()都是获取屏幕坐标(横), ...

  4. android 动画坐标,android TranslateAnimation动画执行时的坐标获取。

    android 的Tween动画并不会改变控件的属性值,比如以下测试片段: 定义一个从屏幕右边进入,滚动到屏幕左边消失的一个TranslateAnimation动画: <?xml version ...

  5. android motionevent 坐标,Android坐标系、视图坐标系与触控事件(MotionEvent)

    前言:本篇文章讲解Android坐标系.视图坐标系与触控事件(MotionEvent) 一. Android 的坐标系:#### 在Android中,将屏幕左上角的定点座位Android坐标系的原点, ...

  6. android 动画坐标,Android应用坐标系统全面详解

    1 背景 去年有很多人私信告诉我让说说自定义控件,其实通观网络上的很多博客都在讲各种自定义控件,但是大多数都是授之以鱼,却很少有较为系统性授之于渔的文章,同时由于自己也迟迟没有时间规划这一系列文章,最 ...

  7. android触摸屏坐标,android触摸屏坐标手工校准/调整成功

    1.简述 android 的坐标转换处理: This implementation is a linear transformation using 7 parameters (a, b, c, d, ...

  8. android 绝对坐标,Android布局之绝对布局AbsoluteLayout详解

    本文实例为大家分享了Android绝对布局AbsoluteLayout的具体代码,供大家参考,具体内容如下 1>AbsoluteLayout(绝对布局) 又可以叫做坐标布局,可以直接指定子元素的 ...

  9. C语言文件操作:获取文件中文本的大小(长度)

    1.方法 1.1 获取文件大小 ftell()函数 描述:C 库函数 long int ftell(FILE *fp ) 返回给定流 stream 的当前文件位置. 参数:fp -- 这是指向 FIL ...

最新文章

  1. 多进程PHP脚本实现海量数据转移总结
  2. 24.Interpreter-解释器模式
  3. golang打包HTML为Android,使用Go开发Web服务,并打包html/js/css等静态资源文件
  4. pom.xml中的常用依赖包总结
  5. 菜鸟教程终极篇之Microsoft Windows Pre-installation Environment (Windows PE) 2.0
  6. 广告banner:手动滑动切换,自动切换,点击跳转,异步加载网络图片
  7. [BuildRelease Management]Team City
  8. iOS开发 CGAffineTransform 让图片旋转, 旋转后获得图片旋转的角度
  9. .net core实践系列之SSO-同域实现
  10. bec初级第一课_在您的第一个初级开发人员工作中如何生存和发展
  11. GROOVY简单语法实习
  12. 适用于Mac的WinX HD视频转换器:视频转换教程
  13. 通信专业顶刊_通信类会议期刊排名(转)
  14. 区块链底层架构概览:第一原则框架
  15. vmware workstation 16 安装centos7 全记录(文字版)
  16. 麒麟V10 kylin v10服务器版yum软件源官方源亲测可用
  17. 电脑误删分区如何恢复?图文详解
  18. 爬取拉勾网招聘信息笔记
  19. Python可视化基础----从0学会matplotlib折线图,条形图,散点图
  20. lol手游日服服务器未响应,LOL手游进不去怎么回事?日服登陆失败解决办法[多图]...

热门文章

  1. Android权威官方屏幕适配全攻略
  2. python画小汽车代码_Python实战小程序利用matplotlib模块画图代码分享
  3. ChatGPT真神奇,但是也真焦虑
  4. java框架 初始化Bean的方法
  5. Java中System.out.println()简单简介
  6. Linux系统如何关闭防火墙【linux系统防火墙】
  7. 事件冒泡(快速理解)
  8. 三维虚拟数字沙盘电子沙盘军标注推演开发教程第41课 多点触摸三维电子沙盘可视化交互系统
  9. JS高级 之 网络编程 - XHR Fetch
  10. 哪吒之魔童降世——豆瓣电影评论爬虫