您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡單文字描邊功能的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
實(shí)現(xiàn)代碼:
package com.example.zhangyu.myview.widget; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Picture; import android.graphics.Rect; import android.graphics.RectF; import android.os.Build; import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; import android.util.AttributeSet; import android.view.View; import com.example.zhangyu.myview.R; public class TouchPullView extends View { //圓的半徑 private float mCircleRadius; private Paint paint; private Rect rect=new Rect(); private int count; public TouchPullView(Context context) { super(context); init(); } public TouchPullView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } /** * 初始化 */ private void init() { paint = new Paint(Paint.ANTI_ALIAS_FLAG); //設(shè)置抗鋸齒 paint.setAntiAlias(true); //設(shè)置防抖動 paint.setDither(true); //設(shè)置填充方式 paint.setStyle(Paint.Style.FILL_AND_STROKE); //View的點(diǎn)擊事件 setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { count++; invalidate();//重新繪制 } }); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //設(shè)置方形畫筆,背景。 paint.setColor(Color.YELLOW); //先繪制底板 canvas.drawRect(0, 0, getWidth(), getHeight(), paint); float x = getWidth() / 2; float y = getHeight() / 2; float offset = getWidth()/6; //繪制圓形 paint.setColor(Color.GRAY); mCircleRadius = offset*1.41f; canvas.drawCircle(x, y, mCircleRadius, paint); //繪制圖片 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic1); if (bitmap != null) { canvas.drawBitmap(bitmap,null,new RectF(x-offset,y-offset,x+offset,y+offset),paint); } //繪制文字外層的文字,邊框 String s = String.valueOf(count); paint.setStrokeWidth(10); paint.setColor(Color.RED); paint.setTextSize(100); paint.getTextBounds(s,0,s.length(),rect); float textWidth = rect.width(); float textHeight = rect.height(); x = getWidth()/2-textWidth/2; y = getHeight() / 2+textHeight/2; canvas.drawText(s, x, y, paint); //繪制文字 paint.setColor(Color.BLUE); paint.setStrokeWidth(0); canvas.drawText(s, x, y, paint); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。