标签:
一、自定义进度条
1.<ProgressBar android:id="@+id/patch_progress" style="@style/gxProgressStyle" android:layout_width="match_parent" android:layout_height="12dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:gravity="center" /> 2.style.xml中引入: <style name="gxProgressStyle" parent="@android:style/Widget.ProgressBar.Horizontal"> <item name="android:progressDrawable">@drawable/progressbarcolor</item> </style> 3.progressbarcolor.xml: <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@android:id/background"> <shape> <corners android:radius="5dip" /> <gradient android:angle="270" android:centerColor="#1B5E9D" android:centerY="0.75" android:endColor="#156096" android:startColor="#156096" /> </shape> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:angle="270" android:centerColor="#2CC2DB" android:endColor="#13A3C8" android:startColor="#63F8FB" /> </shape> </clip> </item> </layer-list>
二、文字描边样式:
package com.ddianle.autoupdate; import com.ddianle.lovedance.commonlibrary.R; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint.Style; import android.text.TextPaint; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.TextView; /** * 有白色描边的文字TextView * */ public class StrokeTextView extends TextView { private TextView borderText = null;// /用于描边的TextView public StrokeTextView(Context context) { super(context); borderText = new TextView(context); init(); } public StrokeTextView(Context context, AttributeSet attrs) { super(context, attrs); borderText = new TextView(context, attrs); init(); } public StrokeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); borderText = new TextView(context, attrs, defStyle); init(); } public void init() { TextPaint tp1 = borderText.getPaint(); tp1.setStrokeWidth(5); // 设置描边宽度 tp1.setStyle(Style.STROKE); // 对文字只描边 borderText.setTextColor(getResources().getColor(R.color.white)); // 设置描边颜色 borderText.setGravity(getGravity()); } @Override public void setLayoutParams(ViewGroup.LayoutParams params) { super.setLayoutParams(params); borderText.setLayoutParams(params); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { CharSequence tt = borderText.getText(); // 两个TextView上的文字必须一致 if (tt == null || !tt.equals(this.getText())) { borderText.setText(getText()); this.postInvalidate(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); borderText.measure(widthMeasureSpec, heightMeasureSpec); } protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); borderText.layout(left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { borderText.draw(canvas); super.onDraw(canvas); } }
三、文字上下滚动TextSwithcher的应用
<TextSwitcher android:id="@+id/ts_tip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" > </TextSwitcher> 设置5s滚动一次: mTipsHandler.sendEmptyMessageDelayed(0, 5000); private Handler mTipsHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (tips == null) { return; } if (tipsIndex >= tips.length) { tipsIndex = 0; } tipsAnimation(tsTip, tips[tipsIndex]); tipsIndex++; mTipsHandler.sendEmptyMessageDelayed(0, TIPS_DELAYED); } }; //为了实现描边效果,用两个TextSwitcher实现 textSwitcherSetting(ts1,true); textSwitcherSetting(ts2,false); // 设置TextSwither,boolean判断是否加描边 private void textSwitcherSetting(TextSwitcher ts, final boolean isStroke) { ts.setFactory(new ViewFactory() { @Override public View makeView() { TextView tv = new TextView(context, null); if (isStroke) { TextPaint paint = tv.getPaint(); paint.setStrokeWidth(12); paint.setStyle(Style.STROKE); } tv.setTextSize(12); tv.setGravity(Gravity.CENTER); if (isStroke) { tv.setTextColor(context.getResources().getColor(R.color.black)); } else { tv.setTextColor(context.getResources().getColor(R.color.white)); } return tv; } }); tipsAnimation(ts, context.getString(ResourceUtil.getStringId(context, "ddl_tips_loading"))); } // TextSwitcher动画 private void tipsAnimation(TextSwitcher ts, String str) { ts.setText(str); // 设置切入动画 ts.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.move_in)); // 设置切出动画 ts.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.move_out)); }
自定义进度条\文字描边样式\文字上下滚动TextSwithcher的应用
标签:
原文地址:http://my.oschina.net/u/1429620/blog/502929