在WCF數據拜訪中應用緩存進步Winform字段中文顯示速度的辦法。本站提示廣大學習愛好者:(在WCF數據拜訪中應用緩存進步Winform字段中文顯示速度的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是在WCF數據拜訪中應用緩存進步Winform字段中文顯示速度的辦法正文
要用TextView應用突變色,那我們就必需要懂得LinearGradient(線性突變)的用法。
LinearGradient的參數說明
LinearGradient也稱作線性襯著,LinearGradient的感化是完成某一區域內色彩的線性突變後果,看源碼你就曉得他是shader的子類。
它有兩個結構函數
public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, Shader.TileMode tile) public LinearGradient (float x0, float y0, float x1, float y1, int[] colors, float[] positions, Shader.TileMode tile);
個中,參數x0表現突變的肇端點x坐標;參數y0表現突變的肇端點y坐標;參數x1表現突變的起點x坐標;參數y1表現突變的起點y坐標 ;color0表現突變開端色彩;color1表現突變停止色彩;參數tile表現平鋪方法。
Shader.TileMode有3種參數可供選擇,分離為CLAMP、REPEAT和MIRROR:
CLAMP的感化是假如襯著器超越原始界限規模,則會復制邊沿色彩對超越規模的區域停止著色
REPEAT的感化是在橫向和縱向上以平鋪的情勢反復襯著位圖
MIRROR的感化是在橫向和縱向上以鏡像的方法反復襯著位圖
LinearGradient的簡略應用
先完成文字後果的程度突變:
Shader shader_horizontal= new LinearGradient(btWidth/4, 0, btWidth, 0, Color.RED, Color.GREEN, Shader.TileMode.CLAMP); tv_text_horizontal.getPaint().setShader(shader_horizontal);
再完成文字的垂直突變後果:
Shader shader_vertical=new LinearGradient(0, btHeight/4, 0, btHeight, Color.RED, Color.GREEN, Shader.TileMode.CLAMP); tv_text_vertical.getPaint().setShader(shader_vertical);
接上去來完成文字的色彩靜態突變後果:
import android.content.Context; import android.graphics.Canvas; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Shader; import android.util.AttributeSet; import android.widget.TextView; /** * Created on 2016/3/13. */ public class GradientHorizontalTextView extends TextView { private LinearGradient mLinearGradient; private Matrix mGradientMatrix;//突變矩陣 private Paint mPaint;//畫筆 private int mViewWidth = 0;//textView的寬 private int mTranslate = 0;//平移量 private boolean mAnimating = true;//能否動畫 private int delta = 15;//挪動增量 public GradientHorizontalTextView(Context ctx) { this(ctx,null); } public GradientHorizontalTextView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (mViewWidth == 0) { mViewWidth = getMeasuredWidth(); if (mViewWidth > 0) { mPaint = getPaint(); String text = getText().toString(); int size; if(text.length()>0) { size = mViewWidth*2/text.length(); }else{ size = mViewWidth; } mLinearGradient = new LinearGradient(-size, 0, 0, 0, new int[] { 0x33ffffff, 0xffffffff, 0x33ffffff }, new float[] { 0, 0.5f, 1 }, Shader.TileMode.CLAMP); //邊沿融會 mPaint.setShader(mLinearGradient);//設置突變 mGradientMatrix = new Matrix(); } } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mAnimating && mGradientMatrix != null) { float mTextWidth = getPaint().measureText(getText().toString());//取得文字寬 mTranslate += delta;//默許向右挪動 if (mTranslate > mTextWidth+1 || mTranslate<1) { delta = -delta;//向左挪動 } mGradientMatrix.setTranslate(mTranslate, 0); mLinearGradient.setLocalMatrix(mGradientMatrix); postInvalidateDelayed(30);//刷新 } } }
完成歌詞進度後果
Canvas 作為繪制文本時,應用FontMetrics對象,盤算地位的坐標。它的思緒和java.awt.FontMetrics的根本雷同。
FontMetrics對象它以四個根本坐標為基准,分離為:
FontMetrics.top
FontMetrics.ascent
FontMetrics.descent
FontMetrics.bottom
// FontMetrics對象 FontMetrics fontMetrics = textPaint.getFontMetrics(); String text = "abcdefghijklmnopqrstu"; // 盤算每個坐標 float baseX = 0; float baseY = 100; float topY = baseY + fontMetrics.top; float ascentY = baseY + fontMetrics.ascent; float descentY = baseY + fontMetrics.descent; float bottomY = baseY + fontMetrics.bottom;
上面是詳細完成代碼:
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; /** * Created on 2016/3/13. */ public class SongTextView extends View { private int postIndex; private Paint mPaint; private int delta = 15; private float mTextHeight; private float mTextWidth; private String mText="夢 裡 面 看 我 七 十 二 變"; private PorterDuffXfermode xformode; public SongTextView(Context ctx) { this(ctx,null); } public SongTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SongTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); xformode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); mPaint.setColor(Color.CYAN); mPaint.setTextSize(60.0f); mPaint.setStyle(Paint.Style.FILL_AND_STROKE); mPaint.setXfermode(null); mPaint.setTextAlign(Paint.Align.LEFT); //文字准確高度 Paint.FontMetrics fontMetrics = mPaint.getFontMetrics(); mTextHeight = fontMetrics.bottom-fontMetrics.descent-fontMetrics.ascent; mTextWidth = mPaint.measureText(mText); } /** *盤算 控件的寬高 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { final int mWidth; final int mHeight; /** * 設置寬度 */ int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); if (widthMode == MeasureSpec.EXACTLY)// match_parent , accurate mWidth = widthSize; else { // 由圖片決議的寬 int desireByImg = getPaddingLeft() + getPaddingRight() + getMeasuredWidth(); if (widthMode == MeasureSpec.AT_MOST)// wrap_content mWidth = Math.min(desireByImg, widthSize); else mWidth = desireByImg; } /*** * 設置高度 */ int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (heightMode == MeasureSpec.EXACTLY)// match_parent , accurate mHeight = heightSize; else { int desire = getPaddingTop() + getPaddingBottom() + getMeasuredHeight(); if (heightMode == MeasureSpec.AT_MOST)// wrap_content mHeight = Math.min(desire, heightSize); else mHeight = desire; } setMeasuredDimension( mWidth, mHeight); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Bitmap srcBitmap = Bitmap.createBitmap(getMeasuredWidth(),getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas srcCanvas = new Canvas(srcBitmap); srcCanvas.drawText(mText, 0, mTextHeight, mPaint); mPaint.setXfermode(xformode); mPaint.setColor(Color.RED); RectF rectF = new RectF(0,0,postIndex,getMeasuredHeight()); srcCanvas.drawRect(rectF, mPaint); canvas.drawBitmap(srcBitmap, 0, 0, null); init(); if(postIndex<mTextWidth) { postIndex+=10; }else{ postIndex=0; } postInvalidateDelayed(30); } }
ProgressBar完成歌詞播放後果
然後接上去的這類歌詞播放進度後果是2張圖片完成的,忘卻是哪一個那邊看來的,壓根之前也沒有想過還可以這麼樣的完成。
只須要預備2張圖便可:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/normal" /> <item android:id="@android:id/progress" android:drawable="@drawable/grandient" /> </layer-list>
看見沒就是2張圖片,一張作為配景圖一張作為進度圖,是否是感到很奇異,然後放入ProgressBar
<ProgressBar android:id="@+id/pb1" android:layout_width="300dp" android:layout_height="40dp" android:max="100" android:maxHeight="2dp" android:minHeight="2dp" android:progress="20" android:progressDrawable="@drawable/m_progress_horizontal" android:secondaryProgress="30" android:visibility="gone"/>
再加上代碼靜態轉變progress就可以完成進度的變更了:
ProgressBar pb1= (ProgressBar) findViewById(R.id.pb1); //設置轉動條可見 setProgressBarIndeterminateVisibility(true); progress=pb1.getProgress();//獲得初始進度 timer=new Timer(); task=new TimerTask() { @Override public void run() { progress+=10; if(progress>100){ progress=0; } handler.sendEmptyMessage(0); } }; timer.schedule(task,1000,300);
完成及進度的轉變:
Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); pb1.setProgress(progress); } }; @Override protected void onDestroy() { super.onDestroy(); timer=null; task=null; handler.removeCallbacksAndMessages(null); }
後果也是不錯的:
才能無限,感到寫一篇博客要弄很久,網速卡的一筆,就寫到這了,其實項目外面也沒有效到,歇息2天了也寫點器械,就認為照樣要學一點器械作為備用常識。
以上內容是小編給年夜家引見的Android完成歌詞突變色和進度的後果,願望對年夜家有所贊助!