您好,登錄后才能下訂單哦!
本文實例為大家分享了Android實現簡單文字描邊功能的具體代碼,供大家參考,具體內容如下
效果圖:
實現代碼:
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); //設置抗鋸齒 paint.setAntiAlias(true); //設置防抖動 paint.setDither(true); //設置填充方式 paint.setStyle(Paint.Style.FILL_AND_STROKE); //View的點擊事件 setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { count++; invalidate();//重新繪制 } }); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //設置方形畫筆,背景。 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); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。