Android:计算Text的Bounds
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); // } }
以上是关于Android:计算Text的Bounds的主要内容,如果未能解决你的问题,请参考以下文章
使用 scrollView 和 imageView 的 bounds.height 属性计算 zoomScaleForHeight
android 12/13 分屏设置Task的Bounds区域-系统车机开发必学