View、Text、Button的drawableLeft左侧图片自定义大小
生活随笔
收集整理的這篇文章主要介紹了
View、Text、Button的drawableLeft左侧图片自定义大小
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
設置工具類
public class ImageTextButton extends android.support.v7.widget.AppCompatTextView {private Drawable drawableLeft;private int scaleWidth; //dp值private int scaleHeight;public ImageTextButton(Context context) {super(context);}public ImageTextButton(Context context, AttributeSet attrs) {super(context, attrs);init(context,attrs);}public ImageTextButton(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}public void init(Context context, AttributeSet attrs) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageTextButton);drawableLeft = typedArray.getDrawable(R.styleable.ImageTextButton_leftDrawable);scaleWidth = typedArray.getDimensionPixelOffset(R.styleable.ImageTextButton_drawableWidth, UIUtils.dp2px(getContext(),20));scaleHeight = typedArray.getDimensionPixelOffset(R.styleable.ImageTextButton_drawableHeight, UIUtils.dp2px(getContext(),20));}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);if (drawableLeft != null) {drawableLeft.setBounds(0, 0, UIUtils.dp2px(getContext(),scaleWidth), UIUtils.dp2px(getContext(),scaleHeight));}}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);this.setCompoundDrawables(drawableLeft, null, null, null);}/*** 設置左側圖片并重繪* @param drawableLeft*/public void setDrawableLeft(Drawable drawableLeft) {this.drawableLeft = drawableLeft;invalidate();}/*** 設置左側圖片并重繪* @param drawableLeftRes*/public void setDrawableLeft(int drawableLeftRes) {this.drawableLeft = UIUtils.getContext().getResources().getDrawable(drawableLeftRes);invalidate();} }?
附上attrs:
<!-- ImageTextButton attrs--> <attr name="leftDrawable" format="reference"/> <attr name="drawableWidth" format="dimension"/> <attr name="drawableHeight" format="dimension"/><declare-styleable name="ImageTextButton"><attr name="leftDrawable"/><attr name="drawableWidth"/><attr name="drawableHeight"/> </declare-styleable>?
最后在xml中調用即可,讓我們看一下效果
<com.guorentong.learn.organ.utils.ImageTextButtonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:drawablePadding="5dp"android:text="課程時長"android:textColor="@color/colorText"android:textSize="30px"app:drawableHeight="20px"app:drawableWidth="6px"app:leftDrawable="@drawable/lantiao" />?
尺寸轉化類
public class UIUtils {public static Context getContext() {return MyApp.getInstance();}/*** 獲取資源對象*/public static Resources getResources() {return getContext().getResources();}/*** 獲取資源文件字符串** @param id* @return*/public static String getString(int id) {return getResources().getString(id);}/*** 獲取資源文件字符串數組** @param id* @return*/public static String[] getStringArray(int id) {return getResources().getStringArray(id);}/*** 獲取資源文件圖片** @param id* @return*/public static Drawable getDrawable(int id) {return ContextCompat.getDrawable(getContext(), id);}/*** 獲取資源文件顏色** @param id* @return*/public static int getColor(int id) {return ContextCompat.getColor(getContext(), id);}/*** 根據id獲取顏色的狀態選擇器** @param id* @return*/public static ColorStateList getColorStateList(int id) {return ContextCompat.getColorStateList(getContext(), id);}/*** 獲取尺寸** @param id* @return*/public static int getDimen(int id) {return getResources().getDimensionPixelSize(id); // 返回具體像素值}/*** dp轉px*/public static int dp2px(Context context, float dpVal) {return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal, context.getResources().getDisplayMetrics());}/*** px -> dp** @param px* @return*/public static float px2dp(int px) {float density = getResources().getDisplayMetrics().density;return px / density;}/*** 加載布局文件** @param id* @return*/public static View inflate(int id) {return View.inflate(getContext(), id, null);}/*** 把自身從父View中移除* @param view*/public static void removeSelfFromParent(View view) {if (view != null) {ViewParent parent = view.getParent();if (parent != null && parent instanceof ViewGroup) {ViewGroup group = (ViewGroup) parent;group.removeView(view);}}}/*** 請求View樹重新布局,用于解決中層View有布局狀態而導致上層View狀態斷裂* @param view* @param isAll*/public static void requestLayoutParent(View view, boolean isAll) {ViewParent parent = view.getParent();while (parent != null && parent instanceof View) {if (!parent.isLayoutRequested()) {parent.requestLayout();if (!isAll) {break;}}parent = parent.getParent();}}/*** 判斷觸點是否落在該View上* @param ev* @param v* @return*/public static boolean isTouchInView(MotionEvent ev, View v) {int[] vLoc = new int[2];v.getLocationOnScreen(vLoc);float motionX = ev.getRawX();float motionY = ev.getRawY();return motionX >= vLoc[0] && motionX <= (vLoc[0] + v.getWidth())&& motionY >= vLoc[1] && motionY <= (vLoc[1] + v.getHeight());}/*** findViewById的泛型封裝,減少強轉代碼* @param layout* @param id* @param <T>* @return*/public static <T extends View> T findViewById(View layout, int id) {return (T) layout.findViewById(id);}/*** 獲取屏幕的比例* @param context* @return*/public static float getScaledDensity(Context context) {DisplayMetrics dm = context.getResources().getDisplayMetrics();float value = dm.scaledDensity;return value;}/*** 獲取控件的高度,如果獲取的高度為0,則重新計算尺寸后再返回高度* @param view* @return*/public static int getViewMeasuredHeight(View view) {calcViewMeasure(view);return view.getMeasuredHeight();}/*** 獲取控件的寬度,如果獲取的寬度為0,則重新計算尺寸后再返回寬度* @param view* @return*/public static int getViewMeasuredWidth(View view) {calcViewMeasure(view);return view.getMeasuredWidth();}/*** 測量控件的尺寸* @param view*/public static void calcViewMeasure(View view) {int width = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);view.measure(width, expandSpec);}/*** 設置textview指定文字為某一顏色** @param content 顯示的文字* @param color 需要轉換成的顏色值* @param start 需要變色文字開始位置* @param end 需要變色文字結束位置*/public static SpannableStringBuilder changeTextColor(String content, int color, int start, int end) {SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);spannableStringBuilder.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);return spannableStringBuilder;} }drawable樣式文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"><!--顏色--><solid android:color="@color/colorAccent"/><!--內邊距--><!--<padding--><!--android:bottom="20dp"--><!--android:left="20dp"--><!--android:right="20dp"--><!--android:top="20dp"/>--><corners android:topLeftRadius="10dp"android:topRightRadius="10dp"android:bottomRightRadius="10dp"android:bottomLeftRadius="10dp"/><!--設置圓角--><corners android:radius="10dip"/><!--stroke設置描邊--><!--<stroke--><!--android:width="2dp"--><!--android:color="@android:color/darker_gray"/>--> </shape>轉載于:https://my.oschina.net/u/3698786/blog/1806228
總結
以上是生活随笔為你收集整理的View、Text、Button的drawableLeft左侧图片自定义大小的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图片上传缩略图预览
- 下一篇: 日志分析工具 Log Parser