Android:计算Text的Bounds

package cc.makeblock.mzero;

import cc.makeblock.android.utils.Loger;
import cc.makeblock.global.Global;
import cc.makeblock.widget.MBButtonCircular;
import cc.makeblock.widget.MBButtonWithIcon;
import cc.makeblock.widget.MBChartView;
import cc.makeblock.widget.MBDiodeViewWithIcon;
import cc.makeblock.widget.MBDirectionView;
import cc.makeblock.widget.MBOnSeekBarChangeListener;
import cc.makeblock.widget.MBSeekBar;
import cc.makeblock.widget.MBSeekBarNoIcon;
import cc.makeblock.widget.MBDirectionView.MBOnDirectionChangedListener;
import cc.makeblock.widget.MBDiodeView;
import cc.makeblock.widget.MBComplicatedView;
import cc.makeblock.widget.MBSeekBarWithIcon;
import cc.makeblock.widget.TestTextBounds;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

public class TestActivity extends BaseActivity {
	protected static final String TAG = "TestAvtivity";
	RelativeLayout root_RelativeLayout;
	Context context;
	MBComplicatedView complicateView;
	MBDiodeView  diodeView;
	MBDirectionView corSignView;
	MBChartView chartView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		context = this;
		Global.init(this);
		root_RelativeLayout = (RelativeLayout) findViewById(R.id.root_RelativeLayout);
		setupCustomView6();
	}

	private void setupCustomView() {
		//		complicateView = new MBComplicatedView(this);
		complicateView.setBackgroundResource(R.drawable.bg);
		RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(complicateView.getMbWidth(),complicateView.getMbHeight());
		relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		int top = 200;
		int left = 300;
		relLayoutParams.setMargins(left, top, 0, 0);
		root_RelativeLayout.addView(complicateView,relLayoutParams);

	}

	private void setupCustomView2() {
		//		diodeView = new MBDiodeView(this);
		//		 diodeView.setBackgroundResource(R.drawable.bg);
		diodeView.setBackgroundColor(Color.WHITE);
		RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(diodeView.getMbWidth(),diodeView.getMbHeight());
		relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		int top = 200;
		int left = 300;
		relLayoutParams.setMargins(left, top, 0, 0);
		root_RelativeLayout.addView(diodeView,relLayoutParams);
	}

	private void setupCustomView3() {
		//		corSignView = new MBDirectionView(this);
		//		 diodeView.setBackgroundResource(R.drawable.bg);
		corSignView.setBackgroundColor(Color.WHITE);
		corSignView.setMbMovable(false);
		RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(corSignView.getMbWidth(),corSignView.getMbHeight());
		relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		int top = 200;
		int left = 300;
		relLayoutParams.setMargins(left, top, 0, 0);
		root_RelativeLayout.addView(corSignView,relLayoutParams);
		corSignView.setMbOnDirectionChangedListener(new MBOnDirectionChangedListener() {
			@Override
			public void onDirectionChanged(int direction, int actionType) {

				switch (actionType) {
				case MotionEvent.ACTION_DOWN:
					Loger.e(TAG, "ACTION_DOWN");
					break;
				case MotionEvent.ACTION_MOVE:
					Loger.e(TAG, "ACTION_MOVE");
					break;
				case MotionEvent.ACTION_UP:
					Loger.e(TAG, "ACTION_UP");
					break;
				}

				switch (direction) {
				case MBDirectionView.DIRECTION_LEFT:
					Loger.e(TAG, "DIRECTION_LEFT");
					break;
				case MBDirectionView.DIRECTION_RIGHT:
					Loger.e(TAG, "DIRECTION_RIGHT");
					break;
				case MBDirectionView.DIRECTION_TOP:
					Loger.e(TAG, "DIRECTION_TOP");
					break;
				case MBDirectionView.DIRECTION_BOTTOM:
					Loger.e(TAG, "DIRECTION_BOTTOM");
					break;
				case MBDirectionView.DIRECTION_UNKNOWN:
					Loger.e(TAG, "DIRECTION_UNKNOWN");
					break;
				}
			}
		});
	}

	private void setupCustomView4() {
		//		chartView = new MBChartView(this);
		chartView.setBackgroundResource(R.drawable.bg);
		//		chartView.setBackgroundColor(Color.WHITE);
		RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(chartView.getMbWidth(),chartView.getMbHeight());
		relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		relLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		int top = 200;
		int left = 300;
		relLayoutParams.setMargins(left, top, 0, 0);
		root_RelativeLayout.addView(chartView,relLayoutParams);
	}

	public void onClick2(View v){
		complicateView.setMbMovable(!complicateView.getMbMovable());
		complicateView.setProgress(complicateView.getProgress()+0.1f);
		complicateView.setDiodeDataNumber((int)(complicateView.getProgress()*100));
		complicateView.setLeftIconResource(R.drawable.control_display_progress_bar_icon);
	}

	public void onClick(View v){
		complicateView.setProgress(complicateView.getProgress()-0.1f);
		complicateView.setDiodeDataNumber((int)(complicateView.getProgress()*100));
		complicateView.setLeftIconResource(R.drawable.control_joystick_01_bg);
	}

	private void setupCustomView5(){
		{
			MBSeekBarNoIcon seekBar1 = new MBSeekBarNoIcon(context, MBSeekBarNoIcon.STYLE_HORIZONTAL_LONG, 1);
			seekBar1.setBackgroundResource(R.drawable.bg);
			seekBar1.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(seekBar1.getMbWidth(),seekBar1.getMbHeight());
			relLayoutParams.setMargins(20, 20, 0, 0);
			root_RelativeLayout.addView(seekBar1,relLayoutParams);
		}
		{
			MBSeekBarNoIcon seekBar2 = new MBSeekBarNoIcon(context, MBSeekBarNoIcon.STYLE_HORIZONTAL_SHORT, 1);
			seekBar2.setBackgroundResource(R.drawable.bg);
			seekBar2.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(seekBar2.getMbWidth(), seekBar2.getMbHeight());
			relLayoutParams.setMargins(20, 220, 0, 0);
			root_RelativeLayout.addView(seekBar2,relLayoutParams);

			seekBar2.setMbOnSeekBarChangeListener(new MBOnSeekBarChangeListener() {
				@Override
				public void onProgressChanged(MBSeekBar mbSeekBar, float progress) {
					Loger.e(TAG, "onProgressChanged:progress="+progress);
				}
				@Override
				public void onStartTrackingTouch(MBSeekBar mbSeekBar) {
					Loger.e(TAG, "onStartTrackingTouch:mbSeekBar="+mbSeekBar);
				}
				@Override
				public void onStopTrackingTouch(MBSeekBar mbSeekBar) {
					Loger.e(TAG, "onStopTrackingTouch:mbSeekBar="+mbSeekBar);
				}
			});
		}
		{
			MBSeekBarNoIcon seekBar3 = new MBSeekBarNoIcon(context, MBSeekBarNoIcon.STYLE_VERTICAL_LONG, 1);
			seekBar3.setBackgroundResource(R.drawable.bg);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(seekBar3.getMbWidth(), seekBar3.getMbHeight());
			relLayoutParams.setMargins(20, 420, 0, 0);
			root_RelativeLayout.addView(seekBar3,relLayoutParams);
		}
		{
			MBSeekBarNoIcon seekBar4 = new MBSeekBarNoIcon(context, MBSeekBarNoIcon.STYLE_VERTICAL_SHORT, 1);
			seekBar4.setBackgroundResource(R.drawable.bg);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(seekBar4.getMbWidth(),seekBar4.getMbHeight());
			relLayoutParams.setMargins(120, 620, 0, 0);
			root_RelativeLayout.addView(seekBar4,relLayoutParams);
		}
		{
			MBSeekBarWithIcon seekBar5 = new MBSeekBarWithIcon(context, MBSeekBarWithIcon.STYLE_HORIZONTAL_LONG_MOTOR, 1);
			seekBar5.setMbMovable(false);
			seekBar5.setBackgroundResource(R.drawable.bg);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(seekBar5.getMbWidth(),seekBar5.getMbHeight());
			relLayoutParams.setMargins(420, 620, 0, 0);
			root_RelativeLayout.addView(seekBar5,relLayoutParams);
			seekBar5.setMbOnSeekBarChangeListener(new MBOnSeekBarChangeListener() {
				@Override
				public void onProgressChanged(MBSeekBar mbSeekBar, float progress) {
					Loger.e(TAG, "onProgressChanged:progress="+progress);
				}
				@Override
				public void onStartTrackingTouch(MBSeekBar mbSeekBar) {
					Loger.e(TAG, "onStartTrackingTouch:mbSeekBar="+mbSeekBar);
				}
				@Override
				public void onStopTrackingTouch(MBSeekBar mbSeekBar) {
					Loger.e(TAG, "onStopTrackingTouch:mbSeekBar="+mbSeekBar);
				}
			});
		}

		{
			MBSeekBarWithIcon seekBar6 = new MBSeekBarWithIcon(context, MBSeekBarWithIcon.STYLE_HORIZONTAL_LONG_DIODE, 1);
			seekBar6.setMbMovable(false);
//			seekBar6.setBackgroundResource(R.drawable.bg);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(seekBar6.getMbWidth(),seekBar6.getMbHeight());
			relLayoutParams.setMargins(620, 720, 0, 0);
			root_RelativeLayout.addView(seekBar6,relLayoutParams);
			seekBar6.setMbOnSeekBarChangeListener(new MBOnSeekBarChangeListener() {
				@Override
				public void onProgressChanged(MBSeekBar mbSeekBar, float progress) {
					Loger.e(TAG, "onProgressChanged:progress="+progress);
				}
				@Override
				public void onStartTrackingTouch(MBSeekBar mbSeekBar) {
					Loger.e(TAG, "onStartTrackingTouch:mbSeekBar="+mbSeekBar);
				}
				@Override
				public void onStopTrackingTouch(MBSeekBar mbSeekBar) {
					Loger.e(TAG, "onStopTrackingTouch:mbSeekBar="+mbSeekBar);
				}
			});
		}

		{
			MBButtonWithIcon button = new MBButtonWithIcon(context, MBButtonWithIcon.STYLE_RED, 2);
			button.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(button.getMbWidth(), button.getMbHeight());
			relLayoutParams.setMargins(1520, 100, 0, 0);
			root_RelativeLayout.addView(button,relLayoutParams);
		}

		{
			MBButtonWithIcon button = new MBButtonWithIcon(context, MBButtonWithIcon.STYLE_GREEN, 2);
			button.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(button.getMbWidth(), button.getMbHeight());
			relLayoutParams.setMargins(1520, 300, 0, 0);
			root_RelativeLayout.addView(button,relLayoutParams);
		}

		{
			MBButtonWithIcon button = new MBButtonWithIcon(context, MBButtonWithIcon.STYLE_BLUE, 2);
			button.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(button.getMbWidth(), button.getMbHeight());
			relLayoutParams.setMargins(1520, 500, 0, 0);
			root_RelativeLayout.addView(button,relLayoutParams);
		}

		{
			MBButtonWithIcon button = new MBButtonWithIcon(context, MBButtonWithIcon.STYLE_BUTTON, 2);
			button.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(button.getMbWidth(), button.getMbHeight());
			relLayoutParams.setMargins(1520, 700, 0, 0);
			root_RelativeLayout.addView(button,relLayoutParams);
		}

		{
			MBButtonWithIcon button = new MBButtonWithIcon(context, MBButtonWithIcon.STYLE_BUZZER, 2);
			button.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(button.getMbWidth(), button.getMbHeight());
			relLayoutParams.setMargins(1520, 900, 0, 0);
			root_RelativeLayout.addView(button,relLayoutParams);
		}

		{
			MBDiodeView diodeView = new MBDiodeView(context, 1);
			diodeView.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(diodeView.getMbWidth(), diodeView.getMbHeight());
			relLayoutParams.setMargins(1020, 900, 0, 0);
			root_RelativeLayout.addView(diodeView,relLayoutParams);
		}

		{
			MBDiodeViewWithIcon diodeView = new MBDiodeViewWithIcon(context, MBDiodeViewWithIcon.STYLE_CUSTOM, 1);
			diodeView.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(diodeView.getMbWidth(), diodeView.getMbHeight());
			relLayoutParams.setMargins(1020, 0, 0, 0);
			root_RelativeLayout.addView(diodeView,relLayoutParams);
		}
		{
			MBDiodeViewWithIcon diodeView = new MBDiodeViewWithIcon(context, MBDiodeViewWithIcon.STYLE_ULTRASONIC, 1);
			diodeView.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(diodeView.getMbWidth(), diodeView.getMbHeight());
			relLayoutParams.setMargins(1020, 200, 0, 0);
			root_RelativeLayout.addView(diodeView,relLayoutParams);
		}
		{
			MBDiodeViewWithIcon diodeView = new MBDiodeViewWithIcon(context, MBDiodeViewWithIcon.STYLE_BRIGHTNESS, 1);
			diodeView.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(diodeView.getMbWidth(), diodeView.getMbHeight());
			relLayoutParams.setMargins(1020, 400, 0, 0);
			root_RelativeLayout.addView(diodeView,relLayoutParams);
		}
		{
			MBDiodeViewWithIcon diodeView = new MBDiodeViewWithIcon(context, MBDiodeViewWithIcon.STYLE_SOUND, 1);
			diodeView.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(diodeView.getMbWidth(), diodeView.getMbHeight());
			relLayoutParams.setMargins(1020, 600, 0, 0);
			root_RelativeLayout.addView(diodeView,relLayoutParams);
		}
		{
			MBDiodeViewWithIcon diodeView = new MBDiodeViewWithIcon(context, MBDiodeViewWithIcon.STYLE_TEMPERATURE, 1);
			diodeView.setMbMovable(false);
			RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(diodeView.getMbWidth(), diodeView.getMbHeight());
			relLayoutParams.setMargins(1020, 800, 0, 0);
			root_RelativeLayout.addView(diodeView,relLayoutParams);
		}

		{

		}
	}

	private void setupCustomView6(){
//		TestTextBounds diodeView = new TestTextBounds(context);
//		RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(700, 300);
//		relLayoutParams.setMargins(10, 20, 0, 0);
//		root_RelativeLayout.addView(diodeView,relLayoutParams);

		View subView = new View(context);
		subView.setBackgroundColor(Color.RED);
		RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(700, 300);
		relLayoutParams.setMargins(10, 20, 0, 0);
		root_RelativeLayout.addView(subView, relLayoutParams);

	}
	//	public void onClick2(View v){
	//		diodeView.setDiodeDataNumber(diodeView.getDiodeDataNumber()+120);
	//	}
	//
	//	public void onClick(View v){
	//		diodeView.setDiodeDataNumber(diodeView.getDiodeDataNumber()-110);
	//	}
}

  

时间: 2024-12-19 10:59:01

Android:计算Text的Bounds的相关文章

Android 计算view 的高度

上午在做一个QuickAction里嵌套一个ListView,在Demo运行没事,结果引入到我的项目里,发现我先让它在Button上面,结果是无视那个Button的高度,这很明显,就是那个Button的高度计算不正确. 看了下别人的建议,大概分为三类: 参数设置: ? mRootView.measure(0, 0); 在draw之前回调: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ViewTreeObserver vto =view.getViewTreeObs

Android 计算当前使用内存百分比的方法

一些手机管家软件可以显示当前系统内存使用了百分之多少,这个数字的得到是通过 取得总内存大小,减去可用内存大小,再与总大小做比值的结果,下面代码可以计算内存使用了多少. /** * 计算已使用内存的百分比 * */ public static String getUsedPercentValue(Context context) { String dir = "/proc/meminfo"; try { FileReader fr = new FileReader(dir); Buffe

【MySQL】使用Length和Cast函数计算TEXT类型字段的长度

背景: 前段时间,业务需要,为了快速让解析的Excel入库,所以把不是很确定的字段全部设置成了TEXT. 今天需要进行表结构优化,把字段长度控制在合适的范围,并尽量不使用TEXT类型. -- 计算长度select LENGTH(CAST(fileName AS CHAR)) from files; select MAX(LENGTH(CAST(fileName AS CHAR))) from files; select LENGTH(CAST(fileName AS CHAR)) as le f

android 计算所在周所在月的前后日期

效果图: 使用了 一个时间相关的工具类 package com.yqy.yqy_date; import android.util.Log; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * 时间工具类 * @author YQY * */ public class ToolDateTime {

Android 计算器界面的实现

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent&

两种计算Text文本的尺寸的区别

(1) 通过getTextBounds的方式获取Text的绘制尺寸 /*** * 通过getTextBounds的方式获取Text的绘制尺寸 * @param text * @return */private int[] getTextMeasurementByBound(String text){ Rect textRect = new Rect(); Paint selectedItemPaint = new Paint(); selectedItemPaint.getTextBounds(

Android 计算Bitmap大小

今天使用LruCache写demo的时候,要获取Bitmap的大小 于是就用到了 return bitmap.getRowBytes() * bitmap.getHeight();// 获取大小并返回//Bitmap所占用的内存空间数等于Bitmap的每一行所占用的空间数乘以Bitmap的行数 为什么不用bitmap.getByteCount()呢?因为getByteCount要求的API版本较高,考虑到兼容性使用上面的方法 1.getRowBytes:Since API Level 12.ge

Android计算app启动时间之二

采用一中的方法计算启动时间,输入adb shell am start -W apk/apk.MainActivity 会报没有权限的错误,google了下,要修改某些东西,懒的改了. 现在来讲下第二种方法,我们可以直接查看日志:如图: 当然有的app首次安装启动会停在引导页,有的app未登录会停在登录界面,这些也可以通过日志查看启动时间.

Android 计算布局背景的透明度

1.#ff000000 此为16进制颜色代码,前2位ff为透明度,后6位为颜色值(000000为黑色,ffffff为白色,可以用ps等软件获取). 2.透明度分为256阶(0-255),计算机上用16进制表示为(00-ff). 透明就是0阶,不透明就是255阶,如果50%透明就是127阶(256的一半当然是128,但因为是从0开始,所以实际上是127). 3.10进制的255换算成16进制是ff,127换算成16进制是7f,#7f000000 代表50%透明度的黑色. (写成#50000000当