日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

        歡迎訪問 生活随笔!

        生活随笔

        當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

        编程问答

        android TextView下划线,圆角边框,数逐字显示,虚线边框, 渐变色背景框, 阴影背景框

        發布時間:2023/12/10 编程问答 35 豆豆
        生活随笔 收集整理的這篇文章主要介紹了 android TextView下划线,圆角边框,数逐字显示,虚线边框, 渐变色背景框, 阴影背景框 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

        ?長方形

        <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><size android:width="10dp" android:height="5dp" /><corners android:radius="2dp"/><solid android:color="@color/app_white" /> </shape>

        #cccccc#E1E1E1#e5e5e5#dddddd#F5F5F5#F8F8F8#FAFAFA#E6E6E6

        xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"

        <?xml version="1.0" encoding="utf-8"?>

        ?陰影背景框

        內View 樣式drawable

        <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@color/white" /><corners android:topLeftRadius="7dp" android:topRightRadius="7dp"android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" /> </shape>

        樣式文件

        <declare-styleable name="ShadowContainer"><attr name="containerShadowColor" format="color"/><attr name="containerShadowRadius" format="dimension"/><attr name="containerDeltaLength" format="dimension"/><attr name="containerCornerRadius" format="dimension"/><attr name="deltaX" format="dimension"/><attr name="deltaY" format="dimension"/><attr name="enable" format="boolean"/> </declare-styleable>

        自定義陰影View

        /**陰影效果*/ public class ShadowContainerAllEdge extends ViewGroup {private final float deltaLength;private final float cornerRadius;private final Paint mShadowPaint;private boolean drawShadow;public ShadowContainerAllEdge(Context context) {this(context, null);}public ShadowContainerAllEdge(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ShadowContainerAllEdge(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShadowContainer);int shadowColor = a.getColor(R.styleable.ShadowContainer_containerShadowColor, Color.RED); // int shadowColor = Color.RED;float shadowRadius = a.getDimension(R.styleable.ShadowContainer_containerShadowRadius, 0);deltaLength = a.getDimension(R.styleable.ShadowContainer_containerDeltaLength, 0);cornerRadius = a.getDimension(R.styleable.ShadowContainer_containerCornerRadius, 0);float dx = a.getDimension(R.styleable.ShadowContainer_deltaX, 0);float dy = a.getDimension(R.styleable.ShadowContainer_deltaY, 0);drawShadow = a.getBoolean(R.styleable.ShadowContainer_enable, true);a.recycle();mShadowPaint = new Paint();mShadowPaint.setStyle(Paint.Style.FILL);mShadowPaint.setAntiAlias(true);mShadowPaint.setColor(shadowColor);mShadowPaint.setShadowLayer(shadowRadius, dx, dy, shadowColor);}@Overrideprotected void onFinishInflate() {super.onFinishInflate();}@Overrideprotected void dispatchDraw(Canvas canvas) {if (drawShadow) {if (getLayerType() != LAYER_TYPE_SOFTWARE) {setLayerType(LAYER_TYPE_SOFTWARE, null);}View child = getChildAt(0);int left = child.getLeft();int top = child.getTop();int right = child.getRight();int bottom = child.getBottom();if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {canvas.drawRoundRect(left, top, right, bottom, cornerRadius, cornerRadius, mShadowPaint);} else {Path drawablePath = new Path();drawablePath.moveTo(left + cornerRadius, top);drawablePath.arcTo(new RectF(left, top, left + 2 * cornerRadius, top + 2 * cornerRadius), -90, -90, false);drawablePath.lineTo(left, bottom - cornerRadius);drawablePath.arcTo(new RectF(left, bottom - 2 * cornerRadius, left + 2 * cornerRadius, bottom), 180, -90, false);drawablePath.lineTo(right - cornerRadius, bottom);drawablePath.arcTo(new RectF(right - 2 * cornerRadius, bottom - 2 * cornerRadius, right, bottom), 90, -90, false);drawablePath.lineTo(right, top + cornerRadius);drawablePath.arcTo(new RectF(right - 2 * cornerRadius, top, right, top + 2 * cornerRadius), 0, -90, false);drawablePath.close();canvas.drawPath(drawablePath, mShadowPaint);}}super.dispatchDraw(canvas);}public void setDrawShadow(boolean drawShadow){if (this.drawShadow == drawShadow){return;}this.drawShadow = drawShadow;postInvalidate();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);if (getChildCount() != 1) {throw new IllegalStateException("子View只能有一個");}int measuredWidth = getMeasuredWidth();int measuredHeight = getMeasuredHeight();int widthMode = MeasureSpec.getMode(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);View child = getChildAt(0);LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();int childBottomMargin = (int) (Math.max(deltaLength, layoutParams.bottomMargin) + 1);int childLeftMargin = (int) (Math.max(deltaLength, layoutParams.leftMargin) + 1);int childRightMargin = (int) (Math.max(deltaLength, layoutParams.rightMargin) + 1);int childTopMargin = (int) (Math.max(deltaLength, layoutParams.topMargin) + 1);int widthMeasureSpecMode;int widthMeasureSpecSize;int heightMeasureSpecMode;int heightMeasureSpecSize;if (widthMode == MeasureSpec.UNSPECIFIED){widthMeasureSpecMode = MeasureSpec.UNSPECIFIED;widthMeasureSpecSize = MeasureSpec.getSize(widthMeasureSpec);}else {if (layoutParams.width == LayoutParams.MATCH_PARENT) {widthMeasureSpecMode = MeasureSpec.EXACTLY;widthMeasureSpecSize = measuredWidth - childLeftMargin - childRightMargin;} else if (LayoutParams.WRAP_CONTENT == layoutParams.width) {widthMeasureSpecMode = MeasureSpec.AT_MOST;widthMeasureSpecSize = measuredWidth - childLeftMargin - childRightMargin;} else {widthMeasureSpecMode = MeasureSpec.EXACTLY;widthMeasureSpecSize = layoutParams.width;}}if (heightMode == MeasureSpec.UNSPECIFIED){heightMeasureSpecMode = MeasureSpec.UNSPECIFIED;heightMeasureSpecSize = MeasureSpec.getSize(heightMeasureSpec);}else {if (layoutParams.height == LayoutParams.MATCH_PARENT) {heightMeasureSpecMode = MeasureSpec.EXACTLY;heightMeasureSpecSize = measuredHeight - childBottomMargin - childTopMargin;} else if (LayoutParams.WRAP_CONTENT == layoutParams.height) {heightMeasureSpecMode = MeasureSpec.AT_MOST;heightMeasureSpecSize = measuredHeight - childBottomMargin - childTopMargin;} else {heightMeasureSpecMode = MeasureSpec.EXACTLY;heightMeasureSpecSize = layoutParams.height;}}measureChild(child, MeasureSpec.makeMeasureSpec(widthMeasureSpecSize, widthMeasureSpecMode), MeasureSpec.makeMeasureSpec(heightMeasureSpecSize, heightMeasureSpecMode));int parentWidthMeasureSpec = MeasureSpec.getMode(widthMeasureSpec);int parentHeightMeasureSpec = MeasureSpec.getMode(heightMeasureSpec);int height = measuredHeight;int width = measuredWidth;int childHeight = child.getMeasuredHeight();int childWidth = child.getMeasuredWidth();if (parentHeightMeasureSpec == MeasureSpec.AT_MOST){height = childHeight + childTopMargin + childBottomMargin;}if (parentWidthMeasureSpec == MeasureSpec.AT_MOST){width = childWidth + childRightMargin + childLeftMargin;}if (width < childWidth + 2 * deltaLength){width = (int) (childWidth + 2 * deltaLength);}if (height < childHeight + 2 * deltaLength){height = (int) (childHeight + 2 * deltaLength);}if (height != measuredHeight || width != measuredWidth){setMeasuredDimension(width, height);}}static class LayoutParams extends MarginLayoutParams{public LayoutParams(Context c, AttributeSet attrs) {super(c, attrs);}public LayoutParams(int width, int height) {super(width, height);}public LayoutParams(MarginLayoutParams source) {super(source);}public LayoutParams(ViewGroup.LayoutParams source) {super(source);}}@Overrideprotected ViewGroup.LayoutParams generateDefaultLayoutParams() {return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);}@Overrideprotected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {return new LayoutParams(p);}@Overridepublic ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {return new LayoutParams(getContext(), attrs);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {View child = getChildAt(0);int measuredWidth = getMeasuredWidth();int measuredHeight = getMeasuredHeight();int childMeasureWidth = child.getMeasuredWidth();int childMeasureHeight = child.getMeasuredHeight();child.layout((measuredWidth - childMeasureWidth) / 2, (measuredHeight - childMeasureHeight) / 2, (measuredWidth + childMeasureWidth) / 2, (measuredHeight + childMeasureHeight) / 2);} }

        ?調用

        app:containerDeltaLength——外圍距離

        app:containerCornerRadius——內內容半徑

        app:containerShadowRadius——陰影半徑

        <com.dlc.observabletest.ShadowContainerAllEdgeandroid:layout_width="200dp"android:layout_height="100dp"app:containerDeltaLength="2dp"app:containerShadowColor="#4D00C8D2"app:containerCornerRadius="7dp"app:containerShadowRadius="2dp"><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/shape_radius_14_white"android:text="aabb"></TextView></com.dlc.observabletest.ShadowContainerAllEdge>

        設置上下圓角

        <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="@color/white" /><stroke android:width="1dp" android:color="@color/white"/><corners android:topLeftRadius="10dp"android:topRightRadius="10dp"android:bottomRightRadius="0dp"android:bottomLeftRadius="0dp" /></shape>

        中空框

        <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><stroke android:width="@dimen/normal_1dp" android:color="@color/color_EF611E"/><corners android:radius="@dimen/normal_5dp"/></shape>

        ?漸變色背景框

        <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="2pt" /><paddingandroid:bottom="0dp"android:left="0dp"android:right="0dp"android:top="0dp" /><gradientandroid:angle="180"android:endColor="@color/top_endc"android:startColor="@color/top_startc"android:type="linear"/></shape>

        虛線邊框

        <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#ffffff" /><corners android:radius="10dp" /><strokeandroid:width="1dp"android:color="@color/color_linexulv"android:dashGap="3pt"android:dashWidth="6pt"/><paddingandroid:bottom="0dp"android:left="0dp"android:right="0dp"android:top="0dp" /></shape>

        drawable文件下創建 border_red.xml樣式 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#ffffff" /><corners android:radius="30dp" /><strokeandroid:width="1dp"android:color="#c416ff" /><paddingandroid:bottom="5dp"android:left="10dp"android:right="10dp"android:top="5dp" /></shape>

        選中樣式

        <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true"><shape android:shape="rectangle"><solid android:color="#56eccb" /><corners android:radius="@dimen/normal_10dp" /></shape></item><item android:state_pressed="false"><shape android:shape="rectangle"><solid android:color="@color/public_color" /><corners android:radius="@dimen/normal_10dp" /></shape></item> </selector>

        <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><corners android:radius="40dp"/><solid android:color="@color/item_gank_grey"/> </shape>

        引用

        <TextViewandroid:id="@+id/item_gank_who"android:layout_width="50dp"android:layout_height="16dp"android:text="推薦"android:textColor="@color/item_gank_white"android:textSize="12sp"android:gravity="center"android:background="@drawable/border_red"/>

        無背景

        holder.status.setBackgroundDrawable(null);

        固定在頂部

        android:layout_alignParentTop="true"

        邊框顏色

        <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"><strokeandroid:color="@color/company_info_blue"android:width="1dp"/><solid android:color="@color/white"/><corners android:radius="5dp"/> </shape>

        人民幣 ?

        獲取URL鍵值

        final String epId = TextUtil.URLRequest(link).get("param");

        public class TextUtil {public static boolean isEmpty(String str) {return TextUtils.isEmpty(str) || "null".equals(str);}//判斷是否有表情public static boolean isEmojiCharacter(char codePoint) {return !(((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));}// 設置下劃線public static TextView setBelowLine(TextView textView) {textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); // 下劃線return textView;}// 設置斜體字public static SpannableString setSpanWord(String word) {SpannableString sp = new SpannableString(word);// 設置斜體sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0,word.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);return sp;}// 設置字體顏色public static SpannableStringBuilder setStringColor(String word,String changePart, String color) {char[] c = changePart.toCharArray();int lastIndex = c.length - 1;int start = word.indexOf(c[0]);int end = start + lastIndex + 1;SpannableStringBuilder style = new SpannableStringBuilder(word);style.setSpan(new ForegroundColorSpan(Color.parseColor(color)), start,end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);return style;}/*** 格式化要顯示的字符串,做非空判斷* 該方法主要做用在ui顯示這一塊,用于更好地顯示字符,防止null字符出現和空指針** @param str 要驗證的字符串* @return 參數若為空或“”或null字符串,則返回空,反之直接返回原有值*/public static String formatString(String str) {if (TextUtils.isEmpty(str))return null;if ("null".equalsIgnoreCase(str))return null;return str;}/*** 去掉url中的路徑,留下請求參數部分* @param strURL url地址* @return url請求參數部分*/private static String TruncateUrlPage(String strURL){String strAllParam=null;String[] arrSplit=null;strURL=strURL.trim().toLowerCase();arrSplit=strURL.split("[?]");if(strURL.length()>1){if(arrSplit.length>1){if(arrSplit[1]!=null){strAllParam=arrSplit[1];}}}return strAllParam;}/*** 解析出url參數中的鍵值對* 如 "index.jsp?Action=del&id=123",解析出Action:del,id:123存入map中* @param URL url地址* @return url請求參數部分*/public static Map<String, String> URLRequest(String URL){Map<String, String> mapRequest = new HashMap<String, String>();String[] arrSplit=null;String strUrlParam=TruncateUrlPage(URL);if(strUrlParam==null){return mapRequest;}//每個鍵值為一組 www.2cto.comarrSplit=strUrlParam.split("[&]");for(String strSplit:arrSplit){String[] arrSplitEqual=null;arrSplitEqual= strSplit.split("[=]");//解析出鍵值if(arrSplitEqual.length>1){//正確解析mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);}else{if(arrSplitEqual[0]!=""){//只有參數沒有值,不加入mapRequest.put(arrSplitEqual[0], "");}}}return mapRequest;} }

        逐字顯示

        /*** 作者:created by meixi* 郵箱:15913707499@163.com* 日期:2019/4/23 11*/public class FadeInTextView extends TextView {private Rect textRect = new Rect();private StringBuffer stringBuffer = new StringBuffer();private String[] arr;private int textCount;private int currentIndex = -1;/*** 每個字出現的時間*/private int duration = 300;private ValueAnimator textAnimation;private TextAnimationListener textAnimationListener;public FadeInTextView setTextAnimationListener(TextAnimationListener textAnimationListener) {this.textAnimationListener = textAnimationListener;return this;}public FadeInTextView(Context context) {this(context, null);}public FadeInTextView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}@Overrideprotected void onDraw(final Canvas canvas) {super.onDraw(canvas); // 使用setText代替重繪就不用自己去繪制text了 // if (stringBuffer != null) { // drawText(canvas, stringBuffer.toString()); // }}/*** 繪制文字** @param canvas 畫布*/private void drawText(Canvas canvas, String textString) {textRect.left = getPaddingLeft();textRect.top = getPaddingTop();textRect.right = getWidth() - getPaddingRight();textRect.bottom = getHeight() - getPaddingBottom();Paint.FontMetricsInt fontMetrics = getPaint().getFontMetricsInt();int baseline = (textRect.bottom + textRect.top - fontMetrics.bottom - fontMetrics.top) / 2;//文字繪制到整個布局的中心位置canvas.drawText(textString, getPaddingLeft(), baseline, getPaint());}/*** 文字逐個顯示動畫 通過插值的方式改變數據源*/private void initAnimation() {//從0到textCount - 1 是設置從第一個字到最后一個字的變化因子textAnimation = ValueAnimator.ofInt(0, textCount - 1);//執行總時間就是每個字的時間乘以字數textAnimation.setDuration(textCount * duration);//勻速顯示文字textAnimation.setInterpolator(new LinearInterpolator());textAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator valueAnimator) {int index = (int) valueAnimator.getAnimatedValue();//過濾去重,保證每個字只重繪一次if (currentIndex != index) {stringBuffer.append(arr[index]);currentIndex = index;//所有文字都顯示完成之后進度回調結束動畫if (currentIndex == (textCount - 1)) {if (textAnimationListener != null) {textAnimationListener.animationFinish();}}//新思路的做法setText(stringBuffer.toString());/*** 之前的做法刷新重繪text,需要自己控制文字的繪制,* 看到網友的評論開拓了思路,既然是直接集成TextView* 就可以直接使用setText()方法進行設置值了*///invalidate();老思路的做法}}});}/*** 設置逐漸顯示的字符串** @param textString* @return*/public FadeInTextView setTextString(String textString) {if (textString != null) {//總字數textCount = textString.length();//存放單個字的數組arr = new String[textCount];for (int i = 0; i < textCount; i++) {arr[i] = textString.substring(i, i + 1);}initAnimation();}return this;}/*** 開啟動畫** @return*/public FadeInTextView startFadeInAnimation() {if (textAnimation != null) {stringBuffer.setLength(0);currentIndex = -1;textAnimation.start();}return this;}/*** 停止動畫** @return*/public FadeInTextView stopFadeInAnimation() {if (textAnimation != null) {textAnimation.end();}return this;}/*** 回調接口*/public interface TextAnimationListener {void animationFinish();} }

        fadeInTextView = (FadeInTextView)findViewById(R.id.fadete); fadeInTextView.setTextString("自定義view實現字符串逐字顯示!"); fadeInTextView.startFadeInAnimation();

        view邊框控制:https://blog.csdn.net/meixi_android/article/details/77374362

        總結

        以上是生活随笔為你收集整理的android TextView下划线,圆角边框,数逐字显示,虚线边框, 渐变色背景框, 阴影背景框的全部內容,希望文章能夠幫你解決所遇到的問題。

        如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。